def create_trace_event(token_string, timestamp_us, trace_id, data): token_values = token_string.split("|") return trace.TraceEvent(event_type=get_trace_type( token_values[TokenIdx.EventType]), module=token_values[TokenIdx.Module], label=token_values[TokenIdx.Label], timestamp_us=timestamp_us, group=token_values[TokenIdx.Group], trace_id=trace_id, flags=token_values[TokenIdx.Flag], has_data=has_data(token_string), data_fmt=(token_values[TokenIdx.data_fmt] if has_data(token_string) else ""), data=data if has_data(token_string) else b'')
def test_generate_single_json_event(self): event = trace.TraceEvent(event_type=trace.TraceType.Instantaneous, module="module", label="label", timestamp_us=10) json_lines = trace.generate_trace_json([event]) self.assertEqual(1, len(json_lines)) self.assertEqual(json.loads(json_lines[0]), { "ph": "I", "pid": "module", "name": "label", "ts": 10, "s": "p" })
def test_generate_json_data_arg_label(self): event = trace.TraceEvent( event_type=trace.TraceType.Instantaneous, module="module", label="", # Is replaced by data string timestamp_us=10, has_data=True, data_fmt="@pw_arg_label", data=bytes("arg", "utf-8")) json_lines = trace.generate_trace_json([event]) self.assertEqual(1, len(json_lines)) self.assertEqual(json.loads(json_lines[0]), { "ph": "I", "pid": "module", "name": "arg", "ts": 10, "s": "p" })
def test_generate_json_data_struct_fmt_single(self): event = trace.TraceEvent(event_type=trace.TraceType.Instantaneous, module="module", label="counter", timestamp_us=10, has_data=True, data_fmt="@pw_py_struct_fmt:H", data=(5).to_bytes(2, byteorder="little")) json_lines = trace.generate_trace_json([event]) self.assertEqual(1, len(json_lines)) self.assertEqual( json.loads(json_lines[0]), { "ph": "I", "pid": "module", "name": "counter", "ts": 10, "s": "p", "args": { "data_0": 5 } })
def test_generate_json_data_struct_fmt_multi(self): event = trace.TraceEvent(event_type=trace.TraceType.Instantaneous, module="module", label="counter", timestamp_us=10, has_data=True, data_fmt="@pw_py_struct_fmt:Hl", data=struct.pack("Hl", 5, 2)) json_lines = trace.generate_trace_json([event]) self.assertEqual(1, len(json_lines)) self.assertEqual( json.loads(json_lines[0]), { "ph": "I", "pid": "module", "name": "counter", "ts": 10, "s": "p", "args": { "data_0": 5, "data_1": 2 } })
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. """Tests the trace module.""" import json import struct import unittest from pw_trace import trace test_events = [ trace.TraceEvent(trace.TraceType.Instantaneous, "m1", "L1", 1), trace.TraceEvent(trace.TraceType.InstantaneousGroup, "m2", "L2", 2, "G2"), trace.TraceEvent(trace.TraceType.AsyncStep, "m3", "L3", 3, "G3", 103), trace.TraceEvent(trace.TraceType.DurationStart, "m4", "L4", 4), trace.TraceEvent(trace.TraceType.DurationGroupStart, "m5", "L5", 5, "G5"), trace.TraceEvent(trace.TraceType.AsyncStart, "m6", "L6", 6, "G6", 106), trace.TraceEvent(trace.TraceType.DurationEnd, "m7", "L7", 7), trace.TraceEvent(trace.TraceType.DurationGroupEnd, "m8", "L8", 8, "G8"), trace.TraceEvent(trace.TraceType.AsyncEnd, "m9", "L9", 9, "G9", 109) ] test_json = [ {"ph": "I", "pid": "m1", "name": "L1", "ts": 1, "s": "p"}, {"ph": "I", "pid": "m2", "tid": "G2", "name": "L2", "ts": 2, "s": "t"}, {"ph": "n", "pid": "m3", "tid": "G3", "name": "L3", "ts": 3, \ "scope": "G3", "cat": "m3", "id": 103, "args": {"id": 103}},