-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_bench.py
More file actions
59 lines (50 loc) · 1.85 KB
/
run_bench.py
File metadata and controls
59 lines (50 loc) · 1.85 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
import fnmatch
import functools
import importlib
import operator
import os
import pkgutil
import tracemalloc
# This is handled via PYTHONPATH at runtime
import elasticapm # type: ignore
import pyperf
import benchmarks
def discover_benchmarks():
for importer, modname, is_pkg in sorted(
pkgutil.iter_modules(benchmarks.__path__), key=operator.itemgetter(1)
):
if modname.startswith("bm_"):
bench_module = importlib.import_module("benchmarks." + modname)
for func_name in sorted(dir(bench_module)):
if func_name.startswith("bench_"):
yield getattr(bench_module, func_name)
def run():
metadata = {}
if "COMMIT_TIMESTAMP" in os.environ:
metadata["timestamp"] = os.environ.get("COMMIT_TIMESTAMP")
metadata["revision"] = os.environ.get("COMMIT_SHA")
metadata["commit_message"] = os.environ.get("COMMIT_MESSAGE").split("\n")[0]
runner = pyperf.Runner(metadata=metadata)
pattern = os.environ.get("BENCH_PATTERN")
args = runner.parse_args()
if args.tracemalloc:
bench_type = "tracemalloc"
elif args.track_memory:
bench_type = "trackmem"
else:
bench_type = "time"
for func in discover_benchmarks():
name = "%s.%s.%s" % (str(func.__module__), func.__name__, bench_type)
if not pattern or fnmatch.fnmatch(name, pattern):
client = None
if hasattr(func, "client_defaults"):
# create the client outside of the benchmarked function
client = elasticapm.Client(**func.client_defaults)
func = functools.partial(func, client=client)
if args.tracemalloc:
tracemalloc.clear_traces()
runner.bench_func(name, func)
if client:
client.close()
if __name__ == "__main__":
run()