forked from Wendyl42/proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.sh
409 lines (340 loc) · 10.4 KB
/
driver.sh
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
#!/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: 2/8/2016
#
# 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"
#####
# Helper functions
#
#
# 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
#
# 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 "basicScore: $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}"
python3 ./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 "concurrencyScore: $concurrencyScore/${MAX_CONCURRENCY}"
#####
# Caching
#
echo ""
echo "*** Cache ***"
# 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}
echo "Success: Was able to fetch tiny/${FETCH_FILE} from the cache."
else
cacheScore=0
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 "cacheScore: $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}"
exit