-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmicrobenchmark.py
98 lines (72 loc) · 2.76 KB
/
microbenchmark.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
90
91
92
93
94
95
96
97
98
import asyncio
import time
import trio
import greenback
from functools import partial
from greenback import await_
checkpoint = trio.lowlevel.checkpoint
ITERS = 100000
async def busy():
for _ in range(ITERS):
await checkpoint()
def sync_busy():
for _ in range(ITERS):
await_(checkpoint())
async def each_busy():
def step():
await_(checkpoint())
async def astep():
step()
for _ in range(ITERS):
await greenback.with_portal_run(astep)
async def each_sync_busy():
def step():
await_(checkpoint())
for _ in range(ITERS):
await greenback.with_portal_run_sync(step)
async def each_pass():
def step():
pass
async def astep():
step()
for _ in range(ITERS):
await greenback.with_portal_run(astep)
async def each_sync_pass():
def step():
pass
for _ in range(ITERS):
await greenback.with_portal_run_sync(step)
async def adapt_sync(fn):
fn()
async def timeit(label, afn, *args):
vals = []
for idx in range(5):
t0 = time.monotonic()
await afn(*args)
t1 = time.monotonic()
vals.append((t1 - t0) * (1e6 / ITERS))
print(f"{label}: {sum(vals)/len(vals):.1f} {vals!r}")
async def main():
# Baseline: checkpoint with no greenback involved.
await timeit("checkpoints", busy)
# Greenback portal installed, but calling checkpoint normally.
await timeit("checkpoints in one portal", greenback.with_portal_run, busy)
# Greenback portal installed, calling checkpoint through await_ each time.
await timeit("await_(checkpoint)s in one portal", greenback.with_portal_run, adapt_sync, sync_busy)
# Greenback portal installed, calling checkpoint many times in a single await_.
await timeit("checkpoints in one (portal + await_)", greenback.with_portal_run, adapt_sync, lambda: await_(busy()))
# Simpler portal from with_portal_run_sync(), calling checkpoint through await_.
await timeit("await_(checkpoint)s in one sync portal", greenback.with_portal_run_sync, sync_busy)
# Create one with_portal_run() portal per checkpoint, and await_ the checkpoint.
await timeit("[async portal + await_ + checkpoint]s", each_busy)
# Create one with_portal_run_sync() portal per checkpoint, and await_ the checkpoint.
await timeit("[sync portal + await_ + checkpoint]s", each_sync_busy)
# Create one with_portal_run() portal per checkpoint, and await_ the checkpoint.
await timeit("async portal creations", each_pass)
# Create one with_portal_run_sync() portal per checkpoint, and await_ the checkpoint.
await timeit("sync portal creations", each_sync_pass)
print("--- Trio ---")
trio.run(main)
print("--- asyncio ---")
checkpoint = partial(asyncio.sleep, 0)
asyncio.run(main())