Skip to content

Instantly share code, notes, and snippets.

@hatarist
Created August 3, 2017 00:01
Show Gist options
  • Save hatarist/7e8f39e61ef8f26ef1255a1fbd661f25 to your computer and use it in GitHub Desktop.
Save hatarist/7e8f39e61ef8f26ef1255a1fbd661f25 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import asyncio
import aiohttp
import ujson
async def get(semaphore, session, url):
async with semaphore as sem:
return await session.get(url)
async def flood(total=10, concurrent=2):
sem = asyncio.Semaphore(concurrent)
async with aiohttp.client.ClientSession() as session:
tasks = [get(sem, session, 'https://now.httpbin.org/') for _ in range(total)]
results = await asyncio.gather(*tasks)
epochs = [ujson.loads(await result.text())['now']['epoch'] for result in results]
print('min:', min(epochs))
print('max:', max(epochs))
print('avg:', (max(epochs) - min(epochs)) / total)
if __name__ == '__main__':
print('50 concurrent requests, 50 total:')
asyncio.get_event_loop().run_until_complete(
flood(total=50, concurrent=50)
)
print('25 concurrent requests, 50 total:')
asyncio.get_event_loop().run_until_complete(
flood(total=50, concurrent=25)
)
print('10 concurrent requests, 50 total:')
asyncio.get_event_loop().run_until_complete(
flood(total=50, concurrent=10)
)
print('5 concurrent requests, 50 total:')
asyncio.get_event_loop().run_until_complete(
flood(total=50, concurrent=5)
)
print('1 concurrent request, 50 total:')
asyncio.get_event_loop().run_until_complete(
flood(total=50, concurrent=1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment