Skip to content

Instantly share code, notes, and snippets.

@chrisovergaauw
Created November 6, 2020 09:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chrisovergaauw/5bc25ae0d0add8258e4f37131824ee70 to your computer and use it in GitHub Desktop.
slightly optimised version of Peter Hinch's benchmark
# rate.py Benchmark for uasyncio. Author Peter Hinch Feb 2018-Apr 2020.
# Benchmark uasyncio round-robin scheduling performance
# This measures the rate at which uasyncio can schedule a minimal coro which
# mereley increments a global.
# Outcome on a Pyboard 1.1
# 100 minimal coros are scheduled at an interval of 195μs on uasyncio V3
# Compares with ~156μs on official uasyncio V2.
import uasyncio as asyncio
num_coros = (100, 200, 500, 1000)
iterations = [0, 0, 0, 0]
duration = 2 # Time to run for each number of coros
count = 0
done = False
async def foo():
global count
sleep_ms = asyncio.sleep_ms
while True:
await sleep_ms(0)
count += 1
async def test():
global count, done
old_n = 0
sleep = asyncio.sleep
for n, n_coros in enumerate(num_coros):
print('Testing {} coros for {}secs'.format(n_coros, duration))
count = 0
r = range(n_coros - old_n)
for _ in r:
asyncio.create_task(foo())
old_n = n_coros
await sleep(duration)
iterations[n] = count
done = True
async def report():
asyncio.create_task(test())
while not done:
await asyncio.sleep(1)
for x, n in enumerate(num_coros):
print('Coros {:4d} Iterations/sec {:5d} Duration {:3d}us'.format(
n, int(iterations[x]/duration), int(duration*1000000/iterations[x])))
asyncio.run(report())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment