3535
3636
3737def _SetExtraBenchmarkFlags (
38- benchmark_perf_counters : str ,
38+ benchmark_perf_counters : list [ str ] ,
3939 benchmark_repetitions : int ,
4040 benchmark_min_time : str ,
4141) -> list [str ]:
4242 """Set extra benchmark flags."""
4343 benchmark_flags = []
4444 if benchmark_perf_counters :
45- benchmark_flags .append (
46- f"--benchmark_perf_counters={ benchmark_perf_counters } "
47- )
45+ perf_counters_str = "," .join (benchmark_perf_counters )
46+ benchmark_flags .append (f"--benchmark_perf_counters={ perf_counters_str } " )
4847 if benchmark_min_time :
4948 benchmark_flags .append (f"--benchmark_min_time={ benchmark_min_time } " )
5049 if benchmark_repetitions :
@@ -82,6 +81,8 @@ class ParallelBench:
8281 temp_root: Child directory to store results in. It is in the format of
8382 "temp_parent_root/run_{repetition_id}".
8483 keep_raw_data: Whether to keep the raw results from each run.
84+ perf_counters: A list of performance counters to collect during benchmark
85+ run.
8586 runtimes: Dictionary of benchmark name -> history of benchmark runtimes.
8687 workers: Dictionary of CPU ID -> Worker thread.
8788 results: List of results from all runs.
@@ -90,7 +91,6 @@ class ParallelBench:
9091 target_ratios: List of target ratios for each benchmark. This is used to
9192 calculate the probability of each benchmark being selected, and determined
9293 by the benchmark weights.
93- keep_raw_data: Whether to keep the raw results from each run.
9494 first_run: Boolean indicating if this is the first run. We use this to
9595 determine if we can randomly select benchmarks or if we need to run all
9696 benchmarks at least once.
@@ -105,6 +105,7 @@ def __init__(
105105 repetitions : int ,
106106 temp_parent_root : str ,
107107 keep_raw_data : bool ,
108+ benchmark_perf_counters : str ,
108109 ):
109110 """Initialize the parallel benchmark runner."""
110111
@@ -123,6 +124,9 @@ def __init__(
123124 self .temp_parent_root = temp_parent_root
124125 self .temp_root = ""
125126 self .keep_raw_data = keep_raw_data
127+ self .perf_counters : list [str ] = (
128+ benchmark_perf_counters .split ("," ) if benchmark_perf_counters else []
129+ )
126130 self .runtimes : dict [str , list [BenchmarkMetrics ]] = {}
127131 self .workers : dict [int , worker .Worker ] = {}
128132 self .utilization_samples : list [tuple [pd .Timestamp , float ]] = []
@@ -161,7 +165,6 @@ def SetWeights(
161165
162166 def _PreRun (
163167 self ,
164- benchmark_perf_counters : str ,
165168 benchmark_repetitions : int ,
166169 benchmark_min_time : str ,
167170 repetition : int ,
@@ -171,7 +174,7 @@ def _PreRun(
171174 logging .info ("Initializing benchmarks and worker threads..." )
172175
173176 benchmark_flags = _SetExtraBenchmarkFlags (
174- benchmark_perf_counters , benchmark_repetitions , benchmark_min_time
177+ self . perf_counters , benchmark_repetitions , benchmark_min_time
175178 )
176179
177180 if benchmark_flags :
@@ -395,24 +398,16 @@ def _RunSchedulingLoop(self) -> None:
395398 )
396399 )
397400
398- def GeneratePerfCounterDataFrame (
399- self , benchmark_perf_counters : str
400- ) -> pd .DataFrame | None :
401+ def GeneratePerfCounterDataFrame (self ) -> pd .DataFrame | None :
401402 """Generates a DataFrame of performance counter results for each benchmark.
402403
403- Args:
404- benchmark_perf_counters: A comma-separated list of performance counters to
405- collect.
406-
407404 Returns:
408405 A DataFrame of performance counter results for each benchmark, or None if
409406 no performance counters were specified.
410407 """
411- if not benchmark_perf_counters :
408+ if not self . perf_counters :
412409 return None
413410
414- counters = benchmark_perf_counters .split ("," )
415-
416411 performance_data = []
417412 for filename in os .listdir (self .temp_root ):
418413
@@ -426,7 +421,7 @@ def GeneratePerfCounterDataFrame(
426421 entry = {
427422 "Benchmark" : benchmark_result ["name" ],
428423 }
429- for counter in counters :
424+ for counter in self . perf_counters :
430425 if counter in benchmark_result :
431426 entry [counter ] = benchmark_result [counter ]
432427 performance_data .append (entry )
@@ -438,7 +433,7 @@ def GeneratePerfCounterDataFrame(
438433 # Group the results by benchmark and counter, and calculate the mean of each
439434 # counter for each benchmark.
440435 aggregations = {}
441- for counter in counters :
436+ for counter in self . perf_counters :
442437 aggregations [counter ] = pd .NamedAgg (column = counter , aggfunc = np .mean )
443438
444439 perf_counters_results = (
@@ -481,25 +476,20 @@ def _RemoveRawData(self) -> None:
481476 logging .exception ("Failed to remove %s: %s" , file_path , e )
482477
483478 def PostProcessBenchmarkResults (
484- self , benchmark_perf_counters : str
479+ self ,
485480 ) -> tuple [reporter .ContextInfo , reporter .BenchmarkRuntimeInfo ]:
486481 """Generate benchmark reports and save results to a JSON file.
487482
488483 If benchmark_perf_counters is specified, the report will include perf
489484 counters for each benchmark. We will also check to see if we want to remove
490485 the raw data.
491-
492- Args:
493- benchmark_perf_counters: A comma-separated list of performance counters to
494- collect.
495-
496486 Returns:
497487 A tuple containing the context and benchmark data dictionaries.
498488 """
499489
500490 df = self .ConvertToDataFrame ()
501491
502- perf_counter_df = self .GeneratePerfCounterDataFrame (benchmark_perf_counters )
492+ perf_counter_df = self .GeneratePerfCounterDataFrame ()
503493 df = reporter .GenerateBenchmarkReport (df , perf_counter_df )
504494 context , data = reporter .SaveBenchmarkResults (self .temp_root , df )
505495
@@ -509,7 +499,6 @@ def PostProcessBenchmarkResults(
509499
510500 def Run (
511501 self ,
512- benchmark_perf_counters : str = "" ,
513502 benchmark_repetitions : int = 0 ,
514503 benchmark_min_time : str = "" ,
515504 ):
@@ -526,7 +515,6 @@ def Run(
526515 print (f"Running trial { i } ......." )
527516
528517 self ._PreRun (
529- benchmark_perf_counters ,
530518 benchmark_repetitions ,
531519 benchmark_min_time ,
532520 i ,
@@ -561,7 +549,7 @@ def Run(
561549 w .join ()
562550
563551 # Post-process benchmark results
564- context , data = self .PostProcessBenchmarkResults (benchmark_perf_counters )
552+ context , data = self .PostProcessBenchmarkResults ()
565553 context_list .append (context )
566554 data_list .append (data )
567555
0 commit comments