/
bench.py
331 lines (275 loc) · 8.51 KB
/
bench.py
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
# -*- coding: utf-8 -*-
import sys
import threading
import httplib
import urllib
import time
import uuid
from subprocess import Popen, PIPE, STDOUT
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
SCORE_HOST = '127.0.0.1'
URL_TXT = '/home/ec2-user/url.txt'
HTTP_LOAD = '/home/ec2-user/http_load'
DEFAULT_SECONDS = 10
DEFAULT_CONCURRENCY = 10
PATH_GET = '/blojsom/blog/default/2012/03/23/test'
PATH_POST = '/blojsom/blog/default/2012/03/23/test'
PARAMS_POST = {
'comment': 'y',
'entry_id': '1',
'permalink': 'test',
'redirect_to': PATH_GET,
'author': 'test',
'authorEmail': '',
'authorURL': '',
'submit': 'Comment',
}
class CheckerThread(object):
def __init__(self, host, seconds):
self._host = host
self._seconds = float(seconds)
self._cond = threading.Condition(threading.Lock())
self._thread = threading.Thread(target=self._run)
self._thread.daemon = True
self._value = None
def _run(self):
try:
start_time = time.time()
comment = ''
num_ok = 0
num_err = 0
num_post = 0
loop = 0
while True:
if loop % 2:
if self.get_page(comment):
num_ok += 1
else:
num_err += 1
#check elapsed
elapsed = time.time() - start_time
if elapsed > self._seconds:
break
else:
comment = self.post_comment()
num_post += 1
loop += 1
score = float(num_post) / elapsed * 10
percent = float(num_ok) / float(num_ok + num_err)
value = (score, percent, num_post)
except Exception, e:
import traceback
print traceback.format_exc()
value = e
finally:
self._cond.acquire()
try:
self._value = value
self._cond.notify()
finally:
self._cond.release()
def get_page(self, comment):
try:
ret = http_get(self._host, PATH_GET)
except Exception:
return False
if comment not in ret:
return False
return True
def post_comment(self):
comment = uuid.uuid4().hex
params = {'commentText': comment}
params.update(PARAMS_POST)
try:
http_post(self._host, PATH_POST, params, timeout=60)
except Exception:
pass
return comment
def start(self):
self._thread.start()
def wait(self, timeout=None):
self._cond.acquire()
try:
if self._value is None:
self._cond.wait(timeout or 60)
finally:
self._cond.release()
def get(self, timeout=None):
self.wait(timeout)
if isinstance(self._value, tuple):
return self._value
raise self._value
def http_get(host, path, timeout=60):
headers = {'User-Agent': 'http_load 12mar2006', 'Connection': 'close'}
conn = httplib.HTTPConnection(host, timeout=timeout)
try:
conn.request("GET", path, headers=headers)
r = conn.getresponse()
status = r.status
if 200 != status:
return None
return r.read() #OK
finally:
conn.close()
def http_post(host, path, params, timeout=60):
headers = {'User-Agent': 'http_load 12mar2006', 'Connection': 'close',
'Content-type': 'application/x-www-form-urlencoded'}
params_encoded = urllib.urlencode(params)
conn = httplib.HTTPConnection(host, timeout=timeout)
try:
conn.request("POST", path, params_encoded, headers=headers)
r = conn.getresponse()
status = r.status
if 200 != status:
return None
return r.read() #OK
finally:
conn.close()
def run_bench(host, seconds, concurrency):
#http://localhost/foo/bar
if host.startswith('http://'):
host, _ = host.split('//', 1)[1].split('/', 1)
#create URL_TXT for http_load
init_url_txt(host)
#start CheckerThread
checker = CheckerThread(host, seconds)
checker.start()
#start http_load
rets = run_http_load(concurrency, seconds)
#parse result of http_load
score_http_load = parse_http_load_result(rets, seconds)
#get result of CheckerThread
score_checker, percent, num_post = checker.get()
score_total = score_http_load + score_checker
score = score_total * percent * percent
print 'Score: %.3f (get=%.3f, comment=%.3f(%d), check=%.3f)' % (
score, score_http_load, score_checker, num_post, percent)
#post score to score server
if 10 == concurrency:
send_score(score)
return score
def init_url_txt(host):
#create URL_TXT for http_load
f = open(URL_TXT, 'wb')
try:
f.write('http://%s%s' % (host, PATH_GET))
finally:
f.close()
def run_http_load(concurrency, seconds):
#http_load -parallel 9 -seconds 10 url.txt
args = [HTTP_LOAD, '-parallel', str(concurrency - 1), '-seconds', str(seconds), URL_TXT]
p = Popen(args, stdout=PIPE, stderr=STDOUT)
#wait http_load
try:
rets = []
for line in p.stdout.readlines():
line = line.rstrip()
if line.endswith('byte count wrong'):
continue #ignore ... ?
rets.append(line)
print line
p.wait()
except:
p.terminate()
raise
return rets
def parse_http_load_result(lines, seconds):
assert lines[0].split(',')[-1].endswith('seconds')
if lines[5] == 'HTTP response codes:':
statuses = lines[6:]
elif lines[6] == 'HTTP response codes:':
statuses = lines[7:]
else:
raise ValueError()
fetches = int(lines[0].split(',')[0].split(' ')[0])
num_ok, num_err = parse_status_lines(statuses)
#assert num_ok + num_err == fetches
#succeeds_percent = num_ok / fetches
#assert 1.0 >= succeeds_percent
elapsed = float(lines[0].split(' in ')[-1].split(' ')[0])
return float(num_ok) / elapsed
def test_parse_http_load_result():
ret = parse_http_load_result("""\
1485 fetches, 1 max parallel, 2.02792e+07 bytes, in 3.10000 seconds
13656 mean bytes/connection
494.978 fetches/sec, 6.75942e+06 bytes/sec
msecs/connect: 0.143007 mean, 0.23 max, 0.072 min
msecs/first-response: 1.87696 mean, 19.596 max, 1.644 min
HTTP response codes:
code 200 -- 1485""".splitlines(), 3)
assert 1437 == int(ret), ret
ret = parse_http_load_result("""\
1485 fetches, 1 max parallel, 2.02792e+07 bytes, in 3.00013 seconds
13656 mean bytes/connection
494.978 fetches/sec, 6.75942e+06 bytes/sec
msecs/connect: 0.143007 mean, 0.23 max, 0.072 min
msecs/first-response: 1.87696 mean, 19.596 max, 1.644 min
HTTP response codes:
code 200 -- 1400
code 503 -- 85""".splitlines(), 3)
assert 1399 == int(ret), ret
ret = parse_http_load_result("""\
135 fetches, 9 max parallel, 3.1411e+06 bytes, in 10.0001 seconds
23267.4 mean bytes/connection
13.4998 fetches/sec, 314105 bytes/sec
msecs/connect: 0.17497 mean, 1.287 max, 0.045 min
msecs/first-response: 572.619 mean, 787.905 max, 373.871 min
126 bad byte counts
HTTP response codes:
code 200 -- 135""".splitlines(), 3)
assert 1399 == int(ret), ret
def parse_status_lines(lines):
num_ok = 0
num_err = 0
for line in lines:
ll, rr = line.split(' -- ')
c = int(ll.split(' ')[-1])
n = int(rr)
if 200 == c:
num_ok += n
else:
num_err += n
return num_ok, num_err
def send_score(score):
return #for admin's benchmark
r = http_post(SCORE_HOST, '/post_result', {'score': score}, timeout=3)
if r:
print 'Score sending was succeeded.'
else:
print 'Score sending was failed.'
def main():
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-c', type='int', dest='concurrency', default=DEFAULT_CONCURRENCY,
help='Number of multiple requests to make')
parser.add_option('-s', type='int', dest='seconds', default=DEFAULT_SECONDS,
help='Number of seconds to perform')
parser.add_option("--mode", dest="mode", default=None)
options, args = parser.parse_args()
#run tests
if 'test' == options.mode:
test_parse_http_load_result()
return
if 1 != len(args):
parser.print_help()
return
assert 1 <= options.concurrency <= 100
assert 1 <= options.seconds <= 100
#run bench
old_sys_stderr = sys.stderr
sys.stderr = StringIO()
try:
run_bench(args[0], options.seconds, options.concurrency)
except Exception, e:
print e
import traceback
print traceback.format_exc()
except KeyboardInterrupt:
print 'KeyboardInterrupt'
finally:
sys.stderr = old_sys_stderr
if __name__ == '__main__':
main()