-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyhaste_demo.py
56 lines (37 loc) · 1 KB
/
pyhaste_demo.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
import time
from random import uniform
from pyhaste import measure, report
def prepare():
print("Preparing")
time.sleep(uniform(0.5, 1.5))
def find_items():
with measure("db.find"):
print("Finding items")
time.sleep(uniform(0.1, 0.3))
return [1, 2, 3]
def process_item(item: int):
with measure("process_item"):
print(f"Processing {item}")
time.sleep(uniform(0.05, 0.25))
return item**2
def save_item():
with measure("db.save"):
print("Saving items")
time.sleep(uniform(0.01, 0.02))
def do_stuff():
items = find_items()
for item in items:
process_item(item)
save_item()
if __name__ == "__main__":
with measure("prepare"):
prepare()
time.sleep(uniform(0.1, 0.25))
elapsed = 0
while elapsed < 6:
start = time.perf_counter()
with measure("do_stuff"):
do_stuff()
elapsed += time.perf_counter() - start
time.sleep(uniform(0.1, 0.25))
report()