-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
89 lines (60 loc) · 1.74 KB
/
benchmark.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import annotations
import asyncio
import pstats
import time
from cProfile import Profile
from pprint import pprint
from arclet.letoderea import Contexts, Param, Provider, es
loop = asyncio.new_event_loop()
class TestEvent:
async def gather(self, context: Contexts): ...
class TestProvider(Provider[str]):
def validate(self, param: Param):
return param.name == "a"
async def __call__(self, context: Contexts) -> str | None:
return "1"
@es.on(TestEvent)
async def test_subscriber(a):
pass
a = TestEvent()
ctx: Contexts = {"$event": a} # type: ignore
tasks = []
pprint(test_subscriber.params)
count = 20000
tasks.extend(loop.create_task(test_subscriber.handle(ctx.copy())) for _ in range(count))
async def main():
await asyncio.gather(*tasks)
s = time.perf_counter_ns()
loop.run_until_complete(main())
e = time.perf_counter_ns()
n = e - s
print("RUN 1:")
print(f"used {n/10e8}, {count*10e8/n}o/s")
print(f"{n / count} ns per task with {count} tasks gather")
async def main1():
for _ in range(count):
await test_subscriber.handle(ctx.copy())
s = time.perf_counter_ns()
loop.run_until_complete(main1())
e = time.perf_counter_ns()
n = e - s
print("RUN 2:")
print(f"used {n/10e8}, {count*10e8/n}o/s")
print(f"{n / count} ns per loop with {count} loops")
# tasks.clear()
# tasks.extend(
# es.loop.create_task(depend_handler(test_subscriber, a))
# for _ in range(count)
# )
async def main2():
for _ in range(count):
await test_subscriber.handle(ctx.copy())
prof = Profile()
prof.enable()
loop.run_until_complete(main2())
prof.disable()
prof.create_stats()
stats = pstats.Stats(prof)
stats.strip_dirs()
stats.sort_stats("tottime")
stats.print_stats(20)