-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathninjatracing_test
executable file
·110 lines (101 loc) · 4.61 KB
/
ninjatracing_test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
# Copyright 2018 Nico Weber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
import ninjatracing
import unittest
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
class TestNinjaTracing(unittest.TestCase):
def test_simple(self):
log = StringIO("# ninja log v5\n"
"100\t200\t0\tmy_output\tdeadbeef\n"
"50\t120\t0\tmy_first_output\t0afef\n")
dicts = list(ninjatracing.log_to_dicts(log, 42, {'showall': True}))
expected = [{
'name': 'my_output', 'cat': 'targets', 'ph': 'X',
'ts': 100000, 'dur': 100000, 'pid': 42, 'tid': 0,
'args': {}
}, {
'name': 'my_first_output', 'cat': 'targets', 'ph': 'X',
'ts': 50000, 'dur': 70000, 'pid': 42, 'tid': 1,
'args': {}
},
]
self.assertEqual(expected, dicts)
def test_last_only(self):
# Test the behavior without --showall.
log = StringIO("# ninja log v5\n"
"100\t200\t0\tmy_output\tdeadbeef\n"
"50\t120\t0\tmy_first_output\t0afef\n")
dicts = list(ninjatracing.log_to_dicts(log, 42, {'showall': False}))
expected = [{
'name': 'my_first_output', 'cat': 'targets', 'ph': 'X',
'ts': 50000, 'dur': 70000, 'pid': 42, 'tid': 0,
'args': {}
},
]
self.assertEqual(expected, dicts)
def test_multiple_outputs(self):
# Both lines here have the same command hash and the same start
# and end times, meaning they were produced by the same command.
log = StringIO("# ninja log v5\n"
"100\t200\t0\toutput\tdeadbeef\n"
"100\t200\t0\tother_output\tdeadbeef\n")
dicts = list(ninjatracing.log_to_dicts(log, 42, {'showall': True}))
expected = [{
'name': 'output, other_output', 'cat': 'targets', 'ph': 'X',
'ts': 100000, 'dur': 100000, 'pid': 42, 'tid': 0,
'args': {}
},
]
self.assertEqual(expected, dicts)
def test_trace(self):
# Simple test for converting time trace to dictionaries. Tests removing
# incomplete, "Total" and short events, replacing tid and pid and
# adjusting timestamp.
trace = StringIO('{ "traceEvents": ['
'{ "dur": 1500, "name": "LongEvent", "ph": "X", "pid": 1, "tid": 12345, "ts": 1000 },'
'{ "dur": 500, "name": "TooShort", "ph": "X", "pid": 1, "tid": 12345, "ts": 1000 },'
'{ "args": { "avg ms": 1, "count": 2 }, "dur": 1111, "name": "Total Count", "ph": "X", "pid": 1, "tid": 12345, "ts": 0 },'
'{ "args": { "name": "clang" }, "cat": "", "name": "process_name", "ph": "M", "pid": 1, "tid": 0, "ts": 0 }'
']}')
target = ninjatracing.Target(5, 10)
dicts = list(ninjatracing.trace_to_dicts(target, trace, {'granularity': 1000}, 42, 5))
expected = [{
'dur': 1500, 'name': 'LongEvent', 'ph': 'X', 'pid': 42, 'tid': 5, 'ts': 6000},
]
self.assertEqual(expected, dicts)
def test_comments(self):
log = StringIO("# ninja log v5\n"
"#\n"
"100\t200\t0\tmy_output\tdeadbeef\n"
"# 100\t666\t0\tignored_output\tdeadbeef\n"
"50\t120\t0\tmy_first_output\t0afef\n"
"# lastline")
dicts = list(ninjatracing.log_to_dicts(log, 42, {'showall': True}))
expected = [{
'name': 'my_output', 'cat': 'targets', 'ph': 'X',
'ts': 100000, 'dur': 100000, 'pid': 42, 'tid': 0,
'args': {}
}, {
'name': 'my_first_output', 'cat': 'targets', 'ph': 'X',
'ts': 50000, 'dur': 70000, 'pid': 42, 'tid': 1,
'args': {}
},
]
self.assertEqual(expected, dicts)
if __name__ == '__main__':
unittest.main()