asyncinject
asyncinject copied to clipboard
Turn that visualization hack into a feature
Hack from:
- https://github.com/simonw/asyncinject/issues/10#issuecomment-1101822735
def visualize_traces(traces, chars=80):
start = min(t[1] for t in traces)
end = max(t[2] for t in traces)
width = end - start
pos = lambda value1, value2: int((value1 - value2) * chars / width)
for trace in traces:
name, p1, p2 = trace[0], pos(trace[1], start), pos(trace[2], trace[1])
print("{}: started {:5f}, took {:5f}".format(name, trace[1] - start, trace[2] - trace[1]))
print(' ' * p1, '*' * p2)
Output looks like this:
a: started 0.000022, took 1.000659
****************************************
b: started 0.000000, took 2.000802
*******************************************************************************
c: started 1.000875, took 1.000121
***************************************
d: started 2.001063, took 0.000000
That's when run against data collected using this pattern:
registry = Registry(a, b, c, d)
collected = []
registry.timer = lambda name, start, end: collected.append((name, start, end))
await registry.resolve(d)
visualize_traces(collected)
This almost feels like a tracing API. I wonder if it could just be made compatible with OpenTelemetry so that existing tooling could be used for visualization?
My hope is that the .timer
hook itself is enough that people who use OpenTelemetry could easily plug it in.