Skip to content

Commit 0f1b098

Browse files
⚡️ Speed up method CodeFlashBenchmarkPlugin.write_benchmark_timings by 167% in PR #59 (codeflash-trace-decorator)
Here is the optimized version of the given Python program. 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.
1 parent 5a34697 commit 0f1b098

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

codeflash/benchmarking/plugin/plugin.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from __future__ import annotations
2+
23
import os
34
import sqlite3
45
import sys
56
import time
67
from pathlib import Path
8+
79
import pytest
10+
811
from codeflash.benchmarking.codeflash_trace import codeflash_trace
912
from codeflash.models.models import BenchmarkKey
1013

@@ -15,7 +18,7 @@ def __init__(self) -> None:
1518
self._connection = None
1619
self.benchmark_timings = []
1720

18-
def setup(self, trace_path:str) -> None:
21+
def setup(self, trace_path: str) -> None:
1922
try:
2023
# Open connection
2124
self._trace_path = trace_path
@@ -28,7 +31,7 @@ def setup(self, trace_path:str) -> None:
2831
"benchmark_time_ns INTEGER)"
2932
)
3033
self._connection.commit()
31-
self.close() # Reopen only at the end of pytest session
34+
self.close() # Reopen only at the end of pytest session
3235
except Exception as e:
3336
print(f"Database setup error: {e}")
3437
if self._connection:
@@ -42,20 +45,23 @@ def write_benchmark_timings(self) -> None:
4245

4346
if self._connection is None:
4447
self._connection = sqlite3.connect(self._trace_path)
48+
self._connection.execute("PRAGMA synchronous = OFF")
49+
self._connection.execute("PRAGMA journal_mode = MEMORY")
4550

4651
try:
4752
cur = self._connection.cursor()
4853
# Insert data into the benchmark_timings table
4954
cur.executemany(
5055
"INSERT INTO benchmark_timings (benchmark_file_path, benchmark_function_name, benchmark_line_number, benchmark_time_ns) VALUES (?, ?, ?, ?)",
51-
self.benchmark_timings
56+
self.benchmark_timings,
5257
)
5358
self._connection.commit()
54-
self.benchmark_timings = [] # Clear the benchmark timings list
59+
self.benchmark_timings = [] # Clear the benchmark timings list
5560
except Exception as e:
5661
print(f"Error writing to benchmark timings database: {e}")
5762
self._connection.rollback()
5863
raise
64+
5965
def close(self) -> None:
6066
if self._connection:
6167
self._connection.close()
@@ -189,12 +195,7 @@ def pytest_sessionfinish(self, session, exitstatus):
189195

190196
@staticmethod
191197
def pytest_addoption(parser):
192-
parser.addoption(
193-
"--codeflash-trace",
194-
action="store_true",
195-
default=False,
196-
help="Enable CodeFlash tracing"
197-
)
198+
parser.addoption("--codeflash-trace", action="store_true", default=False, help="Enable CodeFlash tracing")
198199

199200
@staticmethod
200201
def pytest_plugin_registered(plugin, manager):
@@ -246,7 +247,7 @@ def test_something(benchmark):
246247
os.environ["CODEFLASH_BENCHMARK_LINE_NUMBER"] = str(line_number)
247248
os.environ["CODEFLASH_BENCHMARKING"] = "True"
248249

249-
# Run the function
250+
# Run the function
250251
start = time.perf_counter_ns()
251252
result = func(*args, **kwargs)
252253
end = time.perf_counter_ns()
@@ -260,7 +261,8 @@ def test_something(benchmark):
260261
codeflash_trace.function_call_count = 0
261262
# Add to the benchmark timings buffer
262263
codeflash_benchmark_plugin.benchmark_timings.append(
263-
(benchmark_file_path, benchmark_function_name, line_number, end - start))
264+
(benchmark_file_path, benchmark_function_name, line_number, end - start)
265+
)
264266

265267
return result
266268

@@ -272,4 +274,5 @@ def benchmark(request):
272274

273275
return CodeFlashBenchmarkPlugin.Benchmark(request)
274276

275-
codeflash_benchmark_plugin = CodeFlashBenchmarkPlugin()
277+
278+
codeflash_benchmark_plugin = CodeFlashBenchmarkPlugin()

0 commit comments

Comments
 (0)