Perpatch compare: pylint fixes.
[csit.git] / resources / tools / integrated / compare_perpatch.py
1 # Copyright (c) 2019 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 (subdirs thereof), 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 resources.libraries.python import jumpavg
29
30
31 def main():
32     """Execute the main logic, return the code to return as return code.
33
34     :returns: Return code, 0 or 3 based on the comparison result.
35     :rtype: int
36     """
37     iteration = -1
38     parent_iterations = list()
39     current_iterations = list()
40     num_tests = None
41     while 1:
42         iteration += 1
43         parent_lines = list()
44         current_lines = list()
45         filename = f"csit_parent/{iteration}/results.txt"
46         try:
47             with open(filename) as parent_file:
48                 parent_lines = parent_file.readlines()
49         except IOError:
50             break
51         num_lines = len(parent_lines)
52         filename = f"csit_current/{iteration}/results.txt"
53         with open(filename) as current_file:
54             current_lines = current_file.readlines()
55         if num_lines != len(current_lines):
56             print(
57                 f"Number of tests does not match within iteration {iteration}",
58                 file=sys.stderr
59             )
60             return 1
61         if num_tests is None:
62             num_tests = num_lines
63         elif num_tests != num_lines:
64             print(
65                 f"Number of tests does not match previous at iteration "
66                 f"{iteration}", file=sys.stderr
67             )
68             return 1
69         parent_iterations.append(parent_lines)
70         current_iterations.append(current_lines)
71     exit_code = 0
72     for test_index in range(num_tests):
73         parent_values = list()
74         current_values = list()
75         for iteration_index in range(len(parent_iterations)):
76             parent_values.extend(
77                 json.loads(parent_iterations[iteration_index][test_index])
78             )
79             current_values.extend(
80                 json.loads(current_iterations[iteration_index][test_index])
81             )
82         print(f"Time-ordered MRR values for parent build: {parent_values}")
83         print(f"Time-ordered MRR values for current build: {current_values}")
84         parent_values = sorted(parent_values)
85         current_values = sorted(current_values)
86         max_value = max([1.0] + parent_values + current_values)
87         parent_stats = jumpavg.AvgStdevStats.for_runs(parent_values)
88         current_stats = jumpavg.AvgStdevStats.for_runs(current_values)
89         parent_group_list = jumpavg.BitCountingGroupList(
90             max_value=max_value).append_group_of_runs([parent_stats])
91         combined_group_list = parent_group_list.copy(
92             ).extend_runs_to_last_group([current_stats])
93         separated_group_list = parent_group_list.append_group_of_runs(
94             [current_stats])
95         print(f"Value-ordered MRR values for parent build: {parent_values}")
96         print(f"Value-ordered MRR values for current build: {current_values}")
97         avg_diff = (current_stats.avg - parent_stats.avg) / parent_stats.avg
98         print(f"Difference of averages relative to parent: {100 * avg_diff}%")
99         print(f"Jumpavg representation of parent group: {parent_stats}")
100         print(f"Jumpavg representation of current group: {current_stats}")
101         print(
102             f"Jumpavg representation of both as one group:"
103             f" {combined_group_list[0].stats}"
104         )
105         bits_diff = separated_group_list.bits - combined_group_list.bits
106         compared = u"longer" if bits_diff >= 0 else u"shorter"
107         print(
108             f"Separate groups are {compared} than single group"
109             f" by {abs(bits_diff)} bits"
110         )
111         # TODO: Version of classify that takes max_value and list of stats?
112         # That matters if only stats (not list of floats) are given.
113         classified_list = jumpavg.classify([parent_values, current_values])
114         if len(classified_list) < 2:
115             print(f"Test test_index {test_index}: normal (no anomaly)")
116             continue
117         anomaly = classified_list[1].comment
118         if anomaly == u"regression":
119             print(f"Test test_index {test_index}: anomaly regression")
120             exit_code = 3  # 1 or 2 can be caused by other errors
121             continue
122         print(f"Test test_index {test_index}: anomaly {anomaly}")
123     print(f"Exit code: {exit_code}")
124     return exit_code
125
126 if __name__ == u"__main__":
127     sys.exit(main())