-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis.py
More file actions
executable file
·1388 lines (1026 loc) · 50.6 KB
/
Copy pathanalysis.py
File metadata and controls
executable file
·1388 lines (1026 loc) · 50.6 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
from pymongo import MongoClient
from scipy import stats
import scipy
import numpy
import math
import sys
import argparse
import collections
import re
import os
from rpy2.robjects.packages import importr
import rpy2.robjects as ro
import rpy2
parser = argparse.ArgumentParser(description='Analyze test logs.')
parser.add_argument('--sortByFail', help='Sort the test cases by the number of failures.', action='store_true', required=False )
parser.add_argument('--getDuration', help='Get test execution time (in sec) across all tests.', action='store_true', required=False )
parser.add_argument('--getPassRate', help='Get test case pass rate for each build.', action='store_true', required=False )
parser.add_argument('--getBuildQuality', help='Get # of test pass/fail for each build.', action='store_true', required=False )
parser.add_argument('--prioritizeTests', help='Prioritize tests based on history. Tests that are likely to pass should be run first (they will be surprises if they fail).', action='store_true', required=False )
parser.add_argument('--support', help='Used for determining the support rate for prioritizing tests.', type=float, default=0.8, required=False )
parser.add_argument('--checkNewTestPerf', help='Check the performance of the newest test, using the execution time of prev 10 test runs (outlier removed). Returns the number of standard deviation away from previous runs.', action='store_true', required=False)
parser.add_argument('--checkNewTestPerfARIMA', help='Check the performance of the newest test using ARIMA model', action='store_true', required=False)
parser.add_argument('--perfTrend', help='Check the performance trend of each test.', action='store_true', required=False)
parser.add_argument('--testDeviation', help='Find tests with high deviations in execution time (unstable).', action='store_true', required=False )
parser.add_argument('--testStepDeviation', help='Input takes in a test id. Find steps in a test with high deviations in execution time (unstable).', type=str, required=False )
parser.add_argument('--testDeviationEntropy', help='Find tests with high deviations in execution time (unstable).', action='store_true', required=False )
parser.add_argument('--arimaModel', help='Examine ARIMA model (p, i, q, and drift), and execution time distribution.', action='store_true', required=False)
parser.add_argument('--examineAllHistory', help='Use 10 ... n points to build n - 10 ARIMA models and look for outliers in all historical builds.', action='store_true', required=False)
parser.add_argument('--compare', help='Compare two tests. Pass in two database names', type=str, required=False, nargs='+')
parser.add_argument('--windowSize', help='Number of most recent tests to consider when computing testDeviation', type=int, required=False, default=15)
parser.add_argument('--dbName', help='Name of the database which stores the test results', type=str, required=True)
parser.add_argument('--dst', help='Output folder for storing the graphs generated by the tool.', type=str, required=False, default="plots")
parser.add_argument('--test', help='Testing new features', action='store_true', required=False)
args = parser.parse_args()
conn = MongoClient()
db = conn[args.dbName]
# constants
PASS = 0
FAIL = 1
TIMEDOUT = 2
'''
R code for analysis
col.l <- colorRampPalette(c('green', 'yellow', 'orange', 'red'))(30)
levelplot(t(as.matrix(dat)), aspect="filled", col.regions = col.l)
'''
#d = dat[10,][!is.na(dat[10,])]
#plot(qcc(d, type="xbar.one"))
#################################################################
'''helper method for flatten an iterable'''
def flatten(iterable):
results = []
for i in iterable:
if isinstance(i, collections.Iterable) and not isinstance(i, basestring):
results.extend(flatten(i))
else:
results.append(i)
return results
#################################################################
#################################################################
'''for sorting build numbers'''
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split('(\d+)', text) ]
#################################################################
"""
Get the execution time of each test in each build (averaged across all runs)
R code for visualization:
dat = read.csv("testDuration.csv", row.names=1)
col.l <- colorRampPalette(c('green', 'yellow', 'orange', 'red'))(30)
levelplot(as.matrix(dat), aspect="fill", , col.regions = col.l)
"""
def findTestDuration():
data = db.testStat.find({}, {'buildNumber':1})
buildNumbers = list()
for d in data:
buildNumbers += d["buildNumber"]
buildNumbers = sorted(list(set(buildNumbers)))
testData = db.testStat.find({}, {'buildNumber': 1, 'duration': 1, 'passed': 1})
buildTestDurations = {}
''' set up R code '''
plot = importr('grDevices')
graphics = importr('graphics')
lattice = importr('lattice')
ro.r('''
plotExecutionTime <- function(fname) {
require(lattice)
dat = read.csv(fname, row.names=1)
col.l <- colorRampPalette(c('green', 'yellow', 'orange', 'red'))(30)
print(levelplot(as.matrix(dat), aspect="fill", col.regions = col.l))
}
''')
rPlotTime = ro.globalenv['plotExecutionTime']
output = open('getDuration.csv', 'w')
# change to folder which stores plots
os.chdir(args.dst)
allTestIDs = []
for d in testData:
testInfo = zip(d['buildNumber'], d['duration'], d['passed'])
tid = d['_id']
allTestIDs.append(tid)
for build, duration, passed in testInfo:
if build not in buildTestDurations:
buildTestDurations[build] = {}
if tid not in buildTestDurations[build]:
buildTestDurations[build][tid] = []
if passed:
buildTestDurations[build][tid].append(duration)
if passed == False: # test failed, so give it a NA
buildTestDurations[build][tid].append(numpy.nan)
output.write("build,"+",".join(allTestIDs)+"\n")
allTestIDs = list(set(allTestIDs))
builds = sorted(buildTestDurations.keys(), key = lambda x: natural_keys(x))
#print "build,"+",".join(allTestIDs)
count = 0
for build in builds:
testDurations = []
for tid in allTestIDs:
if tid in buildTestDurations[build]:
time = numpy.array(buildTestDurations[build][tid])
duration = time[~numpy.isnan(time)].mean()
#duration = numpy.mean(buildTestDurations[build][tid])
testDurations.append(duration)
else: # the test was not available, so give it a NA
testDurations.append(numpy.nan)
#print build + "," + ",".join(map(str, testDurations)) # cast pass rate to string for printing
count += 1
output.write(build + "," + ",".join(map(str, testDurations)) + "\n") # cast pass rate to string for printing
plot.pdf("getDuration.pdf")
rPlotTime('../getDuration.csv')
plot.dev_off()
"""
Sort the test cases by the number of times they fail
"""
def sortTestFail():
#data = db.test.find({'duration' : {'$gt':0}}, {'duration' : 1, 'testName' : 0, 'testNumber': 1, '_id' : 0, 'testID': 1})
tests = db.testStat.find({}, {'passed': 1, 'testName': 1})
result = []
testResultForBuild = {}
numFailPerTest = []
for test in tests:
tid = test['_id']
totalFailed = len(test['passed']) - sum(test['passed'])
numFailPerTest.append((test['testName'], tid, totalFailed))
for n, t, c in sorted(numFailPerTest, key=lambda x: x[2], reverse=True):
print n, t, c
"""
Get the pass rate for each test case in each build
"""
def getPassRate():
data = db.testStat.find({}, {'buildNumber':1})
buildNumbers = list()
# get unique build numbers
for d in data:
buildNumbers += d["buildNumber"]
buildNumbers = list(set(buildNumbers))
buildNumbers = sorted(buildNumbers)
testData = db.testStat.find({}, {'buildNumber': 1, 'passed':1})
buildTestQuality = {}
allTestIDs = []
for d in testData:
buildQuality = zip(d['buildNumber'], d['passed'])
tid = d['_id']
allTestIDs.append(tid)
for build, passed in buildQuality:
if build not in buildTestQuality:
buildTestQuality[build] = {}
if tid not in buildTestQuality[build]:
buildTestQuality[build][tid] = [0, 0] # test pass/fail
if passed:
buildTestQuality[build][tid][PASS] += 1
else:
buildTestQuality[build][tid][FAIL] += 1
allTestIDs = list(set(allTestIDs)) # remove duplicate test ids
builds = sorted(buildTestQuality.keys(), key = lambda x: natural_keys(x))
print "build,"+",".join(allTestIDs)
for build in builds:
passRate = []
for tid in allTestIDs:
if tid in buildTestQuality[build]:
quality = buildTestQuality[build][tid]
passRate.append(float(quality[PASS])/(quality[PASS]+quality[FAIL]))
else:
passRate.append('NA')
print build + "," + ",".join(map(str, passRate)) # cast pass rate to string for printing
"""
Get the pass rate, # of passes, and # of fails for each build
"""
def getBuildQuality():
#data = db.test.find({'duration' : {'$gt':0}}, {'duration' : 1, 'testName' : 0, 'testNumber': 1, '_id' : 0, 'testID': 1})
data = db.testStat.find({}, {'buildNumber':1})
buildNumbers = list()
# get unique build numbers
for d in data:
buildNumbers += d["buildNumber"]
buildNumbers = list(set(buildNumbers))
buildNumbers = sorted(buildNumbers, key = lambda x: natural_keys(x))
testData = db.testStat.find({}, {'buildNumber': 1, 'passed': 1, 'timedout': 1})
result = {}
for d in testData:
tmp = zip(d['buildNumber'], d['passed'], d['timedout'])
tid = d['_id']
test = {}
for build, passed, timedout in tmp:
if build not in result:
result[build] = [0, 0, 0]
if passed == True:
result[build][PASS] += 1
else:
result[build][FAIL] += 1
if timedout:
result[build][TIMEDOUT] += 1
print "build,numPass,numFail,passRate,numTimedout"
builds = sorted(result.keys(), key = lambda x: natural_keys(x))
for build in builds:
passFail = result[build]
print "%s,%s,%s,%s,%s" % (build, passFail[PASS], passFail[FAIL], float(passFail[PASS])/(passFail[PASS]+passFail[FAIL]), passFail[TIMEDOUT])
#print passFail[2]
'''
Not complete yet
'''
# dat = read.csv("stdaway.csv", row.names=1, header=F)
def parameterAnalysis():
testInfoDB = db.testStat.find({}, {'_id':1, 'testParams': 1, 'duration': 1, 'testName': 1, 'buildNumber': 1, 'passed': 1})
testGroupByName = {}
# first group the tests by test names in order to do parameter analysis
# test with the same names are the same test with different parameters
for test in testInfoDB:
testName = test['testName']
buildNumber = test['buildNumber']
if testName not in testGroupByName:
testGroupByName[testName] = []
testGroupByName[testName].append(test)
for testName, testGroup in testGroupByName.items():
parameterDict = {}
for test in testGroup:
#TODO: need to skip failed tests
# get the execution time for the set of parameters
testTimeForParam = zip(test['testParams'], test['duration'])
# group by build number
# if the same build, then take average
testTimeGroupedByBuild = zip(testTimeForParam, test['buildNumber'])
testTimeGroupedByBuild = zip(testTimeGroupedByBuild, test['passed'])
for testInfo in testTimeGroupedByBuild:#testTimeForParam:
parameters = testInfo[0][0][0]
duration = testInfo[0][0][1]
buildNum = testInfo[0][1]
passed = testInfo[1]
if passed == False:
continue
params = [p.split('=')[0] for p in parameters]
paramValues = [p.split('=')[1] for p in parameters]
# keep track of the parameters and their values
for p, val in zip(params, paramValues):
if p not in parameterDict:
parameterDict[p] = []
parameterDict[p].append(val)
# store the execution time of the test
if 'duration' not in parameterDict:
parameterDict['duration'] = []
parameterDict['duration'].append(duration)
if 'buildNumber' not in parameterDict:
parameterDict['buildNumber'] = []
parameterDict['buildNumber'].append(buildNum)
paramNameList = []
valList = []
for param, vals in parameterDict.items():
paramNameList.append(param)
valList.append(vals)
try:
values = reduce(zip, valList)
except TypeError:
continue
print ",".join(paramNameList)
for v in values:
print ",".join(map(str, flatten(v)))
print '-------------------'
'''
for k, v in testTime.items():
sortedTest = sorted(v, key = lambda x: x[0])
#print sortedTest
time = [x[1] for x in sortedTest]
#print k+","+",".join(time)
print k+","+str(numpy.var(time))
if k == "9b1d2ce19c29d0f044c78b3226393e89":
print k, time
#print k, sortedTest
f = db.test.find({"_id":111})
'''
"""
Get the pass rate, # of passes, and # of fails for each build
"""
def prioritizedTests():
testData = db.testStat.find({}, {'passed': 1, 'testRun': 1, 'testName': 1, 'buildNumber': 1})
threshold = args.support
buildNumbers = {}
# look at previous n test runs
for test in testData:
testPassInBuild = zip(test['buildNumber'], test['passed'])
# sort by build number
testPassInBuild = list(set(sorted(testPassInBuild, key = lambda x: x[0])))
passOrFail = [i[1] for i in testPassInBuild]
if len(passOrFail) > args.windowSize:
prev = passOrFail[-(args.windowSize+1):-1]
else:
prev = passOrFail[1:-1]
last = passOrFail[-1]
# if pass rate > support threshold (default 0.8)
if sum(prev)/float(args.windowSize) > threshold:
# these tests should run first
#if last != True:
print test['_id'], prev, last, test['testName']
'''
Finding outliers. sd is the number of standard deviation
'''
def findOutliersMedian(data, sd = 2):
d = numpy.abs(data - numpy.median(data))
mdev = numpy.median(d)
s = d/mdev if mdev else 0 #
return data[s<sd]
'''
Finding outliers.
Return true if it is NOT an outlier.
'''
def findOutliersMean(data, sd=2):
return abs(data - numpy.mean(data)) < sd * numpy.std(data)
'''
How many std away?
'''
def stdDiff(data):
return abs(data - numpy.mean(data)) / numpy.std(data)
'''
How many MAD (median absolute deviation) away?
'''
def computeMAD(data):
d = numpy.abs(data - numpy.median(data))
#mdev = numpy.mean(d)/numpy.median(data)
mdev = numpy.median(d)/numpy.median(data)
#s = float(mdev - min(data))/(max(data)-min(data))
#s = d/mdev if mdev else 0 #
return mdev
'''
How many deviations away for each of the execution time (including outliers)
'''
def stdAnalysis():
allTests = db.testStat.find({}, {'_id':1, 'duration': 1, 'testName': 1, 'buildNumber': 1, 'testSteps': 1, 'passed': 1, 'testRun':1})
for test in allTests:
# only look at passed test cases
passedTests = zip(test['passed'], test['duration'], test['testSteps'], test['buildNumber'])
passedTests = filter(lambda x: x[0] == True, passedTests)
testDuration = [duration for passed, duration, testSteps, buildNumber in passedTests]
regressionIndex = [key for key, elem in enumerate(findOutliersMean(testDuration, 3).tolist()) if elem == False]
testDuration = [duration for index, duration in enumerate(testDuration) if index not in regressionIndex]
# how many std away are the execution time? on avearage
result = test['_id']+ ',' + ','.join(map(str, stdDiff(testDuration)))
for i in range(78-len(testDuration)):
result+= 'NA,'
print result
#print test['_id']+ ',' + ','.join(map(str, stdDiff(testDuration)))
#for regression in regressionIndex:
# print test['_id'], passedTests[regression][3]
'''
Check the performance of the newest test, and see if it is an outlier
'''
def checkNewTestPerformance():
allTests = db.testStat.find({}, {'_id':1, 'duration': 1, 'testName': 1, 'buildNumber': 1, 'testSteps': 1, 'passed': 1, 'testRun':1})
stdList = []
timing = []
output = open("checkNewTestPerformance.txt", "w")
for test in allTests:
# only look at passed test cases
passedTests = zip(test['passed'], test['duration'], test['testSteps'], test['buildNumber'])
passedTests = filter(lambda x: x[0] == True, passedTests)
passedTests = sorted(passedTests, key = lambda x: natural_keys(x[3]))
testDuration = [duration for passed, duration, testSteps, buildNumber in passedTests]
outlierIndex = [key for key, elem in enumerate(findOutliersMean(testDuration, 3).tolist()) if elem == False]
if len(testDuration) == 0: # test never passed
continue
newestTestTime = testDuration[-1]
newestTestSteps = passedTests[-1][2]
# remove outlier from the duration
#prevTestTime = [time for index, time in enumerate(testDuration) if index not in outlierIndex][:-1]
#prevTestSteps = [testInfo[2] for index, testInfo in enumerate(passedTests) if index not in outlierIndex][:-1]
# get the test time and test step time
prevTestTime = [time for index, time in enumerate(testDuration)][:-1]
prevTestSteps = [testInfo[2] for index, testInfo in enumerate(passedTests) ][:-1]
# version data, currently not used
versions = [testInfo[3] for index, testInfo in enumerate(passedTests)]
if len(prevTestTime) == 0:
continue
# only look at prev tests with a window size of 15
# if we have less than 15, then we consider all
if len(prevTestTime) > args.windowSize:
prevTestTime = prevTestTime[-args.windowSize:]
std = numpy.std(prevTestTime)
# number of std away
if std == 0:
continue
stdAway = abs(newestTestTime - numpy.mean(prevTestTime))/std
stdList.append(std/numpy.mean(prevTestTime)*100)
timing.append(numpy.mean(prevTestTime))
# if it is an outlier
if stdAway >= 3 and std != 0:
diff = len(versions) - len(prevTestTime)
#print sorted(versions, key = lambda x: natural_keys(x))[diff:]
output.write('testName;stdAway;newestTestTime;prevTime;range\n')
output.write( '%s;%s;%s;%s;%s\n' % (test['testName'], stdAway, newestTestTime, prevTestTime[-1], str(numpy.mean(prevTestTime)) +'+/-' + str(std)))
output.write(",".join(map(str, prevTestTime)))
output.write('\n')
# stepwise timing difference between the newest test and previous test
for prevStep, step in zip(prevTestSteps[-1], newestTestSteps):
output.write( 'Diff:%s\n' %( step[0] - prevStep[0]))
output.write(",".join(map(str, step)))
output.write('\n')
output.write(",".join(map(str, prevStep)))
output.write('\n')
prevTestSteps.append(newestTestSteps)
# consider only past X tests (windowSize)
checkStepwisePerfRegression(output, prevTestSteps[-args.windowSize:])
output.write( '----------------------')
output.close()
'''
Check which step in the newest test cause a performance regression
'''
def checkStepwisePerfRegression(output, allTestSteps):
printed = False
stepWiseResult = []
# create an empty array to store the timing info for each step in prev tests
for i in range(0, len(allTestSteps[0])):
stepWiseResult.append([])
for steps in allTestSteps:
for i, step in enumerate(steps):
stepWiseResult[i].append(step)
for steps in stepWiseResult:
timing = []
for i, step in enumerate(steps):
if i == 0:
timing.append(step[1])
timing.append(step[0])
std = numpy.std(timing[1:-1])
newestStepTime = timing[-1]
stddiff = abs(newestStepTime - numpy.mean(timing[1:-1]))/std
if stddiff > 2:
if not printed:
output.write( 'History of the execution time of the outlier step:\n')
output.write( 'testName;stdAway;newestStepTime;prevStepTime\n')
printed = True
output.write( "%s;%s;%s;%s\n" % (timing[0], stddiff, newestStepTime, str(numpy.mean(timing[1:-1]))+'+/-'+str(std)))
output.write(",".join(map(str, timing[1:])))
output.write('\n')
'''
Using regression to find tests with an upward trend.
Plot moving averages for such tests.
'''
def findTestExecutionTrend():
allTests = db.testStat.find({}, {'_id':1, 'duration': 1, 'testName': 1, 'buildNumber': 1, 'testSteps': 1, 'passed': 1, 'testRun':1})
plot = importr('grDevices')
graphics = importr('graphics')
output = open("perfTrend.csv", 'w+')
os.chdir(args.dst)
output.write( 'testName;slope;pvalue;testDurations\n')
for test in allTests:
# only look at passed test cases
passedTests = zip(test['passed'], test['duration'], test['testSteps'], test['buildNumber'])
passedTests = filter(lambda x: x[0] == True, passedTests)
passedTests = sorted(passedTests, key = lambda x: natural_keys(x[3]))
if len(passedTests) > args.windowSize:
passedTests = passedTests[-args.windowSize:] # pick only X number of newest tests
testDuration = [duration for passed, duration, testSteps, buildNumber in passedTests]
buildNumbers = [buildNumber for passed, duration, testSteps, buildNumber in passedTests]
outlierIndex = [key for key, elem in enumerate(findOutliersMean(testDuration, 3).tolist()) if elem == False]
# remove outliers
testDuration = [duration for index, duration in enumerate(testDuration) if index not in outlierIndex]
if len(testDuration) == 0: # test never passed
continue
movingAvg = calculateMovingAverage(testDuration, window=5).tolist()
slope, intercept, r_value, p_value, std_err = stats.linregress(range(1, len(movingAvg)+1), movingAvg)
#print slope, intercept, r_value, p_value, std_err
if slope > 0 and p_value < 0.01:
output.write("%s;%s;%s;%s\n"%(test['testName'], slope, p_value, testDuration))
#movingAvg = calculateMovingAverage(testDuration, window=5).tolist()
#print movingAvg
plot.jpeg('movingAvg_%s_%s.jpg' % (slope, test['testName']))
graphics.plot(ro.FloatVector(movingAvg), type='l', ylab="", main=test['testName'])
plot.dev_off()
'''
not used in the script
'''
def intensity(values):
result = [0]
for p, n in zip(values, values[1:]):
if n > p:
result.append(result[-1]+1)
else:
if result[-1] == 0:
result.append(0)
else:
result.append(result[-1]-1)
return result
def calculateMovingAverage(values, window=3):
weigths = numpy.repeat(1.0, window)/window
movingAvg = numpy.convolve(values, weigths, 'valid')
return movingAvg
'''
Get the p, i, q (ARIMA model) for each test
'''
def examineArimaModel():
forecast = importr('forecast')
stats = importr('stats')
ro.r('''
getPval <- function(model) {
return((1-pnorm(abs(model$coef)/sqrt(diag(model$var.coef))))*2)
}
''')
ro.r('''
getCoef <- function(model) {
return(model$coef)
}
''')
rGetPval = ro.globalenv['getPval']
rGetCoef = ro.globalenv['getCoef']
allTests = db.testStat.find({}, {'_id':1, 'duration': 1, 'testName': 1, 'buildNumber': 1, 'testSteps': 1, 'passed': 1, 'testRun':1})
stdList = []
timing = []
print "testName,arimaResult,COV,mean,std,numOutlier,perctOutlier,pval,coef,normPval"
for test in allTests:
# only look at passed test cases
passedTests = zip(test['passed'], test['duration'], test['testSteps'], test['buildNumber'])
passedTests = filter(lambda x: x[0] == True, passedTests)
passedTests = sorted(passedTests, key = lambda x: natural_keys(x[3]))
testDuration = [duration for passed, duration, testSteps, buildNumber in passedTests]
outlierIndex = [index for index, elem in enumerate(findOutliersMean(testDuration, 3).tolist()) if elem == False]
testDuration = [duration for index, duration in enumerate(passedTests) if index not in outlierIndex]
if len(testDuration) == 0: # test never passed
continue
newestTestTime = testDuration[-1]
newestTestSteps = passedTests[-1][2]
# remove outlier from the duration
allTestTime = [time for index, time in enumerate(testDuration) if index not in outlierIndex]
#prevTestTime = [time for index, time in enumerate(testDuration)]#[:-1]
versions = [testInfo[3] for index, testInfo in enumerate(passedTests)]
if len(allTestTime) == 0:
continue
try:
lambdaVal = forecast.BoxCox_lambda(ro.FloatVector(allTestTime))
normalizedTimeRO = forecast.BoxCox(ro.FloatVector(allTestTime), lambdaVal)
rTimeSeries = stats.ts(ro.FloatVector(normalizedTimeRO))
arimaModel = forecast.auto_arima(rTimeSeries)
arimaResult = re.findall('.*?ARIMA\(([0-9]+,[0-9]+,[0-9]+)\).*?', str(arimaModel))[0]
pval = str(rGetPval(arimaModel)).replace('\n', '').strip()
pval = re.sub('\s+', ' ', pval)
coef = str(rGetCoef(arimaModel)).replace('\n', '').strip()
coef = re.sub('\s+', ' ', coef)
print "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % (test['testName'], arimaResult.replace(',',' '), numpy.std(testDuration)/numpy.mean(testDuration), numpy.mean(testDuration), numpy.std(testDuration), len(outlierIndex), len(outlierIndex)/float(len(passedTests)), pval, coef, str(stats.shapiro_test(ro.FloatVector(allTestTime)).rx2('p.value')).split(']')[1].strip())
except rpy2.rinterface.RRuntimeError:
continue
'''
Build a model based on n to predict n+1.
If there are 11 executions, there will be 2 models.
Report if the actual vs predicted is an outlier.
'''
def arimaPredict():
forecast = importr('forecast')
stats = importr('stats')
ro.r('''
getPval <- function(model) {
return((1-pnorm(abs(model$coef)/sqrt(diag(model$var.coef))))*2)
}
''')
ro.r('''
getCoef <- function(model) {
return(model$coef)
}
''')
rGetPval = ro.globalenv['getPval']
rGetCoef = ro.globalenv['getCoef']
allTests = db.testStat.find({}, {'_id':1, 'duration': 1, 'testName': 1, 'buildNumber': 1, 'testSteps': 1, 'passed': 1, 'testRun':1})
stdList = []
timing = []
print 'testName;predictedCategory;arima;actual;predicted;origRunTime;prevTestSteps;curTestSteps;runID'
for test in allTests:
# only look at passed test cases
passedTests = zip(test['passed'], test['duration'], test['testSteps'], test['buildNumber'])
passedTests = filter(lambda x: x[0] == True, passedTests)
passedTests = sorted(passedTests, key = lambda x: natural_keys(x[3]))
testDuration = [duration for passed, duration, testSteps, buildNumber in passedTests]
regressionIndex = [key for key, elem in enumerate(findOutliersMean(testDuration, 3).tolist()) if elem == False]
if len(testDuration) == 0: # test never passed
continue
newestTestTime = testDuration[-1]
newestTestSteps = passedTests[-1][2]
# remove regression from the duration
#prevTestTime = [time for index, time in enumerate(testDuration) if index not in regressionIndex][:-1]
prevTestTime = [time for index, time in enumerate(testDuration)]#[:-1]
if len(prevTestTime) == 0:
continue
if len(prevTestTime) <= args.windowSize:
continue
prevTestSteps = [testInfo[2] for index, testInfo in enumerate(passedTests)][:-1][-1]
for i in range(args.windowSize, len(prevTestTime)):
try:
testTime = prevTestTime[0:i+1]
lambdaVal = forecast.BoxCox_lambda(ro.FloatVector(testTime))
#normalizedTimeRO = ro.FloatVector(testTime) #forecast.BoxCox(ro.FloatVector(testTime), lambdaVal)
timeRO = ro.FloatVector(testTime)
timeRO = forecast.BoxCox(ro.FloatVector(testTime), lambdaVal)
rTimeSeries = stats.ts(ro.FloatVector(timeRO))
arimaModel = forecast.auto_arima(rTimeSeries[0:-1]) # build model using first n
arimaResult = re.findall('.*?ARIMA\(([0-9]+,[0-9]+,[0-9]+)\).*?', str(arimaModel))[0]
# random walk
if arimaResult == '0,0,0':
continue
predictionResult = str(forecast.forecast(arimaModel, 1)).split('\n')[1]
#print timeRO[-1], predictionResult, passedTests[i][3]
predictionResult = re.split('\s+', predictionResult.strip())[2:]
predictionResult = map(float, predictionResult)
# lo 80, hi 80, lo 95, hi 95
if timeRO[-1] >= predictionResult[3]:
#print test['testName'], 'high', arimaResult, timeRO[-1], predictionResult, passedTests[i][3]
print '%s;%s;%s;%s;%s;%s;%s;%s;%s' % (test['testName'], 'high', arimaResult, timeRO[-1], predictionResult, prevTestTime[0:i+1], prevTestSteps, newestTestSteps, passedTests[i][3])
elif timeRO[-1] <= predictionResult[2]:
#print test['testName'], 'low', arimaResult, timeRO[-1], predictionResult, passedTests[i][3]
print '%s;%s;%s;%s;%s;%s;%s;%s;%s' % (test['testName'], 'low', arimaResult, timeRO[-1], predictionResult, prevTestTime[0:i+1], prevTestSteps, newestTestSteps, passedTests[i][3])
except rpy2.rinterface.RRuntimeError:
continue
'''
Build a model based on n to predict the newest test.
Report if the actual vs predicted is an outlier.
'''
def checkNewTestPerfARIMA():
forecast = importr('forecast')
stats = importr('stats')
timeSeries = importr('timeSeries')
plot = importr('grDevices')
graphics = importr('graphics')
ro.r('''
getPval <- function(model) {
return((1-pnorm(abs(model$coef)/sqrt(diag(model$var.coef))))*2)
}
''')
ro.r('''
getCoef <- function(model) {
return(model$coef)
}
''')
rGetPval = ro.globalenv['getPval']
rGetCoef = ro.globalenv['getCoef']
allTests = db.testStat.find({}, {'_id':1, 'duration': 1, 'testName': 1, 'buildNumber': 1, 'testSteps': 1, 'passed': 1, 'testRun':1})
stdList = []
timing = []
output = open('checkNewTestPerfARIMA.txt', 'w+')
os.chdir(args.dst)
output.write('testName;predictedCategory;arima;actual;predicted;origRunTime;prevTestSteps;curTestSteps;runID\n')
for test in allTests:
# only look at passed test cases
passedTests = zip(test['passed'], test['duration'], test['testSteps'], test['buildNumber'])
passedTests = filter(lambda x: x[0] == True, passedTests)
passedTests = sorted(passedTests, key = lambda x: natural_keys(x[3]))
testDuration = [duration for passed, duration, testSteps, buildNumber in passedTests]
outlierIndex = [key for key, elem in enumerate(findOutliersMean(testDuration, 3).tolist()) if elem == False]
if len(testDuration) == 0: # test never passed
continue
newestTestTime = testDuration[-1]
newestTestSteps = passedTests[-1][2]
# remove regression from the duration
prevTestTime = [time for index, time in enumerate(testDuration[:-1]) if index not in outlierIndex]#[:-1]
passedTest = [t for index, t in enumerate(passedTests[:-1]) if index not in outlierIndex]#[:-1]
#prevTestTime = [time for index, time in enumerate(testDuration)]#[:-1]
if len(prevTestTime) == 0:
continue
if len(prevTestTime) <= args.windowSize:
continue
prevTestStep = [testInfo[2] for index, testInfo in enumerate(passedTests)][:-1][-1]
prevTestSteps = [testInfo[2] for index, testInfo in enumerate(passedTests)]
try:
prevTestTime.append(newestTestTime)
testTime = prevTestTime
lambdaVal = forecast.BoxCox_lambda(ro.FloatVector(testTime))
timeRO = ro.FloatVector(testTime)
timeRO = forecast.BoxCox(ro.FloatVector(testTime), lambdaVal)
rTimeSeries = stats.ts(ro.FloatVector(timeRO))
arimaModel = forecast.auto_arima(rTimeSeries[0:-1]) # build model using first n
arimaResult = re.findall('.*?ARIMA\(([0-9]+,[0-9]+,[0-9]+)\).*?', str(arimaModel))[0]
# random walk
if arimaResult == '0,0,0':
continue
predictionResult = str(forecast.forecast(arimaModel, 1)).split('\n')[1]
predictionResult = re.split('\s+', predictionResult.strip())[2:]
predictionResult = map(float, predictionResult)
# lo 80, hi 80, lo 95, hi 95
if timeRO[-1] >= predictionResult[3]: # over hi 95
# plotting predicted and actual graph
plot.jpeg('arimaPredict_%s.jpg' % test['testName'])
# predict future 5 builds
graphics.plot(forecast.forecast(arimaModel, '5'))
graphics.points(len(testTime), timeRO[-1])
plot.dev_off()
output.write('%s;%s;%s;%s;%s;%s;%s;%s;%s\n' % (test['testName'], 'high', arimaResult, timeRO[-1], predictionResult, prevTestTime, prevTestStep, newestTestSteps, passedTests[-1][3]))
checkStepwisePerfRegression(output, prevTestSteps)
output.write( '--------------------\n')
'''
elif timeRO[-1] <= predictionResult[2]: # lower than lo 95
# plotting predicted and actual graph
plot.jpeg('%s.jpg' % test['testName'].split('.')[-1])
graphics.plot(forecast.forecast(arimaModel, '5'))
graphics.points(len(testTime), timeRO[-1])
plot.dev_off()
print '%s;%s;%s;%s;%s;%s;%s;%s;%s' % (test['testName'], 'low', arimaResult, timeRO[-1], predictionResult, prevTestTime[0:-1], prevTestStep, newestTestSteps, passedTests[-1][3])
print 'History of the execution time of the outlier step:'
checkStepwisePerfRegression(prevTestSteps)
print '--------------------'
'''
except rpy2.rinterface.RRuntimeError:
continue
'''
Compare the test execution time in the same build