def basic_event_chain(): event_chain = EventChain('event_chain') event_chain.add_events(( TraceEventDurationBegin('event', 0), TraceEventDurationEnd('event', 1), TraceEventDurationBegin('event', 2), TraceEventDurationEnd('event', 3), )) return event_chain
def setup_trace(n_events=3, n_repeat=0, prefix='event'): trace = Trace() ts = 0 for _ in range(n_repeat + 1): for i in range(n_events): trace.add_event(TraceEventDurationBegin(f'{prefix}{i:03}', ts)) ts += 1 for i in reversed(range(n_events)): trace.add_event(TraceEventDurationEnd(f'{prefix}{i:03}', ts)) ts += 1 return trace
def test_add_event_individually(): trace = Trace() event1_begin = TraceEventDurationBegin('event1', 123) event1_end = TraceEventDurationEnd('event1', 124) event2 = TraceEventInstant('event2', 123.5) trace.add_event(event1_begin) trace.add_event(event1_end) trace.add_event(event2) assert trace.events[0] is event1_begin assert trace.events[1] is event1_end assert trace.events[2] is event2
def test_export_merged_trace_to_tef_json(): trace_analyzer = setup_trace_analyzer(n_repeat=1) trace_analyzer.match('event000+*event000-', 'merged_event') exported_tef_json = trace_analyzer.to_tef_json(1000) expected_trace = setup_trace(n_repeat=1) expected_trace.add_event(TraceEventDurationBegin('merged_event', 0, pid=1000)) expected_trace.add_event(TraceEventDurationEnd('merged_event', 5, pid=1000)) expected_trace.add_event(TraceEventDurationBegin('merged_event', 6, pid=1000)) expected_trace.add_event(TraceEventDurationEnd('merged_event', 11, pid=1000)) assert expected_trace.to_tef_json() == exported_tef_json # Export to file import json import tempfile tmp = tempfile.TemporaryFile('w+t') trace_analyzer.to_tef_json(1000, tmp) tmp.seek(0) assert expected_trace.to_tef_json() == json.load(tmp) tmp.close()
def test_add_events_from_iterable(): trace = Trace() events = [ TraceEventDurationBegin('event1', 123), TraceEventDurationEnd('event1', 124), TraceEventInstant('event2', 123.5), ] trace.add_events(events) assert trace.events[0] == events[0] assert trace.events[1] == events[1] assert trace.events[2] == events[2] events = [ TraceEventDurationBegin('event1', 1230), TraceEventDurationEnd('event1', 1240), TraceEventInstant('event2', 1235), ] trace.add_events(e for e in events) assert trace.events[3] == events[0] assert trace.events[4] == events[1] assert trace.events[5] == events[2]
def test_json_export(): import json import tempfile trace = Trace() event1_begin = TraceEventDurationBegin('event1', 0.001) event1_end = TraceEventDurationEnd('event1', 0.002) event2 = TraceEventInstant('event2', 0.003) trace.add_event(event1_begin) trace.add_event(event1_end) trace.add_event(event2) with tempfile.TemporaryFile('w+t') as tmp: trace.to_tef_json(tmp) tmp.seek(0) readback = json.load(tmp) assert readback['traceEvents'][0] == event1_begin.tef assert readback['traceEvents'][1] == event1_end.tef assert readback['traceEvents'][2] == event2.tef
def test_duration_end_events(): ts = 124 event = TraceEventDurationEnd('duration-event', ts) assert event.tef['ph'] == 'E' assert event.tef['ts'] == ts * 1e3
def test_custom_attributes(): event = TraceEventDurationEnd('event', 0, custom_attr1='foo', custom_attr2=100) assert event.tef['args'] == {'custom_attr1': 'foo', 'custom_attr2': 100}