-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostics.py
247 lines (229 loc) · 8.02 KB
/
diagnostics.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
# -*- coding: utf-8 -*-
"""
Created on Sun April 5 2015
@author: mattcook
"""
import matplotlib.pyplot as plt
import simplejson, os, itertools, re, sys, traceback, math, getopt
from scipy.stats import norm
from jsonschema import validate, ValidationError
import numpy as np
# edit this to change path
#os.chdir(r'/Users/username/path')
fileName = ""
# types of nodes on cluster data. Change to specify different types of nodes
nodeNames = ["burnupi", "mira", "plana"]
# max and min value. Any value above and below this will be removed. This is to remove faulty
# incorrectly logged values
threshold = 900.0
# range for histogram plot
histogramRange = None
# reasonable freqOffset range
#histogramRange = (-40, 40)
# reasonable latency range
#histogramRange = (0, 5)
usage = 'usage: diagnostics.py -i <inputfile> [-f | -t]'
nodeSchema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "/",
"type": "object",
"properties": {
"node": {
"id": "node",
"type": "string"
},
"entries": {
"id": "entries",
"type": "array",
"items": {
"id": "1",
"type": "object",
"properties": {
"date": {
"id": "date",
"type": "integer"
},
"time": {
"id": "time",
"type": "number"
}
},
}
}
},
"required": [
"node",
"entries"
]
}
def main(argv):
graphType = ""
col_heads = []
graphLabel = ""
try:
opts, args = getopt.getopt(argv,"hi:o:tf",["ifile=", "--help", "--freqOffset", "--timestamps"])
except getopt.GetoptError:
print usage
sys.exit(2)
for opt, arg in opts:
if opt == "-h" or opt == "--help":
print usage
sys.exit()
elif opt in ("-i", "--ifile"):
fileName = arg
elif opt in ("-f", "--freqOffset"):
graphType = "freqOffset"
graphLabel = "Frequency Offset (PPM)"
col_heads =['date', 'time', 'freqOffset']
# update schema with properties and make them required
nodeSchema["properties"]["entries"]["items"]["properties"].update( {
"freqOffset": {
"id": "freqOffset",
"type": "number"
}})
nodeSchema["properties"]["entries"]["items"].update(
{"required" : ["date", "time", "freqOffset"]})
elif opt in ("-t", "--timestamps"):
graphType = "latency"
graphLabel = "Latency (ms)"
col_heads = ['date', 'time', 'originTS', 'receiveTS', 'transmitTS', 'destTS']
nodeSchema["properties"]["entries"]["items"]["properties"].update( {
"originTS": {
"id": "originTS",
"type": "number"
},
"receiveTS": {
"id": "receiveTS",
"type": "number"
},
"transmitTS": {
"id": "transmitTS",
"type": "number"
},
"destTS": {
"id": "destTS",
"type": "number"
}})
nodeSchema["properties"]["entries"]["items"].update({"required" :
["date", "time", "originTS", "receiveTS", "transmitTS", "destTS"]})
# make sure graph type is specified
if graphType == "" or fileName == "":
print "Error: Arguments not complete."
print usage
sys.exit(0)
json_data = open(fileName)
data = simplejson.load(json_data)
figure_num = 1
axes = plt.gca()
numNodes = [0 for x in xrange(len(nodeNames))]
# list of lists for each cluster
dataSet = [{} for x in xrange(len(nodeNames))]
f = lambda c: [c[col] for col in col_heads]
# can change data to limit which nodes to plot, i.e. data[:55] will get first 55 nodes
for node in data:
try:
validate(node, nodeSchema)
row_wise = [col_heads[:]]
row_wise.extend([f(data_point) for data_point in node['entries']])
col_wise = zip(*row_wise)
plt.figure(figure_num)
newDate = [x * 86400 for x in col_wise[0][1:]]
values = []
if graphType == "latency":
entireRoundTrip = [a - b for a, b in zip(col_wise[5][1:], col_wise[2][1:])]
timeAtServer = [a - b for a, b in zip(col_wise[4][1:], col_wise[3][1:])]
# round trip time in milliseconds
values = [(a - b) * 1000.0 for a, b in zip(entireRoundTrip, timeAtServer)]
elif graphType == "freqOffset":
values = list(col_wise[2][1:])
times = [sum(x) for x in zip(newDate, col_wise[1][1:])]
# keep track of non-faulty values
truetimes = []
truevalues = []
for time, value in itertools.izip(times,values):
if time < 0 or time > 4929100000.0:
# deleting incorrect or padded entry
continue
elif value > abs(threshold):
# deleting bad values
print "data spike: " + str(value) + " on node: " + node['node']
else:
truetimes.append(time)
truevalues.append(value)
times = truetimes
values = truevalues
# check which cluster this data belongs to, plot to respective graph
for nodeNum in range(len(nodeNames)):
if nodeNames[nodeNum] in node['node']:
plt.figure(nodeNum+1)
numNodes[nodeNum] += 1
dataSet[nodeNum][node['node']] = values
plt.scatter(times, values)
except ValidationError as e:
print "Error on node: " + str(node['node'])
# label all plots
for i in range(len(nodeNames)):
# figures start from 1
plt.figure(i+1)
plt.xlabel('Time (s)')
plt.ylabel(graphLabel)
plt.title('Time vs %s Across %d %s Nodes' % (graphType, numNodes[i], nodeNames[i]))
# name of scatterplot graph is here
plt.savefig(graphType + "scatter" + str(i))
overallValues = []
overallStdDevs = []
# make next three histogram plots based on data on each cluster
for i in range(len(nodeNames)):
# need to offset by the figures for previous scatterplots
plt.figure(i+1+len(nodeNames))
# skip if no data for given node type
if len(dataSet[i].items()) == 0:
continue
flattened = []
stdDevs = []
for (key, values) in dataSet[i].items():
# if we have a node with empty data, skip
if len(values) == 0:
continue
(muNode, sigmaNode) = norm.fit(values)
if math.isnan(sigmaNode):
print values
stdDevs.append(sigmaNode)
# uncomment to print stats of each node
#print "node: " + key + " has mean: " + str(muNode) + " and stdDev: " + str(sigmaNode)
flattened += values
overallValues += flattened
# best fit of data
(mu, sigma) = norm.fit(flattened)
median = np.median(flattened)
print nodeNames[i] + " average standard deviation: " + str(sum(stdDevs)/float(len(stdDevs)))
overallStdDevs += stdDevs
if histogramRange != None:
entries, bin_edges, patches = plt.hist(flattened, bins = 100, facecolor = 'green', range = histogramRange)
else:
entries, bin_edges, patches = plt.hist(flattened, bins = 100, facecolor = 'green')
axes = plt.gca()
plt.xlabel(graphLabel)
plt.ylabel('Packet Count')
plt.title("Histogram of %s across %d %s nodes" % (graphLabel, numNodes[i], nodeNames[i]))
print "Histogram of %s for %s nodes: mu=%.3f, sigma=%.3f, median=%.3f" %(graphType, nodeNames[i], mu, sigma, median)
# name of histogram graph is here
plt.savefig(graphType + "histogram" + str(i))
print "Overall Standard Dev across " + str(len(overallStdDevs)) + " nodes is " + str(sum(overallStdDevs)/float(len(overallStdDevs)))
plt.figure((len(nodeNames)*2)+1)
plt.xlabel(graphLabel)
plt.ylabel('Packet Count')
if histogramRange != None:
entries, bin_edges, patches = plt.hist(overallValues, bins = 100, facecolor = 'green', range = histogramRange)
else:
entries, bin_edges, patches = plt.hist(flattened, bins = 100, facecolor = 'green')
totalNumNodes = sum(numNodes)
(mu, sigma) = norm.fit(overallValues)
median = np.median(overallValues)
plt.title("Histogram of %s across %d nodes" % (graphLabel, totalNumNodes))
print "Histogram of %s across %d nodes: mu=%.3f, sigma=%.3f, median=%.3f" %(graphType, totalNumNodes, mu, sigma, median)
plt.savefig(graphType + "histogramtotal")
plt.show()
json_data.close()
if __name__ =='__main__':
main(sys.argv[1:])