[NFC] Add utility to sum/merge stats files
authorTyker <tyker1@outlook.com>
Fri, 7 Aug 2020 17:02:25 +0000 (19:02 +0200)
committerTyker <tyker1@outlook.com>
Fri, 7 Aug 2020 17:02:42 +0000 (19:02 +0200)
Add a small script to sum *.stats file given as input and output the totals
usage example:
    merge-stats.py $(find ./builddir/ -name "*.stats") > total.stats

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D83505

llvm/utils/merge-stats.py [new file with mode: 0755]

diff --git a/llvm/utils/merge-stats.py b/llvm/utils/merge-stats.py
new file mode 100755 (executable)
index 0000000..342be3c
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+'''
+Merge .stats files generated by llvm tools
+
+merge-stats.py takes as argument a list of stats files to merge
+and output the result on stdout
+
+Usage:
+  merge-stats.py $(find ./builddir/ -name "*.stats") > total.stats
+'''
+
+import json
+import sys
+
+result = {}
+
+for arg in range(1, len(sys.argv)):
+  with open(sys.argv[arg], "r", encoding='utf-8',
+            errors='ignore') as f:
+    text = f.read()
+    try:
+      data = json.loads(text)
+    except:
+      print('ignored %s: failed to parse' % sys.argv[arg], file= sys.stderr)
+      continue
+    for key in data:
+      if key in result:
+        result[key] += data[key]
+      else:
+        result[key] = data[key]
+
+out = json.dumps(result, indent=2)
+print(out)