-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcore.py
More file actions
161 lines (121 loc) · 4.23 KB
/
core.py
File metadata and controls
161 lines (121 loc) · 4.23 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from __future__ import print_function
import argparse
import json
import os
import traceback
import logging
from time import time
from types import FunctionType
from collections import OrderedDict
import numpy as np
import matplotlib.pyplot as plt
from jinja2 import Template
import multiprocessing
import platform
log = logging.getLogger("run_benchmarks")
REPORT_FILENAME = 'report/index.html'
DATA_FILENAME = 'report/benchmark_results.json'
LOG_FORMAT = '%(asctime)s %(levelname)-8s %(name)-8s %(message)s'
# =========================
# BENCHMARK DISCOVERY
# =========================
def find_benchmarks(folders=None, platforms=None):
"""Collect benchmarks callable and shared environment initializers."""
benchmark_groups = []
if folders is None:
here = os.path.dirname(os.path.abspath(__file__))
folders = [
f for f in os.listdir(here)
if os.path.isdir(f) and
os.path.exists(os.path.join(f, '__init__.py'))
]
folders.sort()
for folder in folders:
group_name = os.path.basename(folder)
collected_benchmarks = []
modules_in_error = []
pkg = __import__(group_name, fromlist="dummy")
benchmark_groups.append(OrderedDict([
('name', group_name),
('make_env', getattr(pkg, 'make_env', None)),
('benchmarks', collected_benchmarks),
('import_errors', modules_in_error),
]))
for module_filename in sorted(os.listdir(folder)):
module_name, ext = os.path.splitext(module_filename)
if ext not in ('.py', '.so', '.dll', '.pyx'):
continue
if not module_name.startswith(group_name + "_"):
continue
abs_module_name = "%s.%s" % (group_name, module_name)
try:
module = __import__(abs_module_name, fromlist="dummy")
except Exception as e:
modules_in_error.append({
'name': module_name,
'error': str(e),
})
module = None
for benchmark in getattr(module, 'benchmarks', ()):
if callable(benchmark):
collected_benchmarks.append(
(module_filename, benchmark.__name__, benchmark)
)
return benchmark_groups
# =========================
# BENCHMARK EXECUTION
# =========================
def run_benchmark(name, func, args, kwargs, n_runs=5):
def time_once():
tic = time()
func(*args, **kwargs)
return time() - tic
first_timing = time_once()
if isinstance(func, FunctionType):
cold = None
warm = first_timing
all_warm_timings = [first_timing]
else:
other_timings = []
for _ in range(n_runs - 1):
other_timings.append(time_once())
cold = first_timing
warm = np.min(other_timings)
all_warm_timings = other_timings
return OrderedDict([
('name', name),
('cold_time', cold),
('warm_time', warm),
('all_warm_times', all_warm_timings),
('std_warm_times', np.std(all_warm_timings)),
])
def run_benchmarks(folders=None, platforms=None):
collected = find_benchmarks(folders=folders, platforms=platforms)
results = []
for group in collected:
records = []
make_env = group.get('make_env')
args, kwargs = make_env() if make_env else ((), {})
for module_source, name, func in group['benchmarks']:
record = run_benchmark(name, func, args, kwargs)
records.append(record)
results.append({
'group_name': group['name'],
'records': records
})
return results
# =========================
# REPORT
# =========================
def build_report(bench_data,
report_filename=REPORT_FILENAME):
with open(report_filename, "w") as f:
f.write("Report Generated")
# =========================
# CLI
# =========================
def parse_args(args):
parser = argparse.ArgumentParser()
parser.add_argument('--folders', nargs='*', default=None)
parser.add_argument('--platforms', nargs='*', default=None)
return parser.parse_args(args)