CSIT-1135: Scripts for VPP per-patch testing
[csit.git] / resources / tools / scripts / compare_perpatch.py
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Script for determining whether per-patch perf test votes -1.
15
16 This script assumes there exist two text files with processed BMRR results,
17 located at hardcoded relative paths, having several lines
18 of json-parseable lists of float values, corresponding to testcase results.
19 This script then uses jumpavg library to determine whether there was
20 a regression, progression or no change for each testcase.
21 If number of tests does not match, or there was a regression,
22 this script votes -1 (by exiting with code 1), otherwise it votes +1 (exit 0).
23 """
24
25 import json
26 import sys
27
28 from jumpavg.BitCountingMetadataFactory import BitCountingMetadataFactory
29 from jumpavg.BitCountingClassifier import BitCountingClassifier
30
31
32 def hack(value_list):
33     """Return middle two quartiles, hoping to reduce influence of outliers.
34
35     :param value_list: List to pick subset from.
36     :type value_list: list of float
37     :returns: New list containing middle values.
38     :rtype: list of float
39     """
40     tmp = sorted(value_list)
41     quarter = len(tmp) / 4
42     ret = tmp[quarter:-quarter]
43     return ret
44
45 parent_lines = list()
46 new_lines = list()
47 with open("csit_parent/results.txt") as parent_file:
48     parent_lines = parent_file.readlines()
49 with open("csit_new/results.txt") as new_file:
50     new_lines = new_file.readlines()
51 if len(parent_lines) != len(new_lines):
52     print "Number of passed tests does not match!"
53     sys.exit(1)
54 classifier = BitCountingClassifier()
55 num_tests = len(parent_lines)
56 exit_code = 0
57 for index in range(num_tests):
58     parent_values = hack(json.loads(parent_lines[index]))
59     new_values = hack(json.loads(new_lines[index]))
60     parent_max = BitCountingMetadataFactory.find_max_value(parent_values)
61     new_max = BitCountingMetadataFactory.find_max_value(new_values)
62     cmax = max(parent_max, new_max)
63     factory = BitCountingMetadataFactory(cmax)
64     parent_stats = factory.from_data(parent_values)
65     factory = BitCountingMetadataFactory(cmax, parent_stats.avg)
66     new_stats = factory.from_data(new_values)
67     print "DEBUG parent: {p}".format(p=parent_stats)
68     print "DEBUG new: {n}".format(n=new_stats)
69     common_max = max(parent_stats.avg, new_stats.avg)
70     difference = (new_stats.avg - parent_stats.avg) / common_max
71     print "DEBUG difference: {d}%".format(d=100 * difference)
72     classified_list = classifier.classify([parent_stats, new_stats])
73     if len(classified_list) < 2:
74         print "Test index {index}: normal (no anomaly)".format(
75             index=index)
76         continue
77     anomaly = classified_list[1].metadata.classification
78     if anomaly == "regression":
79         print "Test index {index}: anomaly regression".format(index=index)
80         exit_code = 1
81         continue
82     print "Test index {index}: anomaly {anomaly}".format(
83         index=index, anomaly=anomaly)
84 print "DEBUG exit code {code}".format(code=exit_code)
85 sys.exit(exit_code)