Skip to content

Commit 40b43ed

Browse files
committed
Refactor bomber
1 parent 9a1d879 commit 40b43ed

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

benchmark/async.py

+35-22
Original file line numberDiff line numberDiff line change
@@ -176,35 +176,48 @@ def render_GET(self, request):
176176

177177
@asyncio.coroutine
178178
def attack(count, concurrency, connector, loop, url):
179-
sem = asyncio.Semaphore(concurrency)
180179
request = aiohttp.request
181180

182-
@asyncio.coroutine
183-
def do_bomb(rnd):
184-
real_url = url + '/test/' + rnd
185-
with (yield from sem):
186-
t1 = loop.time()
187-
resp = yield from request('GET', real_url,
188-
connector=connector, loop=loop)
189-
assert resp.status == 200, resp.status
190-
if 'text/plain; charset=utf-8' != resp.headers['Content-Type']:
191-
raise AssertionError('Invalid Content-Type: %r' % resp.headers)
192-
body = yield from resp.text()
193-
assert body == ('Hello, ' + rnd), rnd
194-
t2 = loop.time()
195-
return t2 - t1
181+
in_queue = collections.deque()
182+
out_times = collections.deque()
183+
processed_count = 0
196184

197-
bombs = []
185+
@asyncio.coroutine
186+
def do_bomb():
187+
nonlocal processed_count
188+
while in_queue:
189+
rnd = in_queue.popleft()
190+
real_url = url + '/test/' + rnd
191+
try:
192+
t1 = loop.time()
193+
resp = yield from request('GET', real_url,
194+
connector=connector, loop=loop)
195+
assert resp.status == 200, resp.status
196+
if 'text/plain; charset=utf-8' != resp.headers['Content-Type']:
197+
raise AssertionError('Invalid Content-Type: %r' %
198+
resp.headers)
199+
body = yield from resp.text()
200+
assert body == ('Hello, ' + rnd), rnd
201+
t2 = loop.time()
202+
out_times.append(t2 - t1)
203+
processed_count += 1
204+
except Exception:
205+
continue
198206

199207
for i in range(count):
200208
rnd = ''.join(random.sample(string.ascii_letters, 16))
201-
bombs.append(asyncio.async(do_bomb(rnd)))
209+
in_queue.append(rnd)
210+
211+
bombers = []
212+
for i in range(concurrency):
213+
bomber = asyncio.async(do_bomb())
214+
bombers.append(bomber)
202215

203216
t1 = loop.time()
204-
data = (yield from asyncio.gather(*bombs))
217+
yield from asyncio.gather(*bombers)
205218
t2 = loop.time()
206-
rps = count / (t2 - t1)
207-
return rps, data
219+
rps = processed_count / (t2 - t1)
220+
return rps, out_times
208221

209222

210223
@asyncio.coroutine
@@ -280,8 +293,8 @@ def main(argv):
280293
times_stdev = tstd(times)
281294
times_median = float(median(times))
282295
print('Results for', test_name)
283-
print('RPS: {:d},\tmean: {:.3f} μs,'
284-
'\tstandard deviation {:.3f} μs\tmedian {:.3f} μs'
296+
print('RPS: {:d},\tmean: {:.3f} ms,'
297+
'\tstandard deviation {:.3f} ms\tmedian {:.3f} ms'
285298
.format(int(rps_mean),
286299
times_mean,
287300
times_stdev,

0 commit comments

Comments
 (0)