-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
101 lines (77 loc) · 2.72 KB
/
Copy pathbenchmark.py
File metadata and controls
101 lines (77 loc) · 2.72 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
import os
# os.environ["MATH_BACKEND"] = "python"
# os.environ["RAYTRACING_BACKEND"] = "python"
# os.environ["MATH_BACKEND"] = "rust"
# os.environ["RAYTRACING_BACKEND"] = "python"
os.environ["MATH_BACKEND"] = "rust"
os.environ["RAYTRACING_BACKEND"] = "rust"
from pathlib import Path
from time import perf_counter_ns
from matplotlib import pyplot as plt
import numpy as np
from scipy import stats
from backend_wrapper import Vector3, Camera, Sphere
CAMERA = Camera(
light_source=Vector3(-4.0, -4.0, 5.0),
screen_center=Vector3(0, 0.0, 0.0),
screen_direction=Vector3(0.0, 0.0, 1.0),
screen_width_pixels=80,
screen_height_pixels=60,
screen_width=16.0,
screen_height=10.0,
)
SPHERE = Sphere(center=Vector3(0.0, 0.0, 3.0), radius=1.0)
def bench_function(num_evals: int, n_warmup: int, func, *args, **kwargs):
print("Warming up...")
for _ in range(n_warmup):
func(*args, **kwargs)
eval_times = []
for evaluation_idx in range(num_evals):
print(f"Running evaluation {evaluation_idx}/{num_evals}")
start = perf_counter_ns()
func(*args, **kwargs)
end = perf_counter_ns()
dt_ms = (end - start) / 1_000_000.0
eval_times.append(dt_ms)
return eval_times
def plot_bench_results(eval_times, title, save_path: Path):
"""
Plot the benchmark results as a histogram + KDE
:param eval_times: list of evaluation times
:param title: title of the plot
"""
fig, ax = plt.subplots()
ax.set_title(title)
# Create a histogram with the eval times
n_bins = int(1 + np.ceil(np.log2(len(eval_times)))) # sturge's rule
ax.hist(
eval_times, bins=n_bins, density=True, alpha=0.6, color="g", label="Histogram"
)
ax.set_xlabel("Time (ms)")
# Overlay a KDE
kernel = stats.gaussian_kde(eval_times)
time_dom = np.linspace(min(eval_times), max(eval_times), 1_000)
ax.plot(time_dom, kernel(time_dom), "r", label="KDE")
ax.set_ylabel("Density")
# Vertical Line with the median
median_time = np.median(eval_times)
ax.axvline(
median_time, color="k", linestyle="--", label=f"Median: {median_time:.2}ms"
)
# Create Figure
ax.legend()
plt.savefig(save_path)
def bench_raytracing(num_evals: int, n_warmup: int, figure_name: str):
def benched_function():
rgbs = CAMERA.fill_screen(SPHERE)
eval_times = bench_function(num_evals, n_warmup, func=benched_function)
plot_bench_results(
eval_times,
title="Ray Tracing Benchmark",
save_path=Path(__file__).parent
/ "results_report"
/ "figures"
/ f"{figure_name}.svg",
)
if __name__ == "__main__":
bench_raytracing(num_evals=200, n_warmup=20, figure_name="rs_benchmark")