1
# -*- coding: utf-8 -*-
7
from collections import OrderedDict, defaultdict, namedtuple
8
from matplotlib import pyplot
10
PAGE_SIZE_BYTES = 4096
12
TEST_NAME = r'(\w+ \([\w\.]+\))'
13
TEST_DOCSTRING = r'[/#,=\(\)\[\]\+\:\.\-\'\w\s]+'
14
TEST_SEP = r' \.\.\. '
15
TEST_RESULT = r'(?:ok|ERROR|FAIL)'
16
MEM_USAGE = r'(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\.\d+)'
18
TEST_RESULT_ONLY = re.compile(TEST_RESULT)
19
TEST_PLUS_MEM = re.compile(TEST_NAME + TEST_SEP + MEM_USAGE)
20
TEST_DOCSTRING_PLUS_MEM = re.compile(TEST_DOCSTRING + TEST_SEP + MEM_USAGE)
21
TEST_ONLY = re.compile(
22
r'%s|%s' % (TEST_NAME + TEST_SEP + TEST_RESULT, TEST_NAME))
23
DOCSTRING_PLUS_RESULT = re.compile(TEST_DOCSTRING + TEST_SEP + TEST_RESULT)
24
MEM_ONLY = re.compile(MEM_USAGE)
27
Stats = namedtuple('Stats', ['usage', 'percentage', 'res_megs'])
30
def mem_stats(groups):
31
assert len(groups) == 5
32
percentage = float(groups.pop(-1))
34
usage = map(int, groups)
35
assert len(usage) == 4
37
res_megs = ((usage[1] * PAGE_SIZE_BYTES) / 1024.) / 1024.
38
return Stats(usage, percentage, res_megs)
41
def parse_individual(filename):
42
with open(filename) as f:
47
match = MEM_ONLY.match(line)
49
stats = mem_stats(list(match.groups()))
50
result.append(stats.res_megs)
52
print 'no match!', line
56
def parse_test_id(test_id):
57
test_name, test_path = test_id.split(' ')
58
test_path = test_path.strip('(').strip(')')
59
return '%s.%s' % (test_path, test_name)
62
def parse_mem_stats(groups, stats, test_id):
63
result = mem_stats(groups)
65
app = test_id.split('.')[0]
66
stats.setdefault(app, [])
67
stats[app].append(result.res_megs)
73
with open(filename) as f:
76
memory_stats = OrderedDict()
77
last_test_id = 'init.init'
78
combined_stats = OrderedDict()
79
combined_stats[last_test_id] = []
82
if (not line or TEST_RESULT_ONLY.match(line) or
83
DOCSTRING_PLUS_RESULT.match(line)):
86
match = TEST_PLUS_MEM.match(line)
88
groups = list(match.groups())
89
last_test_id = parse_test_id(groups.pop(0))
90
stats = parse_mem_stats(groups, memory_stats, last_test_id)
91
# print 'TEST_PLUS_MEM', last_test_id, stats
92
assert last_test_id not in combined_stats
93
combined_stats[last_test_id] = [stats]
96
match = TEST_ONLY.match(line)
98
groups = filter(None, list(match.groups()))
99
last_test_id = parse_test_id(groups.pop(0))
100
# print 'TEST_ONLY', last_test_id
101
combined_stats[last_test_id] = []
104
match = TEST_DOCSTRING_PLUS_MEM.match(line)
106
groups = list(match.groups())
107
stats = parse_mem_stats(groups, memory_stats, last_test_id)
108
# print 'TEST_DOCSTRING_PLUS_MEM', stats
109
combined_stats[last_test_id].append(stats)
112
match = MEM_ONLY.match(line)
114
groups = list(match.groups())
115
stats = parse_mem_stats(groups, memory_stats, last_test_id)
116
# print 'MEM_ONLY', stats
117
combined_stats[last_test_id].append(stats)
120
###print '!!! not matched:', repr(line)
121
return memory_stats, combined_stats
124
def annotate(legend, x, y, color='white', alpha=0.5):
126
legend, (x, y), xytext=(-10, 10),
127
textcoords='offset points', ha='right', va='bottom',
128
bbox=dict(boxstyle='round,pad=0.5', fc=color, alpha=alpha),
129
arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')
133
def regular_vs_dummy():
134
filenames = ('fullrun-regular.txt', 'fullrun-dummy.txt')
135
for filename in filenames:
136
memory_stats, _ = parse(filename)
139
for app, stats in memory_stats.iteritems():
140
all_stats.extend(stats)
142
annotate(app, i, stats[0], alpha=1.0)
145
pyplot.plot(all_stats, label=filename.split('.')[0])
147
pyplot.ylabel('Resident memory (MB)')
148
pyplot.xlabel('ticks')
149
pyplot.legend(loc='upper left', shadow=True)
153
def dummy_annotated():
154
memory_stats , combined_stats = parse('fullrun-dummy.txt')
156
for stat in memory_stats.itervalues():
157
all_stats.extend(stat)
158
pyplot.plot(all_stats, '.-')
160
def _annotate(test, stats, color, extra=''):
161
legend = '%s\n%s' % tuple(test.split('.')[-2:])
162
y = stats[0].res_megs
163
x = all_stats.index(y)
164
annotate(legend + extra, x, y, color)
168
for test, stats in combined_stats.iteritems():
172
###if last_stats and stats[0][1] < last_stats[0][1]:
173
### # there was a drop in mem usage, let's anotate the test
174
### annotate(last_test, last_stats, color='yellow')
176
_annotate(test, stats, color='grey', extra=' (%s ticks)' % ticks)
181
pyplot.ylabel('Resident memory (MB)')
182
pyplot.xlabel('ticks')
186
def app_individuals():
187
filenames = [f for f in os.listdir('.') if f.startswith('memusage-tests')]
188
for filename in filenames:
189
stats = parse_individual(filename)
190
pyplot.plot(stats, label='-'.join(filename.split('-')[2:4]))
192
pyplot.ylabel('Resident memory (MB)')
193
pyplot.xlabel('ticks')
194
pyplot.legend(loc='upper left')
198
if __name__ == '__main__':