Skip to content

Commit

Permalink
Merge 0e9af73 into 51e010e
Browse files Browse the repository at this point in the history
  • Loading branch information
miurahr committed Feb 18, 2021
2 parents 51e010e + 0e9af73 commit 3913695
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -59,6 +59,7 @@ test =
pytest-timeout
pytest-remotedata
pyannotate
py-cpuinfo
coverage[toml]>=5.2
coveralls>=2.1.1
docs =
Expand Down
10 changes: 10 additions & 0 deletions tests/conftest.py
Expand Up @@ -2,6 +2,7 @@
# Thanks to Guilherme Salgado.
import os

import cpuinfo
import pytest
from pyannotate_runtime import collect_types

Expand Down Expand Up @@ -33,3 +34,12 @@ def pytest_benchmark_update_json(config, benchmarks, output_json):
if 'data_size' in benchmark['extra_info']:
rate = benchmark['extra_info'].get('data_size', 0.0) / benchmark['stats']['mean']
benchmark['extra_info']['rate'] = rate


def pytest_benchmark_update_machine_info(config, machine_info):
cpu_info = cpuinfo.get_cpu_info()
brand = cpu_info.get('brand_raw', None)
if brand is None:
brand = '{} core(s) {} CPU '.format(cpu_info.get('count', 'unknown'), cpu_info.get('arch', 'unknown'))
machine_info['cpu']['brand'] = brand
machine_info['cpu']['hz_actual_friendly'] = cpu_info.get('hz_actual_friendly', 'unknown')
81 changes: 35 additions & 46 deletions utils/bench_result.py
Expand Up @@ -6,68 +6,57 @@
from tabulate import tabulate # type: ignore


def read_results_json(results_file) -> dict:
with open(results_file, 'r') as results:
root = json.load(results)
return root


def generate_metainfo(root: dict) -> str:
machine_info = root['machine_info']
commit_info = root['commit_info']
comment_body = 'Machine Info: {} {} [{} {}]\n'.format( machine_info["python_implementation"],
machine_info["python_version"],
machine_info["python_compiler"], machine_info["machine"])
comment_body += 'Commit: {} on {} in {}\n'.format(commit_info['id'], commit_info['branch'], commit_info['time'])
return comment_body


def get_target(benchmark: dict) -> str:
return benchmark['params']['name']


def get_rate(bm: dict):
return bm['extra_info']['rate'] / 1000000


def get_ratio(bm: dict):
return bm['extra_info']['ratio']
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: str, type='simple') -> str:
table = []
for bm in benchmarks:
if bm['group'] == group:
table.append([get_target(bm), get_rate(bm), get_ratio(bm), bm['stats']['min'], bm['stats']['max'], bm['stats']['mean']])
return tabulate(table, headers=['target', 'rate(MB/sec)', 'ratio', 'min(sec)', 'max(sec)', 'mean(sec)'], tablefmt=type)


def generate_comment(root: dict, md=False):
if group == bm['group']:
target = bm['params']['name']
rate = bm['extra_info']['rate'] / 1000000
ratio = bm['extra_info']['ratio']
min = bm['stats']['min']
max = bm['stats']['max']
avr = bm['stats']['mean']
table.append([target, rate, ratio, min, max, avr])
return tabulate(table, headers=['target', 'rate(MB/sec)', 'ratio', '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']
if md:
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='github')
comment_body += '\n\n### Decompression benchmarks\n\n'
comment_body += generate_table(benchmarks, 'decompress', type='github')

else:
comment_body = 'Benchmark results\n--------------\n\n'
comment_body += generate_metainfo(root)
comment_body += '\n\n---- Compression benchmarks\n\n'
comment_body += generate_table(benchmarks, 'compress')
comment_body += '\n\n---- Decompression benchmark results\n\n'
comment_body += generate_table(benchmarks, 'decompress')
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)
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')
parser.add_argument('--markdown', action='store_true', help='print markdown table')
args = parser.parse_args()
body = generate_comment(read_results_json(args.jsonfile), md=args.markdown)
if args.markdown:
type = 'github'
else:
type = 'simple'
body = generate_comment(args.jsonfile, type)
print(body)


Expand Down
2 changes: 1 addition & 1 deletion utils/github_comment_bench.py
Expand Up @@ -21,7 +21,7 @@ def __init__(self):
self.run_id = args.run_id

def post_comment(self):
body = bench.generate_comment(bench.read_results_json(self.jsonfile), md=True)
body = bench.generate_comment(self.jsonfile, type='github')
body += '\n Posted from [the action](https://github.com/{}/actions/runs/{})\n'.format(self.repository, self.run_id)
token = os.getenv("GITHUB_TOKEN")
g = github.Github(token)
Expand Down

0 comments on commit 3913695

Please sign in to comment.