Collecting and running benchmarks¶
nnbench provides the BenchmarkRunner
as a compact interface to collect and run benchmarks selectively.
The abstract BenchmarkRunner
class¶
Let's first instantiate and then walk through the base class.
Use the BenchmarkRunner.collect()
method to collect benchmarks from files or directories.
Assume we have the following benchmark setup:
# dir_b/bm2.py
import nnbench
@nnbench.benchmark(tags=("tag",))
def another_benchmark(b: int) -> int:
return b
@nnbench.benchmark
def yet_another_benchmark(c: int) -> int:
return c
# dir_b/bm3.py
import nnbench
@nnbench.benchmark(tags=("tag",))
def the_last_benchmark(d: int) -> int:
return d
Now we can collect benchmarks from files:
Or directories:This collection can happen iteratively. So, after executing the two collections our runner has all four benchmarks ready for execution.
To remove the collected benchmarks again, use the BenchmarkRunner.clear()
method.
You can also supply tags to the runner to selectively collect only benchmarks with the appropriate tag.
For example, after clearing the runner again, you can collect all benchmarks with the "tag"
tag as such:
To run the benchmarks, call the BenchmarkRunner.run()
method and supply the necessary parameters required by the collected benchmarks.