asyncinject icon indicating copy to clipboard operation
asyncinject copied to clipboard

Turn that visualization hack into a feature

Open simonw opened this issue 2 years ago • 4 comments

Hack from:

  • https://github.com/simonw/asyncinject/issues/10#issuecomment-1101822735

simonw avatar Apr 18 '22 22:04 simonw

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

simonw avatar Apr 18 '22 22:04 simonw

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)

simonw avatar Apr 18 '22 22:04 simonw

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?

adriangb avatar Apr 18 '22 22:04 adriangb

My hope is that the .timer hook itself is enough that people who use OpenTelemetry could easily plug it in.

simonw avatar Apr 18 '22 22:04 simonw