@@ -158,10 +158,11 @@ def instrument_existing_test(
158158 modified_source = re .sub (pattern , replacement , source )
159159
160160 # For performance mode, add timing instrumentation to test methods
161+ # Use original class name (without suffix) in timing markers for consistency with Python
161162 if mode == "performance" :
162163 modified_source = _add_timing_instrumentation (
163164 modified_source ,
164- new_class_name ,
165+ original_class_name , # Use original name in markers, not the renamed class
165166 func_name ,
166167 )
167168
@@ -236,11 +237,18 @@ def _add_timing_instrumentation(source: str, class_name: str, func_name: str) ->
236237 iteration_counter += 1
237238 iter_id = iteration_counter
238239
240+ # Detect indentation from method signature line (line with opening brace)
241+ method_sig_line = method_lines [- 1 ] if method_lines else ""
242+ base_indent = len (method_sig_line ) - len (method_sig_line .lstrip ())
243+ indent = " " * (base_indent + 4 ) # Add one level of indentation
244+
239245 # Add timing start code
240- indent = " "
246+ # Note: CODEFLASH_LOOP_INDEX must always be set - no null check, crash if missing
247+ # Start marker is printed BEFORE timing starts
248+ # System.nanoTime() immediately precedes try block with test code
241249 timing_start_code = [
242250 f"{ indent } // Codeflash timing instrumentation" ,
243- f'{ indent } int _cf_loop{ iter_id } = Integer.parseInt(System.getenv("CODEFLASH_LOOP_INDEX") != null ? System.getenv("CODEFLASH_LOOP_INDEX") : "1" );' ,
251+ f'{ indent } int _cf_loop{ iter_id } = Integer.parseInt(System.getenv("CODEFLASH_LOOP_INDEX"));' ,
244252 f"{ indent } int _cf_iter{ iter_id } = { iter_id } ;" ,
245253 f'{ indent } String _cf_mod{ iter_id } = "{ class_name } ";' ,
246254 f'{ indent } String _cf_cls{ iter_id } = "{ class_name } ";' ,
@@ -274,13 +282,14 @@ def _add_timing_instrumentation(source: str, class_name: str, func_name: str) ->
274282 result .append (" " + bl )
275283
276284 # Add finally block
285+ method_close_indent = " " * base_indent # Same level as method signature
277286 timing_end_code = [
278287 f"{ indent } }} finally {{" ,
279288 f"{ indent } long _cf_end{ iter_id } = System.nanoTime();" ,
280289 f"{ indent } long _cf_dur{ iter_id } = _cf_end{ iter_id } - _cf_start{ iter_id } ;" ,
281290 f'{ indent } System.out.println("!######" + _cf_mod{ iter_id } + ":" + _cf_cls{ iter_id } + ":" + _cf_fn{ iter_id } + ":" + _cf_loop{ iter_id } + ":" + _cf_iter{ iter_id } + ":" + _cf_dur{ iter_id } + "######!");' ,
282291 f"{ indent } }}" ,
283- " }" , # Method closing brace
292+ f" { method_close_indent } } }" , # Method closing brace
284293 ]
285294 result .extend (timing_end_code )
286295 i += 1
@@ -405,10 +414,11 @@ def instrument_generated_java_test(
405414 )
406415
407416 # For performance mode, add timing instrumentation
417+ # Use original class name (without suffix) in timing markers for consistency with Python
408418 if mode == "performance" :
409419 modified_code = _add_timing_instrumentation (
410420 modified_code ,
411- new_class_name ,
421+ original_class_name , # Use original name in markers, not the renamed class
412422 function_name ,
413423 )
414424
0 commit comments