| 1 |
import enum |
| 2 |
import numbers |
| 3 |
import os |
| 4 |
import subprocess |
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
|
| 9 |
cur_dir = os.getcwd() |
| 10 |
toml_path = os.path.join(cur_dir, "Cargo.toml") |
| 11 |
|
| 12 |
if not os.getcwd().endswith("fast_qr") or not os.path.exists(toml_path): |
| 13 |
toml_path = "../Cargo.toml" |
| 14 |
|
| 15 |
|
| 16 |
try: |
| 17 |
cargo_bench = subprocess.run( |
| 18 |
["cargo", "bench", "--manifest-path", toml_path, "--no-run"] |
| 19 |
) |
| 20 |
except Exception as e: |
| 21 |
print("An error occurred while building cargo bench") |
| 22 |
print(e) |
| 23 |
exit(1) |
| 24 |
|
| 25 |
try: |
| 26 |
cargo_bench = subprocess.check_output( |
| 27 |
["cargo", "bench", "--manifest-path", toml_path], stderr=subprocess.STDOUT |
| 28 |
) |
| 29 |
except Exception as e: |
| 30 |
print("An error occurred while running cargo bench") |
| 31 |
print(e) |
| 32 |
exit(1) |
| 33 |
|
| 34 |
|
| 35 |
lines = list(map(lambda x: x.decode("utf-8"), cargo_bench.splitlines())) |
| 36 |
|
| 37 |
analyze_regex = re.compile(r"^Benchmarking (.*): Analyzing$", re.M | re.I) |
| 38 |
benchmark_names = analyze_regex.findall("\n".join(lines)) |
| 39 |
|
| 40 |
time_lines = [] |
| 41 |
|
| 42 |
for line in lines: |
| 43 |
for benchmark in benchmark_names: |
| 44 |
if line.startswith(benchmark): |
| 45 |
time_lines.append(line) |
| 46 |
break |
| 47 |
|
| 48 |
time_lines.sort() |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
number = re.compile(r"(\d+(?:\.\d*)?\s*(?:ms|ns|µs|s))") |
| 53 |
|
| 54 |
def print_bench(benchmark_name: str, lower: str, estimate: str, upper: str, ratio=" " * len('fast_qr is 10.16x faster')): |
| 55 |
print( |
| 56 |
f"| {benchmark_name:<24} | {lower:<9} | {estimate:<9} | {upper:<9} | {ratio:<24} |" |
| 57 |
) |
| 58 |
|
| 59 |
print_bench("Benchmark", "Lower", "Estimate", "Upper", "Ratio") |
| 60 |
print_bench(":--", ":--:", ":--:", ":--:", "--") |
| 61 |
|
| 62 |
def number_with_unit(s): |
| 63 |
if s.endswith("ns"): |
| 64 |
return float(s) * 1000000000 |
| 65 |
if s.endswith("ms"): |
| 66 |
return float(s[:-2]) * 1000000 |
| 67 |
elif s.endswith("µs"): |
| 68 |
return float(s[:-2]) * 1000 |
| 69 |
elif s.endswith("s"): |
| 70 |
return float(s[:-1]) |
| 71 |
else: |
| 72 |
raise "Not a valid number" |
| 73 |
|
| 74 |
|
| 75 |
for i in range(0, len(time_lines), 2): |
| 76 |
test1, time1 = time_lines[i + 0].split("time:") |
| 77 |
test2, time2 = time_lines[i + 1].split("time:") |
| 78 |
|
| 79 |
test1, test2 = test1.strip(), test2.strip() |
| 80 |
|
| 81 |
data1 = number.findall(time1) |
| 82 |
data2 = number.findall(time2) |
| 83 |
|
| 84 |
data1_number = list(map(number_with_unit, data1)) |
| 85 |
data2_number = list(map(number_with_unit, data2)) |
| 86 |
|
| 87 |
ratio = data2_number[1] / data1_number[1] |
| 88 |
print_bench(test2, data2[0], data2[1], data2[2], "") |
| 89 |
print_bench(test1, data1[0], data1[1], data1[2], f"fast_qr is {ratio:.2f}x faster") |
| 90 |
|
| 91 |
import platform |
| 92 |
|
| 93 |
|
| 94 |
print(f"- System: {platform.system()}") |
| 95 |
print(f"- Machine: {platform.machine()}") |
| 96 |
print(f"- Processor: {platform.processor()}") |
| 97 |
|
| 98 |
|
| 99 |
print() |
| 100 |
print( |
| 101 |
"Benchmarking powered by [Criterion.rs](https://github.com/bheisler/criterion.rs). \\\n" |
| 102 |
"Feel free to run some benchmarkings yourself!\n\n" |
| 103 |
) |