55import os
66class CodeFlashBenchmarkPlugin :
77 benchmark_timings = []
8+
9+ class Benchmark :
10+ def __init__ (self , request ):
11+ self .request = request
12+
13+ def __call__ (self , func , * args , ** kwargs ):
14+ benchmark_file_name = self .request .node .fspath .basename
15+ benchmark_function_name = self .request .node .name
16+ line_number = str (sys ._getframe (1 ).f_lineno ) # 1 frame up in the call stack
17+
18+ os .environ ["CODEFLASH_BENCHMARK_FUNCTION_NAME" ] = benchmark_function_name
19+ os .environ ["CODEFLASH_BENCHMARK_FILE_NAME" ] = benchmark_file_name
20+ os .environ ["CODEFLASH_BENCHMARK_LINE_NUMBER" ] = line_number
21+ os .environ ["CODEFLASH_BENCHMARKING" ] = "True"
22+
23+ start = time .perf_counter_ns ()
24+ result = func (* args , ** kwargs )
25+ end = time .perf_counter_ns ()
26+
27+ os .environ ["CODEFLASH_BENCHMARKING" ] = "False"
28+ CodeFlashBenchmarkPlugin .benchmark_timings .append (
29+ (benchmark_file_name , benchmark_function_name , line_number , end - start ))
30+ return result
831 @staticmethod
932 def pytest_addoption (parser ):
1033 parser .addoption (
@@ -36,23 +59,4 @@ def benchmark(request):
3659 if not request .config .getoption ("--codeflash-trace" ):
3760 return None
3861
39- class Benchmark :
40-
41- def __call__ (self , func , * args , ** kwargs ):
42- benchmark_file_name = request .node .fspath .basename
43- benchmark_function_name = request .node .name
44- line_number = str (sys ._getframe (1 ).f_lineno ) # 1 frame up in the call stack
45- os .environ ["CODEFLASH_BENCHMARK_FUNCTION_NAME" ] = benchmark_function_name
46- os .environ ["CODEFLASH_BENCHMARK_FILE_NAME" ] = benchmark_file_name
47- os .environ ["CODEFLASH_BENCHMARK_LINE_NUMBER" ] = line_number
48- os .environ ["CODEFLASH_BENCHMARKING" ] = "True"
49-
50- start = time .perf_counter_ns ()
51- result = func (* args , ** kwargs )
52- end = time .perf_counter_ns ()
53-
54- os .environ ["CODEFLASH_BENCHMARKING" ] = "False"
55- CodeFlashBenchmarkPlugin .benchmark_timings .append ((benchmark_file_name , benchmark_function_name , line_number , end - start ))
56- return result
57-
58- return Benchmark ()
62+ return CodeFlashBenchmarkPlugin .Benchmark (request )
0 commit comments