| 170 | |
| 171 | @asyncio.coroutine |
| 172 | def attack(count, concurrency, client, loop, url): |
| 173 | |
| 174 | out_times = collections.deque() |
| 175 | processed_count = 0 |
| 176 | |
| 177 | def gen(): |
| 178 | for i in range(count): |
| 179 | rnd = ''.join(random.sample(string.ascii_letters, 16)) |
| 180 | yield rnd |
| 181 | |
| 182 | @asyncio.coroutine |
| 183 | def do_bomb(in_iter): |
| 184 | nonlocal processed_count |
| 185 | for rnd in in_iter: |
| 186 | real_url = url + '/test/' + rnd |
| 187 | try: |
| 188 | t1 = loop.time() |
| 189 | resp = yield from client.get(real_url) |
| 190 | assert resp.status == 200, resp.status |
| 191 | if 'text/plain; charset=utf-8' != resp.headers['Content-Type']: |
| 192 | raise AssertionError('Invalid Content-Type: %r' % |
| 193 | resp.headers) |
| 194 | body = yield from resp.text() |
| 195 | yield from resp.release() |
| 196 | assert body == ('Hello, ' + rnd), rnd |
| 197 | t2 = loop.time() |
| 198 | out_times.append(t2 - t1) |
| 199 | processed_count += 1 |
| 200 | except Exception: |
| 201 | continue |
| 202 | |
| 203 | in_iter = gen() |
| 204 | bombers = [] |
| 205 | for i in range(concurrency): |
| 206 | bomber = asyncio.async(do_bomb(in_iter)) |
| 207 | bombers.append(bomber) |
| 208 | |
| 209 | t1 = loop.time() |
| 210 | yield from asyncio.gather(*bombers) |
| 211 | t2 = loop.time() |
| 212 | rps = processed_count / (t2 - t1) |
| 213 | return rps, out_times |
| 214 | |
| 215 | |
| 216 | @asyncio.coroutine |