-
Notifications
You must be signed in to change notification settings - Fork 4
/
demo_aio.py
52 lines (44 loc) · 1.44 KB
/
demo_aio.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
from aiohttp import ClientSession
import asyncio
import random
base = 'http://localhost:3050'
limit = 260
shards = 280
async def bound_post(sem, url, sesion):
async with sem:
headers = {'Authorization': 'bigdog'}
await session.post(url, headers=headers)
async def run_init(session):
tasks = []
sem = asyncio.Semaphore(limit)
for i in range(shards):
url = f'{base}/api/status/main/{i}/3'
task = asyncio.ensure_future(bound_post(sem, url, session))
tasks.append(task)
await asyncio.gather(*tasks)
async def run_healthy(session):
healthy = list(range(shards))
sem = asyncio.Semaphore(limit)
while True:
if len(healthy) > 90:
healthy.remove(random.choice(healthy))
random.shuffle(healthy)
tasks = []
for s in healthy:
url = f'{base}/api/health/main/{s}'
task = asyncio.ensure_future(bound_post(sem, url, session))
await asyncio.sleep(random.random() / 4)
tasks.append(task)
await asyncio.gather(*tasks)
async def run_demo():
while True:
shard = random.randint(0, shards-1)
state = random.randint(0, 6)
url = f'{base}/api/status/main/{shard}/{state}'
del url
async def main(session):
await run_init(session)
await run_healthy(session)
loop = asyncio.get_event_loop()
with ClientSession() as session:
loop.run_until_complete(main(session))