Skip to content

Commit 9f444d0

Browse files
liyuying0000copybara-github
authored andcommitted
Move perf counters from Run() to a ParallelBench class variable.
This change is intended to be a refactoring, and shouldn't change the behavior of the program. PiperOrigin-RevId: 746145404 Change-Id: I870110b0af524769ec5b76ac6386b7cf3442ed4b
1 parent e4a6472 commit 9f444d0

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

fleetbench/parallel/parallel_bench.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def main(argv: Sequence[str]) -> None:
199199
repetitions=_REPETITIONS.value,
200200
temp_parent_root=_TEMP_ROOT.value,
201201
keep_raw_data=_KEEP_RAW_DATA.value,
202+
benchmark_perf_counters=_BENCHMARK_PERF_COUNTERS.value,
202203
)
203204

204205
bench.SetWeights(
@@ -210,7 +211,6 @@ def main(argv: Sequence[str]) -> None:
210211
)
211212

212213
bench.Run(
213-
benchmark_perf_counters=_BENCHMARK_PERF_COUNTERS.value,
214214
benchmark_repetitions=_BENCHMARK_REPETITIONS.value,
215215
benchmark_min_time=_BENCHMARK_MIN_TIME.value,
216216
)

fleetbench/parallel/parallel_bench_lib.py

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@
3535

3636

3737
def _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

fleetbench/parallel/parallel_bench_lib_test.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def setUp(self):
4343
repetitions=1,
4444
temp_parent_root=absltest.get_default_test_tmpdir(),
4545
keep_raw_data=True,
46+
benchmark_perf_counters="",
4647
)
4748

4849
def tearDown(self):
@@ -102,6 +103,7 @@ def fake_utilization(unused_cpus):
102103
repetitions=1,
103104
temp_parent_root=absltest.get_default_test_tmpdir(),
104105
keep_raw_data=True,
106+
benchmark_perf_counters="",
105107
)
106108
self.pb.SetWeights(
107109
benchmark_target="fake_bench",
@@ -164,7 +166,7 @@ def test_run_multiple_repetitions(
164166
def test_set_extra_benchmark_flags(self):
165167
self.assertEqual(
166168
parallel_bench_lib._SetExtraBenchmarkFlags(
167-
benchmark_perf_counters="instructions",
169+
benchmark_perf_counters=["instructions"],
168170
benchmark_repetitions=10,
169171
benchmark_min_time="10s",
170172
),
@@ -177,12 +179,12 @@ def test_set_extra_benchmark_flags(self):
177179

178180
self.assertEqual(
179181
parallel_bench_lib._SetExtraBenchmarkFlags(
180-
benchmark_perf_counters="instructions",
182+
benchmark_perf_counters=["instructions", "cycles"],
181183
benchmark_repetitions=0,
182184
benchmark_min_time="",
183185
),
184186
[
185-
"--benchmark_perf_counters=instructions",
187+
"--benchmark_perf_counters=instructions,cycles",
186188
],
187189
)
188190

@@ -274,8 +276,8 @@ def test_generate_perf_counter_dataframe(self):
274276
with open(os.path.join(self.pb.temp_root, "run_3"), "w") as f:
275277
json.dump(mock_data3, f)
276278

277-
counters = "instructions,cycles"
278-
df = self.pb.GeneratePerfCounterDataFrame(counters)
279+
self.pb.perf_counters = ["instructions", "cycles"]
280+
df = self.pb.GeneratePerfCounterDataFrame()
279281

280282
self.assertIsInstance(df, pd.DataFrame)
281283
self.assertLen(df, 2) # There are two benchmarks
@@ -311,7 +313,7 @@ def test_post_processing_benchmark_results(
311313
])
312314

313315
mock_save_benchmark_results.return_value = (None, None)
314-
self.pb.PostProcessBenchmarkResults("instructions,cycles")
316+
self.pb.PostProcessBenchmarkResults()
315317
mock_generate_perf_counter_dataframe.assert_called_once()
316318
mock_generate_benchmark_report.assert_called_once()
317319
mock_save_benchmark_results.assert_called_once()

0 commit comments

Comments
 (0)