Skip to content

Commit ad6b189

Browse files
liyuying0000copybara-github
authored andcommitted
Collect average "iterations" in parallel_bench.
PiperOrigin-RevId: 731762429 Change-Id: Ia1f1b795232a9ef2939c7ee4d7fde3a58e1ea3bb
1 parent f411e9b commit ad6b189

File tree

7 files changed

+68
-10
lines changed

7 files changed

+68
-10
lines changed

fleetbench/parallel/parallel_bench_lib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class BenchmarkMetrics:
9191
per_iteration_wall_time: float
9292
# per benchmark iteration cpu time
9393
per_iteration_cpu_time: float
94+
# per benchmark iteration
95+
per_bm_run_iteration: int
9496

9597

9698
class ParallelBench:
@@ -179,6 +181,7 @@ def _PreRun(
179181
total_duration=1.0,
180182
per_iteration_wall_time=0.0,
181183
per_iteration_cpu_time=0.0,
184+
per_bm_run_iteration=0,
182185
)
183186
]
184187
for benchmark in self.benchmarks.keys()
@@ -306,6 +309,7 @@ def _RunSchedulingLoop(self) -> None:
306309
total_duration=r.duration,
307310
per_iteration_wall_time=r.bm_wall_time,
308311
per_iteration_cpu_time=r.bm_cpu_time,
312+
per_bm_run_iteration=r.iteration,
309313
)
310314
)
311315

@@ -373,6 +377,7 @@ def ConvertToDataFrame(self) -> pd.DataFrame:
373377
"Benchmark": benchmark,
374378
"WallTimes": t.per_iteration_wall_time,
375379
"CPUTimes": t.per_iteration_cpu_time,
380+
"Iterations": t.per_bm_run_iteration,
376381
}
377382
data.append(entry)
378383
runtimes = pd.DataFrame(data)
@@ -443,6 +448,7 @@ def Run(
443448
total_duration=r.duration,
444449
per_iteration_wall_time=r.bm_wall_time,
445450
per_iteration_cpu_time=r.bm_cpu_time,
451+
per_bm_run_iteration=r.iteration,
446452
)
447453
)
448454

fleetbench/parallel/parallel_bench_lib_test.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ def tearDown(self):
4949
@mock.patch.object(bm, "GetSubBenchmarks", autospec=True)
5050
@mock.patch.object(run.Run, "Execute", autospec=True)
5151
@mock.patch.object(cpu, "Utilization", autospec=True)
52+
@mock.patch.object(reporter, "GenerateBenchmarkReport", autospec=True)
5253
@mock.patch.object(reporter, "SaveBenchmarkResults", autospec=True)
5354
@flagsaver.flagsaver(
5455
benchmark_dir=absltest.get_default_test_tmpdir(),
5556
)
5657
def testRun(
5758
self,
5859
mock_save_benchmark_results,
60+
mock_generate_benchmark_report,
5961
mock_utilization,
6062
mock_execute,
6163
mock_get_subbenchmarks,
@@ -81,6 +83,7 @@ def fake_utilization(unused_cpus):
8183

8284
mock_utilization.side_effect = fake_utilization
8385

86+
mock_generate_benchmark_report.return_value = pd.DataFrame()
8487
mock_save_benchmark_results.return_value = None
8588

8689
self.pb = parallel_bench_lib.ParallelBench(
@@ -126,23 +129,27 @@ def test_convert_to_dataframe(self):
126129
total_duration=10,
127130
per_iteration_wall_time=1,
128131
per_iteration_cpu_time=1,
132+
per_bm_run_iteration=2,
129133
),
130134
parallel_bench_lib.BenchmarkMetrics(
131135
total_duration=2,
132136
per_iteration_wall_time=3.01,
133137
per_iteration_cpu_time=3,
138+
per_bm_run_iteration=4,
134139
),
135140
]
136141
self.pb.runtimes["BM_Test2"] = [
137142
parallel_bench_lib.BenchmarkMetrics(
138143
total_duration=10,
139144
per_iteration_wall_time=1,
140145
per_iteration_cpu_time=1,
146+
per_bm_run_iteration=10,
141147
),
142148
parallel_bench_lib.BenchmarkMetrics(
143149
total_duration=4,
144150
per_iteration_wall_time=4,
145151
per_iteration_cpu_time=5,
152+
per_bm_run_iteration=8,
146153
),
147154
]
148155
self.pb.utilization_samples.append((pd.Timestamp.now(), 0.5))
@@ -151,8 +158,18 @@ def test_convert_to_dataframe(self):
151158
self.assertEqual(
152159
df.to_dict("records"),
153160
[
154-
{"Benchmark": "BM_Test1", "WallTimes": 3.01, "CPUTimes": 3},
155-
{"Benchmark": "BM_Test2", "WallTimes": 4, "CPUTimes": 5},
161+
{
162+
"Benchmark": "BM_Test1",
163+
"WallTimes": 3.01,
164+
"CPUTimes": 3,
165+
"Iterations": 4,
166+
},
167+
{
168+
"Benchmark": "BM_Test2",
169+
"WallTimes": 4,
170+
"CPUTimes": 5,
171+
"Iterations": 8,
172+
},
156173
],
157174
)
158175

fleetbench/parallel/reporter.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,13 @@ def GenerateBenchmarkReport(
103103
Count=("WallTimes", "count"),
104104
Mean_Wall_Time=("WallTimes", "mean"),
105105
Mean_CPU_Time=("CPUTimes", "mean"),
106+
Mean_Iterations=("Iterations", "mean"),
106107
)
107108
.round(3)
108109
)
110+
grouped_results["Mean_Iterations"] = grouped_results[
111+
"Mean_Iterations"
112+
].astype(int)
109113

110114
# Combine perf_counter_df and benchmark run results on the same
111115
# "benchmark" entry.
@@ -134,6 +138,7 @@ def SaveBenchmarkResults(output_dir, df: pd.DataFrame) -> None:
134138
columns={
135139
"Mean_Wall_Time": "real_time",
136140
"Mean_CPU_Time": "cpu_time",
141+
"Mean_Iterations": "iterations",
137142
}
138143
)
139144
data = df.reset_index().to_dict(orient="records")

fleetbench/parallel/reporter_test.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,24 @@ def test_aggregate_constext_inconsistent_data(self):
115115

116116
def test_generate_benchmark_report(self):
117117
benchmark_df = pd.DataFrame([
118-
{"Benchmark": "BM_Test1", "WallTimes": 3, "CPUTimes": 3},
119-
{"Benchmark": "BM_Test2", "WallTimes": 4, "CPUTimes": 5},
120-
{"Benchmark": "BM_Test1", "WallTimes": 6, "CPUTimes": 7},
118+
{
119+
"Benchmark": "BM_Test1",
120+
"WallTimes": 3,
121+
"CPUTimes": 3,
122+
"Iterations": 10,
123+
},
124+
{
125+
"Benchmark": "BM_Test2",
126+
"WallTimes": 4,
127+
"CPUTimes": 5,
128+
"Iterations": 20,
129+
},
130+
{
131+
"Benchmark": "BM_Test1",
132+
"WallTimes": 6,
133+
"CPUTimes": 7,
134+
"Iterations": 50,
135+
},
121136
])
122137
perf_counter_df = pd.DataFrame([
123138
{"Benchmark": "BM_Test1", "instructions": 130.0, "cycles": 3.0},
@@ -134,6 +149,7 @@ def test_generate_benchmark_report(self):
134149
"Count": 2,
135150
"Mean_Wall_Time": 4.5,
136151
"Mean_CPU_Time": 5.0,
152+
"Mean_Iterations": 30,
137153
"instructions": 130.0,
138154
"cycles": 3.0,
139155
},
@@ -142,6 +158,7 @@ def test_generate_benchmark_report(self):
142158
"Count": 1,
143159
"Mean_Wall_Time": 4,
144160
"Mean_CPU_Time": 5.0,
161+
"Mean_Iterations": 20,
145162
"instructions": 200.0,
146163
"cycles": 2.0,
147164
},

fleetbench/parallel/result.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ class Result:
3030
duration: Optional[float] = None
3131
bm_wall_time: Optional[float] = None
3232
bm_cpu_time: Optional[float] = None
33+
iteration: Optional[int] = None

fleetbench/parallel/run.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ def Execute(self) -> result.Result:
5252

5353
with open(self.out_file, "r") as f:
5454
benchmark_output = f.read()
55-
benchmark_cpu_time, benchmark_wall_time = self._GetBenchmarkRuntime(
56-
benchmark_output
55+
benchmark_cpu_time, benchmark_wall_time, iteration = (
56+
self._GetBenchmarkRuntime(benchmark_output)
5757
)
5858
return result.Result(
5959
benchmark=self.benchmark.Name(),
6060
rc=proc.returncode,
6161
duration=end - start,
6262
bm_wall_time=benchmark_wall_time,
6363
bm_cpu_time=benchmark_cpu_time,
64+
iteration=iteration,
6465
result=benchmark_output,
6566
stdout=proc.stdout,
6667
stderr=proc.stderr,
@@ -72,6 +73,12 @@ def _CommandLine(self) -> list[str]:
7273
f"--benchmark_out={self.out_file}",
7374
]
7475

75-
def _GetBenchmarkRuntime(self, benchmark_output: str) -> tuple[float, float]:
76+
def _GetBenchmarkRuntime(
77+
self, benchmark_output: str
78+
) -> tuple[float, float, int]:
7679
data = json.loads(benchmark_output)
77-
return data["benchmarks"][0]["cpu_time"], data["benchmarks"][0]["real_time"]
80+
return (
81+
data["benchmarks"][0]["cpu_time"],
82+
data["benchmarks"][0]["real_time"],
83+
data["benchmarks"][0]["iterations"],
84+
)

fleetbench/parallel/run_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ class RunTest(absltest.TestCase):
3333
def testRun(self, mock_run):
3434
output_file = self.create_tempfile()
3535

36-
data = {"benchmarks": [{"cpu_time": 12.345, "real_time": 12.3}]}
36+
data = {
37+
"benchmarks": [
38+
{"cpu_time": 12.345, "real_time": 12.3, "iterations": 10}
39+
]
40+
}
3741
json_object = json.dumps(data, indent=4)
3842
with open(output_file.full_path, "w") as f:
3943
f.write(json_object)
@@ -56,6 +60,7 @@ def testRun(self, mock_run):
5660
self.assertGreater(result.duration, 0)
5761
self.assertEqual(result.bm_cpu_time, 12.345)
5862
self.assertEqual(result.bm_wall_time, 12.3)
63+
self.assertEqual(result.iteration, 10)
5964
self.assertEqual(result.rc, 0)
6065

6166
@flagsaver.flagsaver(

0 commit comments

Comments
 (0)