PerfTimer - code timer with Trio async support

PerfTimer seems like yet another code execution timer-- but wait! The package also includes TrioPerfTimer, which is aware of the Trio scheduler and able to deduct periods when a task is blocked. So you can measure the CPU time of any section of code including async calls.

If you’re not looking for a fancy context-manager timer thing, the package also has a trio_perf_counter() primitive, which follows the semantics of the stdlib time perf counters but in the context of a Trio task. Use it to build your own instrumentation stuff!

1 Like

perf-timer has recently been updated:

  • now tracks standard deviation by default
  • percentile tracking is available PerfTimer(..., observer=HistogramObserver), which can report arbitrary percentile latencies
  • overhead of the various observer and timer types has been benchmarked. On an old laptop, the default PerfTimer takes < 2 µs per observation, and TrioPerfTimer takes < 5 µs.

example output

default (avg ± stddev, max):

timer "process thumbnail": avg 73.1 µs ± 18.0 µs, max 320.5 µs in 292 runs

histogram (avg ± stddev, percentiles):

timer "test": avg 117ms ± 128ms, 50% ≤ 81.9ms, 90% ≤ 243ms in 50 runs
1 Like