Skip to content

Commit 48967e6

Browse files
authored
Merge pull request #161 from codeflash-ai/benchmark-docs
first draft
2 parents 761f1ba + 3daf445 commit 48967e6

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
# Using Benchmarks
5+
6+
Codeflash is able the determine the impact of an optimization on predefined benchmarks, when used in benchmark mode.
7+
8+
Benchmark mode is an easy way for users to define workflows that are performance-critical and need to be optimized.
9+
For example, if a user has an important function that requires minimal latency, the user can define a benchmark for that function.
10+
Codeflash will then calculate the impact (if any) of any optimization on the performance of that function.
11+
12+
## Using Codeflash in Benchmark Mode
13+
14+
1. **Create a benchmarks root**
15+
16+
Create a directory for benchmarks. This directory must be a sub directory of your tests directory.
17+
18+
In your pyproject.toml, add the path to the 'benchmarks-root' section.
19+
```yaml
20+
[tool.codeflash]
21+
# All paths are relative to this pyproject.toml's directory.
22+
module-root = "inference"
23+
tests-root = "tests"
24+
test-framework = "pytest"
25+
benchmarks-root = "tests/benchmarks" # add your benchmarks root dir here
26+
ignore-paths = []
27+
formatter-cmds = ["disabled"]
28+
```
29+
30+
2. **Define your benchmarks**
31+
32+
Currently, Codeflash only supports benchmarks written as pytest-benchmarks. Check out the [pytest-benchmark](https://pytest-benchmark.readthedocs.io/en/stable/index.html) documentation for more information on syntax.
33+
34+
For example:
35+
36+
```python
37+
from core.bubble_sort import sorter
38+
39+
def test_sort(benchmark):
40+
result = benchmark(sorter, list(reversed(range(500))))
41+
assert result == list(range(500))
42+
```
43+
44+
Note that these benchmarks should be defined in such a way that they don't take a long time to run.
45+
46+
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
47+
48+
49+
3. **Run Codeflash**
50+
51+
Run Codeflash with the `--benchmark` flag. Note that benchmark mode cannot be used with `--all`.
52+
53+
```bash
54+
codeflash --file test_file.py --benchmark
55+
```
56+
57+
If you did not define your benchmarks-root in your pyproject.toml, you can do:
58+
59+
```bash
60+
codeflash --file test_file.py --benchmark --benchmarks-root path/to/benchmarks
61+
```
62+
63+
64+
4. **Run Codeflash in CI**
65+
66+
Benchmark mode is best used together with Codeflash as a Github Action. This way, with every PR, you will know the impact of Codeflash's optimizations on your benchmarks.
67+
68+
Use `codeflash init` for an easy way to set up Codeflash as a Github Action (with the option to enable benchmark mode).
69+
70+
71+
72+
## How it works
73+
74+
1. Codeflash identifies benchmarks in the benchmarks-root directory.
75+
76+
77+
2. The benchmarks are run so that runtime statistics and information can be recorded.
78+
79+
80+
3. Replay tests are generated so the performance of optimization candidates on the exact inputs used in the benchmarks can be measured.
81+
82+
83+
4. If an optimization candidate is verified to be correct, the speedup of the optimization is calculated for each benchmark.
84+
85+
86+
5. Codeflash then reports the impact of the optimization on each benchmark.
87+
88+
89+
Using Codeflash with benchmarks is a great way to find optimizations that really matter.
90+
91+
Codeflash is actively working on this feature and will be adding new capabilities in the near future!

0 commit comments

Comments
 (0)