Skip to content

Instantly share code, notes, and snippets.

@teju85
Created February 25, 2020 08:26
Show Gist options
  • Save teju85/4bc36f534603bc5b1289d766710a8875 to your computer and use it in GitHub Desktop.
Save teju85/4bc36f534603bc5b1289d766710a8875 to your computer and use it in GitHub Desktop.
Simple script to compare 2 json files from google C++ benchmark library
#!/usr/bin/env python
import sys
import json
def getTimeInSecs(run):
unit = run["time_unit"]
if unit == "us":
return run["real_time"] * 1000000
if unit == "ms":
return run["real_time"] * 1000
if unit == "s":
return run["real_time"]
raise Exception("Bad time_unit '%s' for test '%s'" % (unit, run["name"]))
def getAllTests(file):
fp = open(file, "r")
data = json.load(fp)
if "benchmarks" not in data:
raise Exception("Bad json format in file '%s'!" % file)
tests = []
for b in data["benchmarks"]:
tests.append((b["name"], getTimeInSecs(b)))
fp.close()
return tests
def main(file1, file2):
print("Compare '%s' with '%s'" % (file1, file2))
t1 = getAllTests(file1)
t2 = getAllTests(file2)
if len(t1) != len(t2):
raise Exception("Number of tests mismatch!")
for i in range(len(t1)):
i1, i2 = t1[i], t2[i]
if i1[0] != i2[0]:
raise Exception("Test '%d' mismatch '%s' vs '%s'!" % (i, i1[0], i2[0]))
print("%4d %-64s %.2f" % (i, i1[0], i2[1] * 1.0 / i1[1]))
return
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment