-
Notifications
You must be signed in to change notification settings - Fork 1
/
bw2-performance.py
executable file
·240 lines (201 loc) · 6.41 KB
/
bw2-performance.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib, os, re
import collections
import math
import pylab
from matplotlib import rc
from pylab import arange,pi,sin,cos,sqrt
if os.name is 'posix':
golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_width = 3 # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
params = {'backend': 'ps',
'axes.labelsize': 8,
'font.size': 8,
'legend.fontsize': 6,
'xtick.labelsize': 6,
'ytick.labelsize': 6,
'text.usetex': True,
'ps.usedistiller': 'xpdf',
'figure.figsize': fig_size}
pylab.rcParams.update(params)
rc('font',**{'family':'serif','serif':['Times']})
pylab.rcParams['path.simplify'] = True
import sys, getopt, random, time, datetime
import numpy as np
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from optparse import OptionParser
from matplotlib.ticker import ScalarFormatter
#from matplotlib2tikz import save
print 'matplotlib.__version__ is ' + matplotlib.__version__
class CommaFormatter(ScalarFormatter):
def pprint_val(self, x):
px = ScalarFormatter.pprint_val(self, x)
if os.name is 'posix':
px = px[1:len(px)-1]
px = self.add_commas(px)
if os.name is 'posix' and len(px) is not 0:
px = "$" + px + "$"
return px
def add_commas(self, arg):
if arg.find('e') is not -1:
return arg
s = arg.split('.')
if len(s) is 2 and s[1][0] is not '0':
return ""
if s[0][0] not in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}:
c = '-' + self.recurse(s[0][1:])
else:
c = self.recurse(s[0])
if len(s) > 1 and s[1] is not '0':
return c + '.' + s[1]
else:
return c
def recurse(self, arg):
if len(arg) < 4:
return arg
s = len(arg) - 3
return self.recurse(arg[:s]) + ',' + arg[s:]
class ExpFormatter(ScalarFormatter):
def pprint_val(self, x):
px = ScalarFormatter.pprint_val(self, x)
if os.name is 'posix':
px = px[1:len(px)-1]
px = self.add_commas(self.exp(px))
if os.name is 'posix' and len(px) is not 0:
px = "$" + px + "$"
return px
def add_commas(self, arg):
if arg.find('e') is not -1:
return arg
s = arg.split('.')
if len(s) is 2 and s[1][0] is not '0':
return ""
if s[0][0] not in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}:
c = '-' + self.recurse(s[0][1:])
else:
c = self.recurse(s[0])
if len(s) > 1 and s[1] is not '0':
return c + '.' + s[1]
else:
return c
def recurse(self, arg):
if len(arg) < 4:
return arg
s = len(arg) - 3
return self.recurse(arg[:s]) + ',' + arg[s:]
def exp(self, arg):
return str(math.pow(10, float(arg)) / 1000000.0)
def write_to_csv(filename, x_label, xs, y_labels, yss):
f = open(filename, 'w')
f.write(x_label.translate(None, ',\\'))
for y_label in y_labels:
f.write(',' + y_label.translate(None, ',\\'))
f.write('\n')
for i in range(len(xs)):
f.write(str(xs[i]))
for ys in yss:
f.write(',' + str(ys[i]))
f.write('\n')
def main():
min_x = float("inf")
max_x = float("-inf")
values = {}
for filename in sys.argv[1:]:
f = open(filename, 'r')
line = f.readline()
f.close()
directory = re.search('(^.*[^/]+)/+[^/]*$', filename).group(0)
name = re.search('([^/]+)/+[^/]*$', filename).group(1)
index = int(re.search('-([^-]+)\.txt$', filename).group(1))
time = math.log(float(re.search('= ([^ ]+) ', line).group(1)) * 10000.0, 10.0)
if index < min_x:
min_x = index
if index > max_x:
max_x = index
try:
values[name][index] = time
except KeyError:
values[name] = {}
values[name][index] = time
x = []
for i in range(min_x,max_x+1):
x.append(i)
fig = plt.figure()
fig.canvas.set_window_title('Blocks World Rete Performance')
pylab.axes([0.2,0.15,0.7725,0.82])
remap_names = collections.OrderedDict()
remap_names['dup-flu'] = 'Full Deoptimization'
remap_names['dup'] = 'Disabled Node Sharing'
remap_names['flu'] = 'Flushing WMEs'
remap_names['opt'] = 'Optimized'
labels = []
y_labels = []
yss = []
for agent, name in remap_names.items():
y = []
for index in x:
try:
y.append(values[agent][index])
except KeyError:
y.append(0)
y_labels.append(remap_names[agent])
yss.append(y)
color = 'black'
linestyle = ''
if agent is 'opt':
color = 'blue'
linestyle = '-'
elif agent is 'dup':
color = 'orange'
linestyle = '--'
elif agent is 'flu':
color = 'orange'
linestyle = ':'
elif agent is 'dup-flu':
color = 'red'
linestyle = '-.'
labels += pylab.plot(x, y, label=remap_names[agent], color=color, linestyle=linestyle)
pylab.grid(False)
pylab.xlabel('Number of Blocks', fontsize=8)
pylab.ylabel('CPU Time Per Step (s)', fontsize=8)
pylab.xlim(xmax=100)
# if len(sys.argv) > 1:
# pylab.ylim(ymin=-500, ymax=0)
pylab.ylim(ymin=0, ymax=10)
fig.axes[0].xaxis.set_major_formatter(CommaFormatter())
fig.axes[0].yaxis.set_major_formatter(ExpFormatter())
xlabels = fig.axes[0].xaxis.get_ticklabels()
last_xlabel = xlabels[len(xlabels) - 1]
last_xlabel.set_horizontalalignment('right')
last_xlabel.set_x(0)
#fig.axes[0].yaxis.set_scale('log')
#print last_xlabel.get_size()
#print last_xlabel.get_position()
#print last_xlabel.get_text()
#print last_xlabel
# lower right
pylab.legend(labels, [l.get_label() for l in labels], loc=4, handlelength=4.2, numpoints=2)
splitd = directory.rsplit('/', 2)
m = hashlib.md5()
for name in remap_names:
m.update(name)
filename = str(m.hexdigest())
if not os.path.exists(splitd[0] + '/csv'):
os.makedirs(splitd[0] + '/csv')
write_to_csv(splitd[0] + '/csv/' + filename + '.csv', 'Step Number', x, y_labels, yss)
if not os.path.exists(splitd[0] + '/eps'):
os.makedirs(splitd[0] + '/eps')
pylab.savefig(splitd[0] + '/eps/' + filename + '.eps')
if not os.path.exists(splitd[0] + '/png'):
os.makedirs(splitd[0] + '/png')
pylab.savefig(splitd[0] + '/png/' + filename + '.png', dpi=1200)
if not os.path.exists(splitd[0] + '/svg'):
os.makedirs(splitd[0] + '/svg')
pylab.savefig(splitd[0] + '/svg/' + filename + '.svg')
if __name__ == "__main__":
main()