1
2
3
4 import re
5 import time
6 import sys
7 import corestats
8 from pylab import *
9
10
11
12
13 parse_time_re = re.compile(
14 '([0-9]{4})-([0-9]{2})-([0-9]{2}) '
15 '([0-9]{2}):([0-9]{2}):([0-9]{2})')
16
17
18 -def parse_time(timestamp, time_format='timestamp'):
19
20 if time_format == 'epoch':
21 epoch = timestamp
22 elif time_format == 'timestamp':
23
24 try:
25 matches = parse_time_re.match(timestamp)
26 epoch = time.mktime(
27 (int(matches.group(1)),
28 int(matches.group(2)),
29 int(matches.group(3)),
30 int(matches.group(4)),
31 int(matches.group(5)),
32 int(matches.group(6)),
33 0, 0, -1))
34 except AttributeError:
35 sys.stderr.write('ERROR: bad time value: %s\n'% timestamp)
36 sys.exit(1)
37 else:
38
39 sys.stderr.write('ERROR: bad time_format supplied to time_series.parse_time() \n')
40 sys.exit(1)
41 return int(epoch)
42
43
44
45
47
48 - def __init__(self, interval, data_set, time_format='timestamp'):
49
50
51
52 self.interval = interval
53
54
55 self.datapoints = [re.split(',', row) for row in data_set]
56
57
58 self.starttime = self.datapoints[0][0]
59 self.finishtime = self.datapoints[len(self.datapoints) - 1][0]
60
61
62 self.offset = parse_time(self.datapoints[0][0], time_format)
63
64 self.timespan = parse_time(self.datapoints[len(self.datapoints) - 1][0], time_format) - self.offset
65
66
67 self.datapoints = [[parse_time(self.datapoints[i][0], time_format) - self.offset, float(self.datapoints[i][1])]
68 for i in range(len(self.datapoints))]
69
70
71 self.length = (len(self.datapoints))
72
73
74
75 self.series = []
76 current_marker = 0
77 next_marker = self.interval
78
79 while next_marker < self.timespan:
80
81 self.series.append([timeval[1] for timeval in self.datapoints if current_marker <= timeval[0] < next_marker])
82 current_marker = next_marker
83 next_marker += self.interval
84
85
86 self.calced_series = []
87
88
89 self.screen_output = True
90 self.image_output = True
91 self.graph_type = 'LINE'
92 self.output_name = 'timeseries.png'
93 self.graph_xlabel = 'Elapsed Time (s)'
94 self.graph_ylabel = 'Value'
95 self.graph_title = ''
96
97
98
100
101 for row in self.datapoints:
102 print row
103
104
106
107 for slot in self.series:
108 print slot
109
110
112
113 for slot in self.calced_series:
114 print slot
115
116
118 calced_series = []
119 timeslots = []
120 marker = 0
121 for slot in self.series:
122 if slot:
123 if calc_type == 'SUM':
124 stats = corestats.Stats(slot)
125 calced_series.append(stats.sum())
126 if calc_type == 'COUNT':
127 stats = corestats.Stats(slot)
128 calced_series.append(stats.count())
129 if calc_type == 'COUNT_PERSEC':
130 stats = corestats.Stats(slot)
131 calced_series.append(stats.count() / self.interval)
132 if calc_type == 'MIN':
133 stats = corestats.Stats(slot)
134 calced_series.append(stats.min())
135 if calc_type == 'MAX':
136 stats = corestats.Stats(slot)
137 calced_series.append(stats.max())
138 if calc_type == 'AVG':
139 stats = corestats.Stats(slot)
140 calced_series.append(stats.avg())
141 if calc_type == 'MEDIAN':
142 stats = corestats.Stats(slot)
143 calced_series.append(stats.median())
144 if calc_type == 'STDEV':
145 stats = corestats.Stats(slot)
146 calced_series.append(stats.stdev())
147 if calc_type == 'PERCENT50':
148 stats = corestats.Stats(slot)
149 calced_series.append(stats.percentile(50))
150 if calc_type == 'PERCENT90':
151 stats = corestats.Stats(slot)
152 calced_series.append(stats.percentile(90))
153 if calc_type == 'PERCENT95':
154 calced_series.append(stats.percentile(95))
155 if calc_type == 'PERCENT99':
156 calced_series.append(stats.percentile(99))
157 else:
158 calced_series.append(0)
159 marker += self.interval
160 self.calced_series = calced_series
161 return self.calced_series
162
163
165 self.screen_output = True
166 self.image_output = False
167 self.__graph_all()
168
169
171 self.screen_output = False
172 self.image_output = True
173 self.__graph_all()
174
175
177 sequence = [item[1] for item in self.datapoints]
178 timeslots = [item[0] for item in self.datapoints]
179 self.__graph(timeslots, sequence)
180
181
183 self.screen_output = True
184 self.image_output = False
185 self.__graph_series()
186
187
189 self.screen_output = False
190 self.image_output = True
191 self.__graph_series()
192
193
195 if len(self.calced_series) < 1:
196 sys.stderr.write('ERROR: there is no calculated series to graph. create one first.\n')
197 sys.exit(1)
198 sequence = self.calced_series
199
200 timeslots = []
201 marker = 0
202 for slot in self.series:
203 timeslots.append(marker)
204 marker += self.interval
205 self.__graph(timeslots, sequence)
206
207
208 - def __graph(self, timeslots, sequence):
209 xlabel(self.graph_xlabel)
210 ylabel(self.graph_ylabel)
211 title(self.graph_title)
212 if self.graph_type == 'LINE':
213 lines = plot(timeslots, sequence, 'r-')
214 setp(lines, color='r', linewidth=.5)
215 elif self.graph_type == 'SCATTER':
216 lines = plot(timeslots, sequence, 'r,')
217 else:
218 sys.stderr.write('ERROR: invalid graph type.\n')
219 sys.exit(1)
220 grid(True)
221 if self.image_output:
222 savefig(self.output_name)
223 if self.screen_output:
224 show()
225