-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.sh
More file actions
executable file
·508 lines (420 loc) · 13.2 KB
/
Copy pathdriver.sh
File metadata and controls
executable file
·508 lines (420 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/bin/bash
#
# driver.sh - This is a simple autograder for the Proxy Lab. It does
# basic sanity checks that determine whether or not the code
# behaves like a concurrent caching proxy.
#
# David O'Hallaron, Carnegie Mellon University
# updated: 12/09/2013
#
# Stanley Zhang <szz@andrew.cmu.edu>
# updated: 4/6/2017
# Added full key.txt checking
#
# Stanley Zhang <szz@andrew.cmu.edu>
# updated: 11/11/2017
# Added new tiny-static caching test
#
# usage: ./driver.sh
#
# Point values
MAX_BASIC=40
MAX_CONCURRENCY=15
MAX_CACHE=15
# Various constants
HOME_DIR=`pwd`
PROXY_DIR="./.proxy"
NOPROXY_DIR="./.noproxy"
TIMEOUT=5
MAX_RAND=63000
PORT_START=1024
PORT_MAX=65000
MAX_PORT_TRIES=10
# List of text and binary files for the basic test
BASIC_LIST="home.html
csapp.c
tiny.c
godzilla.jpg
tiny"
# List of text files for the cache test
CACHE_LIST="tiny.c
home.html
csapp.c"
# The file we will fetch for various tests
FETCH_FILE="home.html"
# Number of requests to tiny-static for cache tests
CACHE_STATIC_REQS=20
# The static file that tiny-static serves
STATIC_FILE="home.html"
#####
# Helper functions
#
#
# check_key - Checks key.txt for correct key
# usage: check_key
# returns 255 if the key is malformed, or 0 otherwise.
#
function check_key {
local -a lines
mapfile -n 2 -t lines < 'key.txt'
local andrewid="${lines[0]}"
local key="${lines[1]}"
if [[ $andrewid == "Enter your andrew id here" ]]
then
echo "Error: key.txt: Missing andrew id."
return 255
fi
if [[ $key == "Enter the key from TheProjectZone here" ]]
then
echo "Error: key.txt: Missing key."
return 255
fi
return 0
}
#
# download_proxy - download a file from the origin server via the proxy
# usage: download_proxy <testdir> <filename> <origin_url> <proxy_url>
#
function download_proxy {
cd $1
curl --max-time ${TIMEOUT} --silent --proxy $4 --output $2 $3
(( $? == 28 )) && echo "Error: Fetch timed out after ${TIMEOUT} seconds"
cd $HOME_DIR
}
#
# download_noproxy - download a file directly from the origin server
# usage: download_noproxy <testdir> <filename> <origin_url>
#
function download_noproxy {
cd $1
curl --max-time ${TIMEOUT} --silent --output $2 $3
(( $? == 28 )) && echo "Error: Fetch timed out after ${TIMEOUT} seconds"
cd $HOME_DIR
}
#
# clear_dirs - Clear the download directories
#
function clear_dirs {
rm -rf ${PROXY_DIR}/*
rm -rf ${NOPROXY_DIR}/*
}
#
# wait_for_port_use - Spins until the TCP port number passed as an
# argument is actually being used. Times out after 5 seconds.
#
function wait_for_port_use() {
timeout_count="0"
portsinuse=`netstat --numeric-ports --numeric-hosts -a --protocol=tcpip \
| grep tcp | cut -c21- | cut -d':' -f2 | cut -d' ' -f1 \
| grep -E "[0-9]+" | uniq | tr "\n" " "`
echo "${portsinuse}" | grep -wq "${1}"
while [ "$?" != "0" ]
do
timeout_count=`expr ${timeout_count} + 1`
if [ "${timeout_count}" == "${MAX_PORT_TRIES}" ]; then
kill -ALRM $$
fi
sleep 1
portsinuse=`netstat --numeric-ports --numeric-hosts -a --protocol=tcpip \
| grep tcp | cut -c21- | cut -d':' -f2 | cut -d' ' -f1 \
| grep -E "[0-9]+" | uniq | tr "\n" " "`
echo "${portsinuse}" | grep -wq "${1}"
done
}
#
# free_port - returns an available unused TCP port
#
function free_port {
# Generate a random port in the range [PORT_START,
# PORT_START+MAX_RAND]. This is needed to avoid collisions when many
# students are running the driver on the same machine.
port=$((( RANDOM % ${MAX_RAND}) + ${PORT_START}))
while [ TRUE ]
do
portsinuse=`netstat --numeric-ports --numeric-hosts -a --protocol=tcpip \
| grep tcp | cut -c21- | cut -d':' -f2 | cut -d' ' -f1 \
| grep -E "[0-9]+" | uniq | tr "\n" " "`
echo "${portsinuse}" | grep -wq "${port}"
if [ "$?" == "0" ]; then
if [ $port -eq ${PORT_MAX} ]
then
echo "-1"
return
fi
port=`expr ${port} + 1`
else
echo "${port}"
return
fi
done
}
#######
# Main
#######
######
# Verify that we have all of the expected files with the right
# permissions
#
check_key
if [ $? != 0 ]
then
echo "Error: missing or malformed key.txt. Please re-read the writeup!"
exit
fi
# Kill any stray proxies or tiny servers owned by this user
killall -q proxy tiny nop-server.py 2> /dev/null
# Make sure we have a Tiny directory
if [ ! -d ./tiny ]
then
echo "Error: ./tiny directory not found."
exit
fi
# If there is no Tiny executable, then try to build it
if [ ! -x ./tiny/tiny ]
then
echo "Building the tiny executable."
(cd ./tiny; make)
echo ""
fi
# Make sure we have all the Tiny files we need
if [ ! -x ./tiny/tiny ]
then
echo "Error: ./tiny/tiny not found or not an executable file."
exit
fi
for file in ${BASIC_LIST}
do
if [ ! -e ./tiny/${file} ]
then
echo "Error: ./tiny/${file} not found."
exit
fi
done
# Make sure we have an existing executable proxy
if [ ! -x ./proxy ]
then
echo "Error: ./proxy not found or not an executable file. Please rebuild your proxy and try again."
exit
fi
# Make sure we have an existing executable nop-server.py file
if [ ! -x ./nop-server.py ]
then
echo "Error: ./nop-server.py not found or not an executable file."
exit
fi
# Create the test directories if needed
if [ ! -d ${PROXY_DIR} ]
then
mkdir ${PROXY_DIR}
fi
if [ ! -d ${NOPROXY_DIR} ]
then
mkdir ${NOPROXY_DIR}
fi
# Add a handler to generate a meaningful timeout message
trap 'echo "Timeout waiting for the server to grab the port reserved for it"; kill $$' ALRM
#####
# Basic
#
echo "*** Basic ***"
# Run the Tiny Web server
tiny_port=$(free_port)
echo "Starting tiny on ${tiny_port}"
cd ./tiny
./tiny ${tiny_port} &> /dev/null &
tiny_pid=$!
cd ${HOME_DIR}
# Wait for tiny to start in earnest
wait_for_port_use "${tiny_port}"
# Run the proxy
proxy_port=$(free_port)
echo "Starting proxy on ${proxy_port}"
./proxy ${proxy_port} &> /dev/null &
proxy_pid=$!
# Wait for the proxy to start in earnest
wait_for_port_use "${proxy_port}"
# Now do the test by fetching some text and binary files directly from
# Tiny and via the proxy, and then comparing the results.
numRun=0
numSucceeded=0
for file in ${BASIC_LIST}
do
numRun=`expr $numRun + 1`
echo "${numRun}: ${file}"
clear_dirs
# Fetch using the proxy
echo " Fetching ./tiny/${file} into ${PROXY_DIR} using the proxy"
download_proxy $PROXY_DIR ${file} "http://localhost:${tiny_port}/${file}" "http://localhost:${proxy_port}"
# Fetch directly from Tiny
echo " Fetching ./tiny/${file} into ${NOPROXY_DIR} directly from Tiny"
download_noproxy $NOPROXY_DIR ${file} "http://localhost:${tiny_port}/${file}"
# Compare the two files
echo " Comparing the two files"
diff -q ${PROXY_DIR}/${file} ${NOPROXY_DIR}/${file} # &> /dev/null
if [ $? -eq 0 ]; then
numSucceeded=`expr ${numSucceeded} + 1`
echo " Success: Files are identical."
else
echo " Failure: Files differ."
fi
done
echo "Killing tiny and proxy"
kill $tiny_pid 2> /dev/null
wait $tiny_pid 2> /dev/null
kill $proxy_pid 2> /dev/null
wait $proxy_pid 2> /dev/null
basicScore=`expr ${MAX_BASIC} \* ${numSucceeded} / ${numRun}`
echo "Basic: $basicScore / ${MAX_BASIC}"
######
# Concurrency
#
echo ""
echo "*** Concurrency ***"
# Run the Tiny Web server
tiny_port=$(free_port)
echo "Starting tiny on port ${tiny_port}"
cd ./tiny
./tiny ${tiny_port} &> /dev/null &
tiny_pid=$!
cd ${HOME_DIR}
# Wait for tiny to start in earnest
wait_for_port_use "${tiny_port}"
# Run the proxy
proxy_port=$(free_port)
echo "Starting proxy on port ${proxy_port}"
./proxy ${proxy_port} &> /dev/null &
proxy_pid=$!
# Wait for the proxy to start in earnest
wait_for_port_use "${proxy_port}"
# Run a special blocking nop-server that never responds to requests
nop_port=$(free_port)
echo "Starting the blocking NOP server on port ${nop_port}"
./nop-server.py ${nop_port} &> /dev/null &
nop_pid=$!
# Wait for the nop server to start in earnest
wait_for_port_use "${nop_port}"
# Try to fetch a file from the blocking nop-server using the proxy
clear_dirs
echo "Trying to fetch a file from the blocking nop-server"
download_proxy $PROXY_DIR "nop-file.txt" "http://localhost:${nop_port}/nop-file.txt" "http://localhost:${proxy_port}" &
# Fetch directly from Tiny
echo "Fetching ./tiny/${FETCH_FILE} into ${NOPROXY_DIR} directly from Tiny"
download_noproxy $NOPROXY_DIR ${FETCH_FILE} "http://localhost:${tiny_port}/${FETCH_FILE}"
# Fetch using the proxy
echo "Fetching ./tiny/${FETCH_FILE} into ${PROXY_DIR} using the proxy"
download_proxy $PROXY_DIR ${FETCH_FILE} "http://localhost:${tiny_port}/${FETCH_FILE}" "http://localhost:${proxy_port}"
# See if the proxy fetch succeeded
echo "Checking whether the proxy fetch succeeded"
diff -q ${PROXY_DIR}/${FETCH_FILE} ${NOPROXY_DIR}/${FETCH_FILE} # &> /dev/null
if [ $? -eq 0 ]; then
concurrencyScore=${MAX_CONCURRENCY}
echo "Success: Was able to fetch tiny/${FETCH_FILE} from the proxy."
else
concurrencyScore=0
echo "Failure: Was not able to fetch tiny/${FETCH_FILE} from the proxy."
fi
# Clean up
echo "Killing tiny, proxy, and nop-server"
kill $tiny_pid 2> /dev/null
wait $tiny_pid 2> /dev/null
kill $proxy_pid 2> /dev/null
wait $proxy_pid 2> /dev/null
kill $nop_pid 2> /dev/null
wait $nop_pid 2> /dev/null
echo "Concurrency: $concurrencyScore / ${MAX_CONCURRENCY}"
#####
# Caching
#
echo ""
echo "*** Cache ***"
cacheScore=0
echo "*** Testing single request ***"
# Run the Tiny Web server
tiny_port=$(free_port)
echo "Starting tiny on port ${tiny_port}"
cd ./tiny
./tiny ${tiny_port} &> /dev/null &
tiny_pid=$!
cd ${HOME_DIR}
# Wait for tiny to start in earnest
wait_for_port_use "${tiny_port}"
# Run the proxy
proxy_port=$(free_port)
echo "Starting proxy on port ${proxy_port}"
./proxy ${proxy_port} &> /dev/null &
proxy_pid=$!
# Wait for the proxy to start in earnest
wait_for_port_use "${proxy_port}"
# Fetch some files from tiny using the proxy
clear_dirs
for file in ${CACHE_LIST}
do
echo "Fetching ./tiny/${file} into ${PROXY_DIR} using the proxy"
download_proxy $PROXY_DIR ${file} "http://localhost:${tiny_port}/${file}" "http://localhost:${proxy_port}"
done
# Kill Tiny
echo "Killing tiny"
kill $tiny_pid 2> /dev/null
wait $tiny_pid 2> /dev/null
# Now try to fetch a cached copy of one of the fetched files.
echo "Fetching a cached copy of ./tiny/${FETCH_FILE} into ${NOPROXY_DIR}"
download_proxy $NOPROXY_DIR ${FETCH_FILE} "http://localhost:${tiny_port}/${FETCH_FILE}" "http://localhost:${proxy_port}"
# See if the proxy fetch succeeded by comparing it with the original
# file in the tiny directory
diff -q ./tiny/${FETCH_FILE} ${NOPROXY_DIR}/${FETCH_FILE} # &> /dev/null
if [ $? -eq 0 ]; then
$((cacheScore += MAX_CACHE / 3))
echo "Success: Was able to fetch tiny/${FETCH_FILE} from the cache."
echo "*** Testing multiple requests ***"
# Run the tiny-static server
tiny_static_port=$(free_port)
echo "Starting tiny-static on port ${tiny_static_port}"
cd ./tiny
./tiny-static ${tiny_static_port} &> /dev/null &
tiny_static_pid=$!
cd ${HOME_DIR}
# Wait for tiny-static to start in earnest
wait_for_port_use "${tiny_static_port}"
echo "Warming up cache with ${CACHE_STATIC_REQS} unique requests"
# Fetch many different URLs - they should all be the same.
for ((n=0; n<=CACHE_STATIC_REQS; n++)); do
download_proxy $PROXY_DIR /dev/null "http://localhost:${tiny_static_port}/${n}" "http://localhost:${proxy_port}"
done
# Kill tiny-static
echo "Killing tiny-static"
kill $tiny_static_pid 2> /dev/null
wait $tiny_static_pid 2> /dev/null
# Now try to fetch a cached copy of each requested URL.
echo "Attempting to re-fetch ${CACHE_STATIC_REQS} requests from cache"
failure=0
clear_dirs
for ((n=0; n<=CACHE_STATIC_REQS; n++)); do
download_proxy $PROXY_DIR ${STATIC_FILE} "http://localhost:${tiny_static_port}/${n}" "http://localhost:${proxy_port}"
diff -q ./tiny/${STATIC_FILE} ${PROXY_DIR}/${STATIC_FILE}
if [ $? -ne 0 ]; then
echo "Failure: was not able to fetch request ${n} from proxy cache"
failure=1
break
fi
rm ${PROXY_DIR}/${STATIC_FILE}
done
if [ $failure -eq 0 ]; then
echo "Success: fetched all ${CACHE_STATIC_REQS} requests from cache"
$((cacheScore += MAX_CACHE / 3 * 2))
fi
else
echo "Failure: Was not able to fetch tiny/${FETCH_FILE} from the proxy cache."
fi
# Kill the proxy
echo "Killing proxy"
kill $proxy_pid 2> /dev/null
wait $proxy_pid 2> /dev/null
echo "Cache: $cacheScore / ${MAX_CACHE}"
# Emit the total score
totalScore=`expr ${basicScore} + ${cacheScore} + ${concurrencyScore}`
maxScore=`expr ${MAX_BASIC} + ${MAX_CACHE} + ${MAX_CONCURRENCY}`
echo ""
echo "totalScore = ${totalScore} / ${maxScore}"
echo ""
echo "{ \"scores\": {\"Basic\":${basicScore}, \"Concurrency\":${concurrencyScore}, \"Caching\":${cacheScore}}, \"scoreboard\": [${totalScore}, ${basicScore}, ${concurrencyScore}, ${cacheScore}]}"
exit