Skip to content

Commit 89cf9e9

Browse files
liyuying0000copybara-github
authored andcommitted
Replace NaN in std columns with string "NaN" for JSON compatibility.
PiperOrigin-RevId: 822745633 Change-Id: I4337e86104f4d3b1325a623146c1497aafd476f7
1 parent 97779fa commit 89cf9e9

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

fleetbench/parallel/reporter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ def SaveBenchmarkResults(
269269
"Iterations_std": "iterations_std",
270270
}
271271
)
272+
df = df.fillna("NaN")
272273
data = df.reset_index().to_dict(orient="records")
273274

274275
context = AggregateContext(output_dir)

fleetbench/parallel/reporter_test.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,51 @@ def test_save_benchmark_results_returns_remapped_data(self):
320320
}
321321
self.assertEqual(remapped_data, expected_remapped_data)
322322

323+
def test_save_benchmark_results_handles_nan(self):
324+
df = pd.DataFrame({
325+
"Benchmark": ["BM_Test1", "BM_Test2"],
326+
"Mean_Wall_Time": [10.0, 20.0],
327+
"Mean_CPU_Time": [11.0, 21.0],
328+
"Mean_Iterations": [100, 200],
329+
"Wall_Time_std": [1.0, math.nan],
330+
"CPU_Time_std": [1.1, math.nan],
331+
"Iterations_std": [10.0, math.nan],
332+
"cycles": [1000, math.nan],
333+
"instructions": [500, 600],
334+
}).set_index("Benchmark")
335+
336+
reporter.SaveBenchmarkResults(self.test_dir.full_path, df)
337+
file_name = os.path.join(self.test_dir, "results.json")
338+
self.assertTrue(os.path.exists(file_name))
339+
with open(file_name, "r") as json_file:
340+
data = json.load(json_file)
341+
342+
expected_benchmarks = [
343+
{
344+
"name": "BM_Test1",
345+
"real_time": 10.0,
346+
"cpu_time": 11.0,
347+
"iterations": 100,
348+
"real_time_std": 1.0,
349+
"cpu_time_std": 1.1,
350+
"iterations_std": 10.0,
351+
"cycles": 1000,
352+
"instructions": 500,
353+
},
354+
{
355+
"name": "BM_Test2",
356+
"real_time": 20.0,
357+
"cpu_time": 21.0,
358+
"iterations": 200,
359+
"real_time_std": "NaN",
360+
"cpu_time_std": "NaN",
361+
"iterations_std": "NaN",
362+
"cycles": "NaN",
363+
"instructions": 600,
364+
},
365+
]
366+
self.assertEqual(data["benchmarks"], expected_benchmarks)
367+
323368
def test_generate_final_report_multiple_repetitions(self):
324369
output_dir = self.create_tempdir()
325370
context_list = [{"key": "value1"}, {"key": "value2"}]

0 commit comments

Comments
 (0)