3 # Copyright 2023 Nordix Foundation.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
22 def load_metrics_table(filename):
23 with open(filename) as tsvFile:
24 csvreader = csv.DictReader(tsvFile, dialect="excel-tab")
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) }
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"])
37 count, sum_time = table[method]['Count'], table[method]['Sum']
38 csvwriter.writerow([method, count, sum_time])
41 def subtract_metrics_tables(table, table_to_subtract):
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)
51 def filter_null_metrics_from_metrics_table(table):
54 if table[method]['Count'] > 0:
55 result[method] = table[method]
59 if __name__ == "__main__":
60 parser = argparse.ArgumentParser()
61 parser.add_argument('-a', '--metrics-after',
63 help='path to metrics table to subtract from',
66 parser.add_argument('-b', '--metrics-before',
68 help='path to metrics table to subtract',
69 dest='tsvpath_before',
71 parser.add_argument('-o', '--output',
73 help='path to output metrics table',
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)