From 0f1b098c265d8105d78c7a2fc9c563fcfae7079a Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 1 Apr 2025 00:15:21 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20method=20`Cod?= =?UTF-8?q?eFlashBenchmarkPlugin.write=5Fbenchmark=5Ftimings`=20by=20167%?= =?UTF-8?q?=20in=20PR=20#59=20(`codeflash-trace-decorator`)=20Here=20is=20?= =?UTF-8?q?the=20optimized=20version=20of=20the=20given=20Python=20program?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes made. 1. Added PRAGMA statements (`PRAGMA synchronous = OFF` and `PRAGMA journal_mode = MEMORY`) after establishing the database connection. These settings help in improving the write performance by reducing the disk I/O operations. However, note that setting `PRAGMA synchronous = OFF` and `PRAGMA journal_mode = MEMORY` makes the database more vulnerable to corruption in the event of an application crash or power failure. Use these optimizations only when performance is critical, and the database is not mission-critical. --- codeflash/benchmarking/plugin/plugin.py | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/codeflash/benchmarking/plugin/plugin.py b/codeflash/benchmarking/plugin/plugin.py index 09858601c..b969959e7 100644 --- a/codeflash/benchmarking/plugin/plugin.py +++ b/codeflash/benchmarking/plugin/plugin.py @@ -1,10 +1,13 @@ from __future__ import annotations + import os import sqlite3 import sys import time from pathlib import Path + import pytest + from codeflash.benchmarking.codeflash_trace import codeflash_trace from codeflash.models.models import BenchmarkKey @@ -15,7 +18,7 @@ def __init__(self) -> None: self._connection = None self.benchmark_timings = [] - def setup(self, trace_path:str) -> None: + def setup(self, trace_path: str) -> None: try: # Open connection self._trace_path = trace_path @@ -28,7 +31,7 @@ def setup(self, trace_path:str) -> None: "benchmark_time_ns INTEGER)" ) self._connection.commit() - self.close() # Reopen only at the end of pytest session + self.close() # Reopen only at the end of pytest session except Exception as e: print(f"Database setup error: {e}") if self._connection: @@ -42,20 +45,23 @@ def write_benchmark_timings(self) -> None: if self._connection is None: self._connection = sqlite3.connect(self._trace_path) + self._connection.execute("PRAGMA synchronous = OFF") + self._connection.execute("PRAGMA journal_mode = MEMORY") try: cur = self._connection.cursor() # Insert data into the benchmark_timings table cur.executemany( "INSERT INTO benchmark_timings (benchmark_file_path, benchmark_function_name, benchmark_line_number, benchmark_time_ns) VALUES (?, ?, ?, ?)", - self.benchmark_timings + self.benchmark_timings, ) self._connection.commit() - self.benchmark_timings = [] # Clear the benchmark timings list + self.benchmark_timings = [] # Clear the benchmark timings list except Exception as e: print(f"Error writing to benchmark timings database: {e}") self._connection.rollback() raise + def close(self) -> None: if self._connection: self._connection.close() @@ -189,12 +195,7 @@ def pytest_sessionfinish(self, session, exitstatus): @staticmethod def pytest_addoption(parser): - parser.addoption( - "--codeflash-trace", - action="store_true", - default=False, - help="Enable CodeFlash tracing" - ) + parser.addoption("--codeflash-trace", action="store_true", default=False, help="Enable CodeFlash tracing") @staticmethod def pytest_plugin_registered(plugin, manager): @@ -246,7 +247,7 @@ def test_something(benchmark): os.environ["CODEFLASH_BENCHMARK_LINE_NUMBER"] = str(line_number) os.environ["CODEFLASH_BENCHMARKING"] = "True" - # Run the function + # Run the function start = time.perf_counter_ns() result = func(*args, **kwargs) end = time.perf_counter_ns() @@ -260,7 +261,8 @@ def test_something(benchmark): codeflash_trace.function_call_count = 0 # Add to the benchmark timings buffer codeflash_benchmark_plugin.benchmark_timings.append( - (benchmark_file_path, benchmark_function_name, line_number, end - start)) + (benchmark_file_path, benchmark_function_name, line_number, end - start) + ) return result @@ -272,4 +274,5 @@ def benchmark(request): return CodeFlashBenchmarkPlugin.Benchmark(request) -codeflash_benchmark_plugin = CodeFlashBenchmarkPlugin() \ No newline at end of file + +codeflash_benchmark_plugin = CodeFlashBenchmarkPlugin()