Merge "Support for Patch across multiple data nodes"
[cps.git] / test-tools / subtract-metrics-reports.py
1 #!/usr/bin/env python3
2 #
3 # Copyright 2023 Nordix Foundation.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import argparse
19 import csv
20
21
22 def load_metrics_table(filename):
23     with open(filename) as tsvFile:
24         csvreader = csv.DictReader(tsvFile, dialect="excel-tab")
25         table = {}
26         for source_row in csvreader:
27             method, count, sum_time = source_row['Method'], source_row['Count'], source_row['Sum']
28             table[method] = { 'Count': int(float(count)), 'Sum': float(sum_time) }
29     return table
30
31
32 def save_metrics_table(table, filename):
33     with open(filename, 'w', newline='') as outfile:
34         csvwriter = csv.writer(outfile, dialect="excel-tab")
35         csvwriter.writerow(["Method", "Count", "Sum"])
36         for method in table:
37             count, sum_time = table[method]['Count'], table[method]['Sum']
38             csvwriter.writerow([method, count, sum_time])
39
40
41 def subtract_metrics_tables(table, table_to_subtract):
42     result = {}
43     for method in table:
44         result[method] = table[method]
45     for method in table_to_subtract:
46         result[method]['Count'] = result[method]['Count'] - table_to_subtract[method]['Count']
47         result[method]['Sum'] = result[method]['Sum'] - table_to_subtract[method]['Sum']
48     return filter_null_metrics_from_metrics_table(result)
49
50
51 def filter_null_metrics_from_metrics_table(table):
52     result = {}
53     for method in table:
54         if table[method]['Count'] > 0:
55             result[method] = table[method]
56     return result
57
58
59 if __name__ == "__main__":
60     parser = argparse.ArgumentParser()
61     parser.add_argument('-a', '--metrics-after',
62                         required=True,
63                         help='path to metrics table to subtract from',
64                         dest='tsvpath_after',
65                         type=str)
66     parser.add_argument('-b', '--metrics-before',
67                         required=True,
68                         help='path to metrics table to subtract',
69                         dest='tsvpath_before',
70                         type=str)
71     parser.add_argument('-o', '--output',
72                         required=True,
73                         help='path to output metrics table',
74                         dest='outpath',
75                         type=str)
76     args = parser.parse_args()
77     table1 = load_metrics_table(args.tsvpath_before)
78     table2 = load_metrics_table(args.tsvpath_after)
79     table_diff = subtract_metrics_tables(table2, table1)
80     save_metrics_table(table_diff, args.outpath)