1 # Copyright (c) 2021 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Script for determining whether per-patch perf test votes -1.
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).
28 from resources.libraries.python import jumpavg
32 """Execute the main logic, return the code to return as return code.
34 :returns: Return code, 0 or 3 based on the comparison result.
38 parent_iterations = list()
39 current_iterations = list()
44 current_lines = list()
45 filename = f"csit_parent/{iteration}/results.txt"
47 with open(filename) as parent_file:
48 parent_lines = parent_file.readlines()
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):
57 f"Number of tests does not match within iteration {iteration}",
63 elif num_tests != num_lines:
65 f"Number of tests does not match previous at iteration "
66 f"{iteration}", file=sys.stderr
69 parent_iterations.append(parent_lines)
70 current_iterations.append(current_lines)
72 for test_index in range(num_tests):
73 parent_values = list()
74 current_values = list()
75 for iteration_index, _ in enumerate(parent_iterations):
77 json.loads(parent_iterations[iteration_index][test_index])
79 current_values.extend(
80 json.loads(current_iterations[iteration_index][test_index])
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(
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}")
102 f"Jumpavg representation of both as one group:"
103 f" {combined_group_list[0].stats}"
105 bits_diff = separated_group_list.bits - combined_group_list.bits
106 compared = u"longer" if bits_diff >= 0 else u"shorter"
108 f"Separate groups are {compared} than single group"
109 f" by {abs(bits_diff)} bits"
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)")
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
122 print(f"Test test_index {test_index}: anomaly {anomaly}")
123 print(f"Exit code: {exit_code}")
126 if __name__ == u"__main__":