blob: 893c0fc73d28c38f556edf8dd8dd658726352060 [file] [log] [blame]
Ved-vampir0d0740c2015-04-06 15:39:33 +03001import sys
2import time
3
4from copy import deepcopy
5
6import numpy
7import scipy.optimize as scp
8import matplotlib.pyplot as plt
9
10import io_py_result_processor as io_test
11
12key_pos = {'blocksize': 0, 'direct_io': 1, 'name': 2}
13actions = ['randwrite', 'randread', 'read', 'write']
14types = ['s', 'd']
15colors = ['red', 'green', 'blue', 'cyan',
16 'magenta', 'black', 'yellow', 'burlywood']
17
18def get_key(x, no):
19 """ x = (), no = key_pos key """
20 keys = deepcopy(key_pos)
21 del keys[no]
22 key = [x[n] for n in keys.values()]
23 return tuple(key), x[key_pos[no]]
24
25
26def generate_groups(data, group_id):
27 """ select data for plot by group_id
28 data - processed_series"""
29 grouped = {}
30
31 for key, val in data.items():
32 new_key, group_val = get_key(key, group_id)
33 group = grouped.setdefault(new_key, {})
34 group[group_val] = val
35
36 return grouped
37
38
39def gen_dots(val):
40 """Generate dots from real data
41 val = dict (x:y)
42 return ox, oy lists """
43 oy = []
44 ox = []
45 for x in sorted(val.keys()):
46 ox.append(int(x[:-1]))
47 if val[x][0] != 0:
48 oy.append(1.0/val[x][0])
49 else:
50 oy.append(0)
51 return ox, oy
52
53
54def gen_line_numpy(x, y):
55 A = numpy.vstack([x, numpy.ones(len(x))]).T
56 coef = numpy.linalg.lstsq(A, y)[0]
57 funcLine = lambda tpl, x: tpl[0] * x + tpl[1]
58 print coef
59 return x, funcLine(coef, x)
60
61
62def gen_line_scipy(x, y):
63 funcLine = lambda tpl, x: tpl[0] * x + tpl[1]
64 ErrorFunc = lambda tpl, x, y: 1.0 - y/funcLine(tpl, x)
65 tplInitial = (1.0, 0.0)
66 # print x, y
67 tplFinal, success = scp.leastsq(ErrorFunc, tplInitial[:], args=(x, y),
68 diag=(1./x.mean(), 1./y.mean()))
69 if success not in range(1, 4):
70 raise ValueError("No line for this dots")
71 xx = numpy.linspace(x.min(), x.max(), 50)
72 print tplFinal
73 # print x, ErrorFunc(tplFinal, x, y)
74 return xx, funcLine(tplFinal, xx)
75
76
77def gen_app_plot(key, val, plot, color):
78 """ Plots with fake line and real dots around"""
79 ox, oy = gen_dots(val)
80 name = "_".join(str(k) for k in key)
81 if len(ox) < 2:
82 # skip single dots
83 return False
84 # create approximation
85 x = numpy.array(ox)#numpy.log(ox))
86 y = numpy.array(oy)#numpy.log(oy))
87 print x, y
88 try:
89 print name
90 x1, y1 = gen_line_scipy(x, y)
91 plot.plot(x1, y1, color=color)
92 #
93 #plot.loglog(x1, y1, color=color)
94 except ValueError:
95 # just don't draw it - it's ok
96 # we'll see no appr and bad dots
97 # not return False, because we need see dots
98 pass
99 plot.plot(x, y, '^', label=name, markersize=7, color=color)
100 #plot.loglog(x, y, '^', label=name, markersize=7, color=color)
101 return True
102
103
104def save_plot(key, val):
105 """ one plot from one dict item with value list"""
106 ox, oy = gen_dots(val)
107 name = "_".join(str(k) for k in key)
108 plt.plot(ox, oy, label=name)
109
110
111def plot_generation(fname, group_by):
112 """ plots for value group_by in imgs by actions"""
113 data = list(io_test.load_io_py_file(fname))
114 item = io_test.Data("hdr")
115 for key, vals in io_test.groupby_globally(data, io_test.key_func).items():
116 item.series[key] = [val['iops'] for val in vals]
117 io_test.process_inplace(item)
118
119 pr_data = generate_groups(item.processed_series, group_by)
120 print pr_data
121
122 #fig = plt.figure()
123 plot = plt.subplot(111)
124
125 for action in actions:
126 for tp in types:
127 color = 0
128 hasPlot = False
129 for key, val in pr_data.items():
130 if action in key and tp in key:
131 ok = gen_app_plot(key, val, plot, colors[color])
132 hasPlot = hasPlot or ok
133 color += 1
134 # use it for just connect dots
135 #save_plot(key, val)
136 if hasPlot:
137 # Shrink current axis by 10%
138 box = plot.get_position()
139 plot.set_position([box.x0, box.y0 + box.height * 0.1,
140 box.width, box.height * 0.9])
141
142 # Put a legend to the bottom
143 plot.legend(loc='lower center', bbox_to_anchor=(0.5, -0.25),
144 fancybox=True, shadow=True, ncol=4,
145 fontsize='xx-small')
146 plt.title("Plot for %s on %s" % (group_by, action))
147 plt.ylabel("time")
148 plt.xlabel(group_by)
149 plt.grid()
150 # use it if want scale plot somehow
151 # plt.axis([0.0, 5000.0, 0.0, 64.0])
152 name = "%s__%s_%s.png" % (group_by, action, tp)
153 plt.savefig(name, format='png', dpi=100)
154 plt.clf()
155 plot = plt.subplot(111)
156 color = 0
157
158
159def deviation_on_deviation(groups_list, data):
160 """ calc deviation of data all and by selection groups"""
161 total_dev = io_test.round_deviation(io_test.med_dev(data))
162 grouped_dev = [total_dev]
163 for group in groups_list:
164 beg = 0
165 end = group
166 local_dev = []
167 while end <= len(data):
168 local_dev.append(io_test.round_deviation(io_test.med_dev(data[beg:end]))[0])
169 beg += group
170 end += group
171 grouped_dev.append(io_test.round_deviation(io_test.med_dev(local_dev)))
172 return grouped_dev
173
174
175
176def deviation_generation(fname, groups_list):
177 """ Print deviation by groups for data from fname """
178 CONC_POS = key_pos['concurence']
179 int_list = [int(i) for i in groups_list]
180 data = list(io_test.load_io_py_file(fname))
181 item = io_test.Data("hdr")
182 for key, vals in io_test.groupby_globally(data, io_test.key_func).items():
183 item.series[key] = [val['iops'] * key[CONC_POS] for val in vals]
184 print deviation_on_deviation(int_list, item.series[key])
185
186
187def main(argv):
188 if argv[1] == "plot":
189 plot_generation(argv[2], argv[3])
190 elif argv[1] == "dev":
191 deviation_generation(argv[2], argv[3:])
192
193
194if __name__ == "__main__":
195 exit(main(sys.argv))
196
197
198
199