-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmsparser_test.py
286 lines (241 loc) · 9.11 KB
/
msparser_test.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright (c) 2011 Mathieu Turcotte
# Licensed under the MIT license.
# Enable with statement in Python 2.5.
# This has to be the first statement.
from __future__ import with_statement
# Python 2.5 doesn't have the json module.
try:
import json
except ImportError:
import simplejson as json
import msparser
import os
import os.path
import sys
# Use unittest2 on versions older than Python 2.7.
if sys.version_info[0] < 3 and sys.version_info[1] < 7:
from unittest2 import TestCase, main
else:
from unittest import TestCase, main
class FakeContext():
def __init__(self, lines=[], filename="fake.txt", baseline=0):
self.index_ = 0
self.lines_ = lines
self.filename_ = filename
self.baseline_ = baseline
def set_content(self, lines, baseline=0):
self.index_ = 0
self.lines_ = lines
self.baseline_ = baseline
def line(self):
return self.baseline_ + self.index_
def readline(self):
if len(self.lines_) > self.index_:
line = self.lines_[self.index_] + "\n"
self.index_ += 1
return line
else:
return ""
def filename(self):
return self.filename_
class ParseHeaderTest(TestCase):
def setUp(self):
self.ctx = FakeContext()
self.mdata = {}
def parse_header(self, lines, baseline=0):
self.ctx.set_content(lines, baseline)
msparser._parse_header(self.ctx, self.mdata)
def test_parse_header(self):
self.parse_header([
"desc: --time-unit=B --pages-as-heap=yes",
"cmd: ./thompson --log=2 --fastpath",
"time_unit: B"
])
self.assertEqual(self.mdata["desc"],
"--time-unit=B --pages-as-heap=yes")
self.assertEqual(self.mdata["cmd"], "./thompson --log=2 --fastpath")
self.assertEqual(self.mdata["time_unit"], "B")
def test_parse_malformed_header(self):
lines = [
"desc: --time-unit=B --pages-as-heap=yes",
"cmd: ./thompson --log=2 --fastpath",
"snapshot=1"
]
try:
self.parse_header(lines, 0)
self.fail("ParseError should have been thrown.")
except msparser.ParseError:
# Capture the exception in a Python 2.5/3.x compatible way.
# See Python porting guide for details: http://goo.gl/yho0u
err = sys.exc_info()[1]
self.assertEqual(err.line, 3)
self.assertEqual(err.filename, self.ctx.filename())
class ParseHeapTreeTest(TestCase):
def parse_heap_tree(self, lines):
self.ctx = FakeContext()
self.ctx.set_content(lines)
return msparser._parse_heap_tree(self.ctx)
def test_parse_one_level_simple(self):
tree = self.parse_heap_tree([
"n0: 50456 0x804BFC0: DancingLinksSolver::build_cover_matrix("
"Sudoku&) (SudokuSolver.cpp:30)"
])
self.assertEqual(tree["nbytes"], 50456)
self.assertEqual(len(tree["children"]), 0)
self.assertEqual(tree["details"], {
"function": "DancingLinksSolver::build_cover_matrix(Sudoku&)",
"address": "0x804BFC0",
"file": "SudokuSolver.cpp",
"line": 30
})
def test_parse_one_level_empty_filename(self):
tree = self.parse_heap_tree(["n0: 24305664 0x7FF0007A5: ???"])
self.assertEqual(tree["nbytes"], 24305664)
self.assertEqual(len(tree["children"]), 0)
self.assertEqual(tree["details"], {
"function": "???",
"address": "0x7FF0007A5",
"file": None,
"line": None
})
def test_parse_one_level_page_allocation(self):
tree = self.parse_heap_tree([
"n0: 165990400 (page allocation syscalls) mmap/mremap/brk, "
"--alloc-fns, etc."
])
self.assertEqual(tree["nbytes"], 165990400)
self.assertEqual(len(tree["children"]), 0)
self.assertEqual(tree["details"], None)
def test_parse_one_level_below_threshold(self):
tree = self.parse_heap_tree([
"n0: 8192 in 1 place, below massif's threshold (01.00%)"
])
self.assertEqual(tree["nbytes"], 8192)
self.assertEqual(len(tree["children"]), 0)
self.assertEqual(tree["details"], None)
def test_parse_multi_levels(self):
tree = self.parse_heap_tree([
"n2: 165990400 (page allocation syscalls) mmap/mremap/brk, "
"--alloc-fns, etc.",
" n2: 111468544 0x5E70169: mmap (syscall-template.S:82)",
" n0: 83079168 0x5E031E7: malloc (arena.c:824)",
" n0: 8192 in 1 place, below massif's threshold (01.00%)",
" n0: 83079168 0x5DFE377: new_heap (arena.c:554)"
])
self.assertEqual(tree["nbytes"], 165990400)
self.assertEqual(len(tree["children"]), 2)
self.assertEqual(tree["details"], None)
child1 = tree["children"][0]
self.assertEqual(child1["nbytes"], 111468544)
self.assertEqual(len(child1["children"]), 2)
self.assertEqual(child1["details"], {
"function": "mmap",
"address": "0x5E70169",
"file": "syscall-template.S",
"line": 82
})
child11 = child1["children"][0]
self.assertEqual(child11["nbytes"], 83079168)
self.assertEqual(len(child11["children"]), 0)
self.assertEqual(child11["details"], {
"function": "malloc",
"address": "0x5E031E7",
"file": "arena.c",
"line": 824
})
child12 = child1["children"][1]
self.assertEqual(child12["nbytes"], 8192)
self.assertEqual(len(child12["children"]), 0)
self.assertEqual(child12["details"], None)
child2 = tree["children"][1]
self.assertEqual(child2["nbytes"], 83079168)
self.assertEqual(len(child2["children"]), 0)
self.assertEqual(child2["details"], {
"function": "new_heap",
"address": "0x5DFE377",
"file": "arena.c",
"line": 554
})
class ParseSnapshotTest(TestCase):
def setUp(self):
self.ctx = FakeContext()
self.mdata = {
"snapshots": [],
"detailed_snapshot_indices": []
}
def parse_snapshot(self, lines, baseline=0):
self.ctx.set_content(lines, baseline)
msparser._parse_snapshots(self.ctx, self.mdata)
def test_parse_empty_snapshot(self):
self.parse_snapshot([
"#-----------",
"snapshot=1",
"#-----------",
"time=100279",
"mem_heap_B=30035968",
"mem_heap_extra_B=0",
"mem_stacks_B=0",
"heap_tree=empty"
])
self.assertEqual(len(self.mdata["detailed_snapshot_indices"]), 0)
self.assertEqual(self.mdata["snapshots"][0], {
"id": 1,
"time": 100279,
"mem_heap": 30035968,
"mem_heap_extra": 0,
"mem_stack": 0,
"heap_tree": None
})
def test_parse_peak_snapshot(self):
self.parse_snapshot([
"#-----------",
"snapshot=1",
"#-----------",
"time=100279",
"mem_heap_B=30035968",
"mem_heap_extra_B=0",
"mem_stacks_B=0",
"heap_tree=peak",
"n0: 8192 in 1 place, below massif's threshold (01.00%)"
])
self.assertEqual(self.mdata["peak_snapshot_index"], 0)
self.assertEqual(self.mdata["detailed_snapshot_indices"][0], 0)
self.assertEqual(len(self.mdata["detailed_snapshot_indices"]), 1)
self.assertIsNotNone(self.mdata["snapshots"][0]["heap_tree"])
def test_parse_malformed_snapshot(self):
lines = [
"#-----------",
"snapshot=1",
"#-----------",
"time=100279",
"foo=30035968",
"mem_heap_extra_B=0"
]
try:
self.parse_snapshot(lines, 345)
self.fail("ParseError should have been thrown.")
except msparser.ParseError:
# Capture the exception in a Python 2.5/3.x compatible way.
# See Python porting guide for details: http://goo.gl/yho0u
err = sys.exc_info()[1]
self.assertEqual(err.line, 350)
self.assertEqual(err.filename, self.ctx.filename())
class TestFullParse(TestCase):
pass
def make_parse_test(path_to_actual, path_to_expected):
def test_parse(self):
actual = msparser.parse_file(path_to_actual)
with open(path_to_expected) as fd_to_expected:
expected = json.load(fd_to_expected)
self.assertEqual(expected, actual)
return test_parse
for filename in os.listdir("test_data"):
if not filename.endswith("json"):
path_to_actual = os.path.join("test_data", filename)
path_to_expected = path_to_actual + ".json"
test_name = "test" + filename.replace(".", "_")
test_function = make_parse_test(path_to_actual, path_to_expected)
test_function.__doc__ = test_name
setattr(TestFullParse, test_name, test_function)
if __name__ == "__main__":
main()