Performance-critical optimization - Define benchmarks for your most important code sections and let Codeflash optimize and measure the real-world impact of every optimization on your performance metrics.
Using Codeflash in Benchmark Mode
-
Create a benchmarks root:
Create a directory for benchmarks if it does not already exist.
In your pyproject.toml, add the path to the ‘benchmarks-root’ section.
-
Define your benchmarks:
Currently, Codeflash only supports benchmarks written as pytest-benchmarks. Check out the pytest-benchmark documentation for more information on syntax.
For example:
Note that these benchmarks should be defined in such a way that they don’t take a long time to run. The pytest-benchmark format is simply used as an interface. The plugin is actually not used - Codeflash will run these benchmarks with its own pytest plugin
-
Run and Test Codeflash:
Run Codeflash with the
--benchmark
flag. Note that benchmark mode cannot be used with--all
.If you did not define your benchmarks-root in your pyproject.toml, you can do: -
Run Codeflash :
Benchmark mode is best used together with Codeflash as a GitHub Action. This way,
Codeflash will trace through your benchmark and optimize the functions modified in your Pull Request to speed up the benchmark.
It will also report the impact of Codeflash’s optimizations on your benchmarks.
Use
codeflash init
for an easy way to set up Codeflash as a GitHub Action. After that, you can add the--benchmark
argument to codeflash to enable benchmarks optimization.
How it works
- Codeflash identifies benchmarks in the benchmarks-root directory.
- The benchmarks are run so that runtime statistics and inputs can be recorded.
- Replay tests are generated so the performance of optimization candidates on the exact inputs used in the benchmarks can be measured.
- If an optimization candidate is verified to be correct, the speedup of the optimization is calculated for each benchmark.
- Codeflash then reports the impact of the optimization on each benchmark.