@@ -154,7 +154,7 @@ def _format_stats(title, data, p50, p90, p99, fmt):
154154{ _format_stats ("Physical RSS RAM (MB)" , rss_memories , p50_rss , p90_rss , p99_rss , ".4f" )} """
155155 print (final_output .strip ())
156156
157- def run_master (iterations , target_module , cpu = 0 , csv_path = None , clear_cache = True , fail_threshold = None ):
157+ def run_master (iterations , target_module , cpu = 0 , csv_path = None , clear_cache = True , fail_threshold = None , diff_baseline = None , diff_threshold = None ):
158158 """Orchestrates the benchmark."""
159159 if iterations < 1 :
160160 raise ValueError ("Number of iterations must be at least 1." )
@@ -229,12 +229,39 @@ def run_master(iterations, target_module, cpu=0, csv_path=None, clear_cache=True
229229 rss_memories , p50_rss , p90_rss , p99_rss
230230 )
231231
232- if fail_threshold is not None :
233- if p99_time > fail_threshold :
234- print (f"\n FAILURE: P99 import time ({ p99_time :.2f} ms) exceeds the failure threshold ({ fail_threshold } ms)." , file = sys .stderr )
235- sys .exit (1 )
236- else :
237- print (f"\n SUCCESS: P99 import time ({ p99_time :.2f} ms) is within the failure threshold ({ fail_threshold } ms)." )
232+ if fail_threshold is not None :
233+ if p99_time > fail_threshold :
234+ print (f"
235+ FAILURE: P99 import time ({ p99_time :.2f} ms) exceeds the failure threshold ({ fail_threshold } ms)." , file = sys .stderr )
236+ sys .exit (1 )
237+ else :
238+ print (f"
239+ SUCCESS: P99 import time ({ p99_time :.2f} ms) is within the failure threshold ({ fail_threshold } ms)." )
240+
241+ if diff_baseline :
242+ if os .path .exists (diff_baseline ):
243+ baseline_times = []
244+ with open (diff_baseline , "r" , encoding = "utf-8" ) as f :
245+ reader = csv .reader (f )
246+ next (reader ) # skip header
247+ for row in reader :
248+ baseline_times .append (float (row [1 ]))
249+ _ , _ , baseline_p99 = _calculate_percentiles (baseline_times )
250+ diff = p99_time - baseline_p99
251+
252+ print (f"
253+ --- Diff vs Baseline ---" )
254+ print (f"Baseline P99: { baseline_p99 :.2f} ms" )
255+ print (f"Current P99: { p99_time :.2f} ms" )
256+ print (f"Difference: { diff :+.2f} ms" )
257+
258+ if diff > diff_threshold :
259+ print (f"FAILURE: Import time regression of { diff :.2f} ms exceeds the allowed threshold of { diff_threshold } ms." , file = sys .stderr )
260+ sys .exit (1 )
261+ else :
262+ print ("SUCCESS: Import time diff is within acceptable thresholds." )
263+ else :
264+ print (f"WARNING: Baseline CSV { diff_baseline } not found. Skipping diff check." )
238265
239266
240267def run_trace (target_module ):
@@ -342,6 +369,8 @@ def find_module_from_package(pkg):
342369 parser .add_argument ("--mprofile" , action = "store_true" , help = "Run tracemalloc memory snapshot" )
343370 parser .add_argument ("--keep-pycache" , action = "store_true" , help = "Preserve __pycache__ and allow bytecode execution (Default: False, script automatically sweeps __pycache__ for true cold-starts)" )
344371 parser .add_argument ("--fail-threshold" , type = float , help = "Fail the profiling if the P99 time exceeds this threshold (in ms)." )
372+ parser .add_argument ("--diff-baseline" , help = "Path to a baseline CSV file to compare against." )
373+ parser .add_argument ("--diff-threshold" , type = float , default = 100.0 , help = "Fail if P99 time exceeds baseline P99 by this many ms." )
345374 parser .add_argument ("--worker" , action = "store_true" , help = argparse .SUPPRESS )
346375
347376 args = parser .parse_args ()
@@ -362,4 +391,4 @@ def find_module_from_package(pkg):
362391 if not args .keep_pycache : clean_bytecode ()
363392 run_mprofile (target_module )
364393 else :
365- run_master (args .iterations , target_module , args .cpu , args .csv , not args .keep_pycache , args .fail_threshold )
394+ run_master (args .iterations , target_module , args .cpu , args .csv , not args .keep_pycache , args .fail_threshold , args . diff_baseline , args . diff_threshold )
0 commit comments