Skip to content

Commit 71fd94c

Browse files
authored
Merge pull request #44836 from fwyzard/add_mergeResourcesJson_script
Add script to merge the JSON files produced by the FastTimerService
2 parents 95766dd + 96f98b2 commit 71fd94c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#! /usr/bin/env python3
2+
3+
import sys
4+
import json
5+
6+
usage = """Usage: mergeResourceJson.py FILE [FILE ...]
7+
8+
Merge the content of multiple "resources.json" files produced by the FastTimerService,
9+
and print the result to standard output.
10+
11+
Example:
12+
mergeResourceJson.py step*/pid*/resources.json > resources.json
13+
"""
14+
15+
def merge_into(metrics, data, dest):
16+
dest["events"] += data["events"]
17+
for metric in metrics:
18+
dest[metric] += data[metric]
19+
20+
21+
if len(sys.argv) == 1:
22+
print(usage)
23+
sys.exit(1)
24+
25+
if '-h' in sys.argv[1:] or '--help' in sys.argv[1:]:
26+
print(usage)
27+
sys.exit(0)
28+
29+
with open(sys.argv[1]) as f:
30+
output = json.load(f)
31+
32+
metrics = [ label for resource in output["resources"] for label in resource ]
33+
34+
datamap = { module["type"] + '|' + module["label"] : module for module in output["modules"] }
35+
36+
for arg in sys.argv[2:]:
37+
with open(arg) as f:
38+
input = json.load(f)
39+
40+
if output["resources"] != input["resources"]:
41+
print("Error: input files describe different metrics")
42+
sys.exit(1)
43+
44+
if output["total"]["label"] != input["total"]["label"]:
45+
print("Warning: input files describe different process names")
46+
merge_into(metrics, input["total"], output["total"])
47+
48+
for module in input["modules"]:
49+
key = module["type"] + '|' + module["label"]
50+
if key in datamap:
51+
merge_into(metrics, module, datamap[key])
52+
else:
53+
datamap[key] = module
54+
output["modules"].append(datamap[key])
55+
56+
json.dump(output, sys.stdout, indent = 2 )
57+
sys.stdout.write('\n')

0 commit comments

Comments
 (0)