Last active
August 20, 2020 22:12
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Usage: | |
``` | |
from benchmarking import benchmarks | |
with benchmarks("some_name"): | |
time.sleep(10) | |
print(benchmarks.report()) | |
``` | |
""" | |
import time | |
import sys | |
from typing import List, Dict | |
from collections import defaultdict | |
import pprint | |
import math | |
class Benchmark(): | |
def __init__(self, name: str, frame_id: str = None): | |
self.name = name | |
self.frame_id = frame_id | |
self.start = None | |
self.end = None | |
def __enter__(self): | |
self.start = time.time() | |
def __exit__(self, exc_type, exc_value, traceback): | |
self.end = time.time() | |
print(f"{self.name} took {self.get_duration():.3f} seconds") | |
def get_duration(self) -> float: | |
if self.end is None: | |
return None | |
return self.end - self.start | |
class BenchmarkNameReport(): | |
def __init__(self, name: str, durations: List[float]): | |
self.name = name | |
self.durations = durations | |
self.mean, self.std = self.get_stats() | |
def get_stats(self): | |
mean = sum(self.durations) / len(self.durations) | |
var = sum([ ((mean - duration) ** 2) for duration in self.durations]) / len(self.durations) | |
std = math.sqrt(var) | |
return mean, std | |
def __repr__(self): | |
return f"{self.name}: count {len(self.durations)}, mean {self.mean:.3f}, std {self.std:.3f}" | |
class Benchmarks(): | |
def __init__(self): | |
self.benchmarks: List[Benchmark] = [] | |
def __call__(self, name, frame_id=None) -> Benchmark: | |
benchmark = Benchmark(name, frame_id) | |
self.benchmarks.append(benchmark) | |
return benchmark | |
def report(self) -> List[BenchmarkNameReport]: | |
benchmarks_by_name: Dict[str, List[float]] = defaultdict(list) | |
benchmark_name_reports: List[BenchmarkNameReport] = [] | |
for benchmark in self.benchmarks: | |
duration = benchmark.get_duration() | |
if duration is not None: | |
benchmarks_by_name[benchmark.name].append(duration) | |
for name, benchmarks in benchmarks_by_name.items(): | |
benchmark_name_reports.append( | |
BenchmarkNameReport(name, benchmarks)) | |
return benchmark_name_reports | |
benchmarks = Benchmarks() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment