@@ -176,35 +176,48 @@ def render_GET(self, request):
176
176
177
177
@asyncio .coroutine
178
178
def attack (count , concurrency , connector , loop , url ):
179
- sem = asyncio .Semaphore (concurrency )
180
179
request = aiohttp .request
181
180
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
196
184
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
198
206
199
207
for i in range (count ):
200
208
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 )
202
215
203
216
t1 = loop .time ()
204
- data = ( yield from asyncio .gather (* bombs ) )
217
+ yield from asyncio .gather (* bombers )
205
218
t2 = loop .time ()
206
- rps = count / (t2 - t1 )
207
- return rps , data
219
+ rps = processed_count / (t2 - t1 )
220
+ return rps , out_times
208
221
209
222
210
223
@asyncio .coroutine
@@ -280,8 +293,8 @@ def main(argv):
280
293
times_stdev = tstd (times )
281
294
times_median = float (median (times ))
282
295
print ('Results for' , test_name )
283
- print ('RPS: {:d},\t mean: {:.3f} μs ,'
284
- '\t standard deviation {:.3f} μs \t median {:.3f} μs '
296
+ print ('RPS: {:d},\t mean: {:.3f} ms ,'
297
+ '\t standard deviation {:.3f} ms \t median {:.3f} ms '
285
298
.format (int (rps_mean ),
286
299
times_mean ,
287
300
times_stdev ,
0 commit comments