Skip to content

Commit 0302d6d

Browse files
committed
Add undocumented metrics module.
1 parent cc73e07 commit 0302d6d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

v3/as_drivers/metrics/__init__.py

Whitespace-only changes.

v3/as_drivers/metrics/metrics.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import uasyncio as asyncio
2+
from utime import ticks_us, ticks_diff
3+
4+
5+
def metrics():
6+
ncalls = 0
7+
max_d = 0
8+
min_d = 100_000_000
9+
tot_d = 0
10+
st = 'Max {}μs Min {}μs Avg {}μs No. of calls {} Freq {}'
11+
async def func():
12+
nonlocal ncalls, max_d, min_d, tot_d
13+
while True:
14+
tstart = ticks_us()
15+
t_last = None
16+
while ticks_diff(t := ticks_us(), tstart) < 10_000_000:
17+
await asyncio.sleep(0)
18+
if ncalls:
19+
dt = ticks_diff(t, t_last)
20+
max_d = max(max_d, dt)
21+
min_d = min(min_d, dt)
22+
tot_d += dt
23+
ncalls += 1
24+
t_last = t
25+
print(st.format(max_d, min_d, tot_d//ncalls, ncalls, ncalls//10))
26+
ncalls = 0
27+
max_d = 0
28+
min_d = 100_000_000
29+
tot_d = 0
30+
return func
31+
32+
async def main():
33+
asyncio.create_task(metrics()())
34+
while True:
35+
await asyncio.sleep(0)
36+
37+
asyncio.run(main())

0 commit comments

Comments
 (0)