|
| 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