Skip to content

Commit 4e8483b

Browse files
committed
created benchmarks for codeflash, modified codeflash-optimize to use codeflash --benchmark
1 parent 28fd746 commit 4e8483b

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

.github/workflows/codeflash-optimize.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ jobs:
6868
id: optimize_code
6969
run: |
7070
source .venv/bin/activate
71-
poetry run codeflash
71+
poetry run codeflash --benchmark
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from argparse import Namespace
2+
from pathlib import Path
3+
4+
from codeflash.context.code_context_extractor import get_code_optimization_context
5+
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
6+
from codeflash.models.models import FunctionParent
7+
from codeflash.optimization.optimizer import Optimizer
8+
9+
10+
def test_benchmark_extract(benchmark)->None:
11+
file_path = Path(__file__).parent.parent.parent.resolve() / "codeflash"
12+
opt = Optimizer(
13+
Namespace(
14+
project_root=file_path.resolve(),
15+
disable_telemetry=True,
16+
tests_root=(file_path / "tests").resolve(),
17+
test_framework="pytest",
18+
pytest_cmd="pytest",
19+
experiment_id=None,
20+
test_project_root=Path.cwd(),
21+
)
22+
)
23+
function_to_optimize = FunctionToOptimize(
24+
function_name="replace_function_and_helpers_with_optimized_code",
25+
file_path=file_path / "optimization" / "function_optimizer.py",
26+
parents=[FunctionParent(name="FunctionOptimizer", type="ClassDef")],
27+
starting_line=None,
28+
ending_line=None,
29+
)
30+
31+
benchmark(get_code_optimization_context,function_to_optimize, opt.args.project_root)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from pathlib import Path
2+
3+
from codeflash.discovery.discover_unit_tests import discover_unit_tests
4+
from codeflash.verification.verification_utils import TestConfig
5+
6+
7+
def test_benchmark_code_to_optimize_test_discovery(benchmark) -> None:
8+
project_path = Path(__file__).parent.parent.parent.resolve() / "code_to_optimize"
9+
tests_path = project_path / "tests" / "pytest"
10+
test_config = TestConfig(
11+
tests_root=tests_path,
12+
project_root_path=project_path,
13+
test_framework="pytest",
14+
tests_project_rootdir=tests_path.parent,
15+
)
16+
benchmark(discover_unit_tests, test_config)
17+
def test_benchmark_codeflash_test_discovery(benchmark) -> None:
18+
project_path = Path(__file__).parent.parent.parent.resolve() / "codeflash"
19+
tests_path = project_path / "tests"
20+
test_config = TestConfig(
21+
tests_root=tests_path,
22+
project_root_path=project_path,
23+
test_framework="pytest",
24+
tests_project_rootdir=tests_path.parent,
25+
)
26+
benchmark(discover_unit_tests, test_config)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from codeflash.models.models import FunctionTestInvocation, InvocationId, TestResults, TestType
2+
from codeflash.verification.parse_test_output import merge_test_results
3+
4+
5+
def generate_test_invocations(count=100):
6+
"""Generate a set number of test invocations for benchmarking."""
7+
test_results_xml = TestResults()
8+
test_results_bin = TestResults()
9+
10+
# Generate test invocations in a loop
11+
for i in range(count):
12+
iteration_id = str(i * 3 + 5) # Generate unique iteration IDs
13+
14+
# XML results - some with None runtime
15+
test_results_xml.add(
16+
FunctionTestInvocation(
17+
id=InvocationId(
18+
test_module_path="code_to_optimize.tests.unittest.test_bubble_sort",
19+
test_class_name="TestPigLatin",
20+
test_function_name="test_sort",
21+
function_getting_tested="sorter",
22+
iteration_id=iteration_id,
23+
),
24+
file_name="/tmp/tests/unittest/test_bubble_sort__perfinstrumented.py",
25+
did_pass=True,
26+
runtime=None if i % 3 == 0 else i * 100, # Vary runtime values
27+
test_framework="unittest",
28+
test_type=TestType.EXISTING_UNIT_TEST,
29+
return_value=None,
30+
timed_out=False,
31+
loop_index=i,
32+
)
33+
)
34+
35+
# Binary results - with actual runtime values
36+
test_results_bin.add(
37+
FunctionTestInvocation(
38+
id=InvocationId(
39+
test_module_path="code_to_optimize.tests.unittest.test_bubble_sort",
40+
test_class_name="TestPigLatin",
41+
test_function_name="test_sort",
42+
function_getting_tested="sorter",
43+
iteration_id=iteration_id,
44+
),
45+
file_name="/tmp/tests/unittest/test_bubble_sort__perfinstrumented.py",
46+
did_pass=True,
47+
runtime=500 + i * 20, # Generate varying runtime values
48+
test_framework="unittest",
49+
test_type=TestType.EXISTING_UNIT_TEST,
50+
return_value=None,
51+
timed_out=False,
52+
loop_index=i,
53+
)
54+
)
55+
56+
return test_results_xml, test_results_bin
57+
58+
59+
def run_merge_benchmark(count=100):
60+
test_results_xml, test_results_bin = generate_test_invocations(count)
61+
62+
# Perform the merge operation that will be benchmarked
63+
merge_test_results(
64+
xml_test_results=test_results_xml,
65+
bin_test_results=test_results_bin,
66+
test_framework="unittest"
67+
)
68+
69+
70+
def test_benchmark_merge_test_results(benchmark):
71+
benchmark(run_merge_benchmark, 1000) # Default to 100 test invocations

0 commit comments

Comments
 (0)