-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
54 lines (35 loc) · 1.29 KB
/
monitor.py
File metadata and controls
54 lines (35 loc) · 1.29 KB
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
53
54
import asyncio
import aiohttp
import matplotlib.pyplot as plt
PROBING_INTERVAL_SECONDS = 1
TOTAL_PROBES = 60
data = []
async def on_request_start(session, trace_config_ctx, params):
trace_config_ctx.start = asyncio.get_event_loop().time()
async def on_request_end(session, trace_config_ctx, params):
elapsed = asyncio.get_event_loop().time() - trace_config_ctx.start
if params.response.status != 200 or await params.response.text() != "I'm alive":
elapsed = 0
print(".")
data.append((trace_config_ctx.start, elapsed))
# print(await params.response.text())
# print("Request took {}".format(elapsed))
trace_config = aiohttp.TraceConfig()
trace_config.on_request_start.append(on_request_start)
trace_config.on_request_end.append(on_request_end)
async def probe(sleep=0):
async with aiohttp.ClientSession(trace_configs=[trace_config]) as session:
await asyncio.sleep(sleep)
try:
await session.get("https://lab.polymed.online/health/")
except:
pass
async def main():
async with asyncio.TaskGroup() as tg:
for offset in range(TOTAL_PROBES):
tg.create_task(probe(offset * PROBING_INTERVAL_SECONDS))
asyncio.run(main())
data.sort()
times, values = zip(*data)
plt.plot(times, values)
plt.show()