2323from fleetbench .parallel import benchmark as bm
2424from fleetbench .parallel import cpu
2525from fleetbench .parallel import parallel_bench_lib
26+ from fleetbench .parallel import reporter
2627from fleetbench .parallel import result
2728from fleetbench .parallel import run
2829
@@ -48,10 +49,17 @@ def tearDown(self):
4849 @mock .patch .object (bm , "GetSubBenchmarks" , autospec = True )
4950 @mock .patch .object (run .Run , "Execute" , autospec = True )
5051 @mock .patch .object (cpu , "Utilization" , autospec = True )
52+ @mock .patch .object (reporter , "SaveBenchmarkResults" , autospec = True )
5153 @flagsaver .flagsaver (
5254 benchmark_dir = absltest .get_default_test_tmpdir (),
5355 )
54- def testRun (self , mock_utilization , mock_execute , mock_get_subbenchmarks ):
56+ def testRun (
57+ self ,
58+ mock_save_benchmark_results ,
59+ mock_utilization ,
60+ mock_execute ,
61+ mock_get_subbenchmarks ,
62+ ):
5563 mock_get_subbenchmarks .return_value = ["BM_Test1" , "BM_Test2" ]
5664 mock_execute .return_value = result .Result (
5765 benchmark = "fake_bench (BM_Test1)" ,
@@ -72,6 +80,9 @@ def fake_utilization(unused_cpus):
7280 return fake_utilizations [min (mock_utilization .call_count - 1 , 1 )]
7381
7482 mock_utilization .side_effect = fake_utilization
83+
84+ mock_save_benchmark_results .return_value = None
85+
7586 self .pb = parallel_bench_lib .ParallelBench (
7687 cpus = [0 , 1 ],
7788 cpu_affinity = False ,
@@ -193,57 +204,24 @@ def test_generate_perf_counter_dataframe(self):
193204 ]).set_index ("Benchmark" )
194205 pd .testing .assert_frame_equal (df , expected_df )
195206
196- def test_generate_benchmark_report (self ):
197- benchmark_df = pd .DataFrame ([
198- {"Benchmark" : "BM_Test1" , "WallTimes" : 3 , "CPUTimes" : 3 },
199- {"Benchmark" : "BM_Test2" , "WallTimes" : 4 , "CPUTimes" : 5 },
200- {"Benchmark" : "BM_Test1" , "WallTimes" : 6 , "CPUTimes" : 7 },
201- ])
202- perf_counter_df = pd .DataFrame ([
203- {"Benchmark" : "BM_Test1" , "instructions" : 130.0 , "cycles" : 3.0 },
204- {"Benchmark" : "BM_Test2" , "instructions" : 200.0 , "cycles" : 2.0 },
205- ])
206- combined_df = self .pb .GenerateBenchmarkReport (benchmark_df , perf_counter_df )
207- self .assertIsInstance (combined_df , pd .DataFrame )
208- self .assertLen (combined_df , 2 ) # Two benchmarks: BM_Test1 and BM_Test2
209- expected_df = pd .DataFrame ([
210- {
211- "Benchmark" : "BM_Test1" ,
212- "Count" : 2 ,
213- "Mean_Wall_Time" : 4.5 ,
214- "Mean_CPU_Time" : 5.0 ,
215- "instructions" : 130.0 ,
216- "cycles" : 3.0 ,
217- },
218- {
219- "Benchmark" : "BM_Test2" ,
220- "Count" : 1 ,
221- "Mean_Wall_Time" : 4 ,
222- "Mean_CPU_Time" : 5.0 ,
223- "instructions" : 200.0 ,
224- "cycles" : 2.0 ,
225- },
207+ @mock .patch .object (reporter , "SaveBenchmarkResults" , autospec = True )
208+ @mock .patch .object (reporter , "GenerateBenchmarkReport" , autospec = True )
209+ @mock .patch .object (
210+ parallel_bench_lib .ParallelBench , "ConvertToDataFrame" , autospec = True
211+ )
212+ def test_post_processing_benchmark_results (
213+ self ,
214+ mock_convert_to_dataframe ,
215+ mock_generate_benchmark_report ,
216+ mock_save_benchmark_results ,
217+ ):
218+ mock_convert_to_dataframe .return_value = pd .DataFrame ([
219+ {"Benchmark" : "test_benchmark1" , "WallTimes" : 10 , "CPUTimes" : 10 },
226220 ])
227- pd .testing .assert_frame_equal (combined_df , expected_df )
228221
229- def test_save_benchmark_results (self ):
230- df = pd .DataFrame ([
231- {"Benchmark" : "BM_Test1" , "Mean_Wall_Time" : 1 , "Mean_CPU_Time" : 1 },
232- {"Benchmark" : "BM_Test2" , "Mean_Wall_Time" : 1 , "Mean_CPU_Time" : 2 },
233- ]).set_index ("Benchmark" )
234-
235- self .pb .SaveBenchmarkResults (df )
236- file_name = os .path .join (absltest .get_default_test_tmpdir (), "results.json" )
237- self .assertTrue (os .path .exists (file_name ))
238- with open (file_name , "r" ) as json_file :
239- data = json .load (json_file )
240- self .assertEqual (
241- data ,
242- [
243- {"name" : "BM_Test1" , "real_time" : 1 , "cpu_time" : 1 },
244- {"name" : "BM_Test2" , "real_time" : 1 , "cpu_time" : 2 },
245- ],
246- )
222+ self .pb .PostProcessBenchmarkResults ("instructions,cycles" )
223+ mock_generate_benchmark_report .assert_called_once ()
224+ mock_save_benchmark_results .assert_called_once ()
247225
248226
249227class ParseBenchmarkWeightsTest (absltest .TestCase ):
0 commit comments