1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
import argparse
import json
import pathlib
import sys
from tabulate import tabulate # type: ignore
def generate_metainfo(root: dict) -> str:
machine_info = root["machine_info"]
commit_info = root["commit_info"]
result = "Machine: {} {} on {}({})\n".format(
machine_info["system"],
machine_info["release"],
machine_info["cpu"]["brand"],
machine_info["cpu"]["hz_actual_friendly"],
)
result += "Python: {} {} [{} {}]\n".format(
machine_info["python_implementation"],
machine_info["python_version"],
machine_info["python_compiler"],
machine_info["machine"],
)
result += "Commit: {} on {} in {}\n".format(commit_info["id"], commit_info["branch"], commit_info["time"])
return result
def generate_table(benchmarks: dict, group, type="simple") -> str:
base = 1
table = []
for bm in benchmarks:
if group == bm["group"]:
target = bm["params"]["name"]
rate = 1.0 * bm["extra_info"]["rate"] / 1000000.0
if rate < 10:
rate = round(rate, 2)
else:
rate = round(rate, 1)
min = bm["stats"]["min"]
max = bm["stats"]["max"]
avr = bm["stats"]["mean"]
table.append([target, rate, min, max, avr])
return tabulate(
table, headers=["target", "speed(MB/sec)", "min(sec)", "max(sec)", "mean(sec)"], tablefmt=type
)
def generate_comment(results_file, type):
with open(results_file, "r") as results:
root = json.load(results)
benchmarks = root["benchmarks"]
comment_body = "## Benchmark results\n\n"
comment_body += generate_metainfo(root)
comment_body += "\n\n### Compression benchmarks\n\n"
comment_body += generate_table(benchmarks, "compress", type=type)
comment_body += "\n\n### Decompression benchmarks\n\n"
comment_body += generate_table(benchmarks, "decompress", type=type)
comment_body += "\n\n"
return comment_body
def main():
parser = argparse.ArgumentParser(prog="benchmark_result")
parser.add_argument("jsonfile", type=pathlib.Path, help="pytest-benchmark saved result.")
parser.add_argument("--markdown", action="store_true", help="print markdown table")
args = parser.parse_args()
if args.markdown:
type = "github"
else:
type = "simple"
body = generate_comment(args.jsonfile, type)
print(body)
if __name__ == "__main__":
sys.exit(main())
|