/optimised_uasyncio_benchmark.py Secret
Created
November 6, 2020 09:04
Star
You must be signed in to star a gist
slightly optimised version of Peter Hinch's benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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