-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchronicreplay.py
executable file
·375 lines (189 loc) · 7.44 KB
/
chronicreplay.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 6 10:24:07 2020
@author: TeSolva
"""
import numpy as np
import pandas as pd
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import os
import re
#%%
def my_int(my_string):
try:
out = int(my_string)
except:
out = -9999
return out
#%%
def func_nummer(x):
temp = re.findall(r"[\w']+", x)
my_list=list(map(my_int, temp))
temp = np.array(my_list)[np.array(my_list)>-9999]
if len(temp) > 1:
out = temp[0]
else:
out = False
return out
#%%
def func_id(x):
temp = re.findall(r"[\w']+", x)
my_list=list(map(my_int, temp))
temp = np.array(my_list)[np.array(my_list)>-9999]
if len(temp) > 1:
out = temp[1]
else:
out = False
return out
#%%
def outlier_1d_mad_based(sample, thresh=3.5):
"""
outlier_1d_mad_based(sample, thresh=3.5)
routine to analyse a given 1d data sample to check for outliers.
see reference for more details on the background of the used algorithm.
the function returns a boolean array with True if a value in the sample
is an outliers and False otherwise.
Parameters:
-----------
sample : array_like
An numobservations by numdimensions array of observations
thresh : float
The modified z-score to use as a threshold. Observations with
a modified z-score (based on the median absolute deviation) greater
than this value will be classified as outliers.
Returns:
--------
A numobservations-length boolean array.
Examples
--------
# Generate some data
sample = np.random.normal(0, 0.5, 50)
# Add three outliers...
sample = np.r_[sample, -3, -10, 12]
# call function and check for outliers
out = outlier_1d_mad_based(sample)
References:
----------
Boris Iglewicz and David Hoaglin (1993), "Volume 16: How to Detect and
Handle Outliers", The ASQC Basic References in Quality Control:
Statistical Techniques, Edward F. Mykytka, Ph.D., Editor.
"""
if len(sample.shape) == 1:
sample = sample[:, None]
median = np.median(sample, axis=0)
diff = np.sum((sample - median) ** 2, axis=-1)
diff = np.sqrt(diff)
med_abs_deviation = np.median(diff)
modified_z_score = 0.6745 * diff / med_abs_deviation
return modified_z_score > thresh
#%%
# #path = "PATH\"
# # define all filenames in current directory
# path = os.getcwd() # path to this file here
# list_dir = os.listdir(os.getcwd()) # all filenames in the directory
# file_set = []
# for i in list_dir:
# if i.endswith(".csv"):
# file_set.append(i)
#%%
# define file name
file1 = "chronicreplay-audiothek-appapi.tesolva.dev_2020-08-08_07-29-08.csv"
file2 = "chronicreplay-audiothek-appapi.solr.tesolva.dev_2020-08-11_08-47-32.csv"
#
file_set = [file1, file2]
###############################################################################
#%% pre-process
X_file = file_set[1]
filename = os.path.splitext(X_file)[0]
path = os.getcwd()
outputPath = path + '/' + filename + '/' + filename
os.makedirs(filename, exist_ok=True)
df = pd.read_csv(X_file,sep='\t')
df.StartTime = pd.to_datetime(df.StartTime)
# create new columns
df['Request_No']=df.Request
df['Request_ID']=df.Request
# add values (No and ID) from URL to new columns
df.Request_No = df.Request_No.apply(func_nummer)
df.Request_ID = df.Request_ID.apply(func_id)
# Requests without /health and /metric
df = df[df['Request_No'] != False]
#%%
# Threshold to find URLs with high values of request time
THLD = 1000 # Threshold
df['Duration_greaterThan_1000'] = (df.Duration>THLD).astype(int)
#%%
plt.figure(figsize = (8.5,11))
myFmt = mdates.DateFormatter('%H:%M')
plt.gca().xaxis.set_major_formatter(myFmt)
plt.scatter(df.StartTime[df.Duration<THLD].values, df.Duration[df.Duration<THLD].values, marker='^', label='Duration < THLD [ms]',
alpha=0.3, edgecolors='none')
plt.scatter(df.StartTime[df.Duration>THLD].values, df.Duration[df.Duration>THLD].values, marker='o', label='Duration > THLD [ms]',
alpha=0.3, edgecolors='none')
# Show the boundary between the regions:
plt.plot(df.StartTime.values, np.ones(len(df))*THLD,'-.k')
plt.legend()
plt.ylabel("Duration of request time (ms)")
plt.xlabel("Time")
plt.title("file:" + X_file)
plt.axis([None, None, 0, THLD*2])
plt.savefig(outputPath + '-Duration.png')
#%%
plt.figure(figsize = (8.5,11))
myFmt = mdates.DateFormatter('%H:%M')
plt.gca().xaxis.set_major_formatter(myFmt)
plt.scatter(df.StartTime[df.Duration<THLD].values, df.Difference[df.Duration<THLD].values, marker='^', label='Duration < THLD [ms]',
alpha=0.3, edgecolors='none')
plt.scatter(df.StartTime[df.Duration>THLD].values, df.Difference[df.Duration>THLD].values, marker='o', label='Duration > THLD [ms]',
alpha=0.3, edgecolors='none')
# Show the boundary between the regions:
plt.plot(df.StartTime.values, np.ones(len(df))*THLD,'-.k')
plt.legend()
plt.ylabel("Difference of request time (ms)")
plt.xlabel("Time")
plt.title("file:" + X_file)
plt.axis([None, None, 0, THLD*2])
plt.savefig(outputPath + '-Difference.png')
#%%
# only interested in URLs with request time greater than THLD
df_Upper = df[df.Duration>THLD]
df_Upper['Percent'] = len(df_Upper)/len(df.index)*100
df_Upper['Mean'] = df_Upper["Duration"].mean()
# export csv files
df_Upper[['Duration', 'Request_No', 'Request_ID', 'Percent','Mean', 'Request']].to_csv(outputPath + '-URLs-request-time-Upper.csv', index=False)
#%%
# count Request_No for Requests time > THLD
df_temp = (df_Upper.drop_duplicates().Request_No.value_counts())
df_Counts = pd.DataFrame({'Request_No':df_temp.index, 'Counts':df_temp.values})
df_Counts.to_csv(outputPath + '-RequestsNo-Counts-Upper.csv', index=False)
#%%
# values between 500 and 1000 ms
df_Between = df[df['Duration'].between(500, 1000, inclusive=True)]
# export csv files
df_Between['Percent'] = len(df_Between)/len(df.index)*100
df_Between['Mean'] = df_Between["Duration"].mean()
df_Between[['Duration', 'Request_No', 'Request_ID', 'Percent','Mean', 'Request']].to_csv(outputPath + '-URLs-request-time-Between.csv', index=False)
# count Request_No for Requests time 500 < time < 1000
df_temp2 = (df_Between.drop_duplicates().Request_No.value_counts())
df_CountsBetween = pd.DataFrame({'RequestNo':df_temp2.index, 'Counts':df_temp2.values})
df_CountsBetween.to_csv(outputPath + '-RequestsNo-Counts-Between.csv', index=False)
#%%
# values lower than 500 ms
df_Lower = df[df['Duration'].between(0, 500, inclusive=False)]
# export csv files
df_Lower['Percent'] = len(df_Lower)/len(df.index)*100
df_Lower['Mean'] = df_Lower["Duration"].mean()
df_Lower[['Duration', 'Request_No', 'Request_ID', 'Percent','Mean', 'Request']].to_csv(outputPath + '-URLs-request-time-Lower.csv', index=False)
# count Request_No for Requests time < 500
df_temp2 = (df_Lower.drop_duplicates().Request_No.value_counts())
df_CountsLower = pd.DataFrame({'RequestNo':df_temp2.index, 'Counts':df_temp2.values})
df_CountsLower.to_csv(outputPath + '-RequestsNo-Counts-Lower.csv', index=False)
#%%
# df_notSameStatus = df[df.SameStatus==False]
# # export csv files
# df_notSameStatus.to_csv(outputPath + '-URLs-wo-same-status.csv', index=False)
#%%
del(X_file,file1, file2, file_set) # delete the variable from workspace
#%%