1717import shutil
1818from unittest import mock
1919
20+ from absl import flags
2021from absl .testing import absltest
2122from absl .testing import flagsaver
2223from absl .testing import parameterized
3031from fleetbench .parallel import run
3132from fleetbench .parallel import weights
3233
34+ FLAGS = flags .FLAGS
35+
3336
3437class ParallelBenchTest (parameterized .TestCase ):
3538
3639 def setUp (self ):
3740 super ().setUp ()
41+ self .temp_dir = self .create_tempdir ()
3842 self .pb = parallel_bench_lib .ParallelBench (
3943 cpus = [0 , 1 ],
4044 cpu_affinity = False ,
4145 utilization = 0.5 ,
4246 duration = 0.1 ,
4347 repetitions = 1 ,
44- temp_parent_root = absltest . get_default_test_tmpdir () ,
48+ temp_parent_root = self . temp_dir . full_path ,
4549 keep_raw_data = True ,
4650 benchmark_perf_counters = "" ,
4751 benchmark_threads = {},
4852 )
4953
50- def tearDown (self ):
51- super ().tearDown ()
52- for name in os .listdir (self .pb .temp_parent_root ):
53- if name .startswith ("run_" ):
54- shutil .rmtree (os .path .join (self .pb .temp_parent_root , name ))
55- else :
56- os .remove (os .path .join (self .pb .temp_parent_root , name ))
57-
5854 @mock .patch .object (bm , "GetSubBenchmarks" , autospec = True )
5955 @mock .patch .object (run .Run , "Execute" , autospec = True )
6056 @mock .patch .object (cpu , "Utilization" , autospec = True )
6157 @mock .patch .object (reporter , "GenerateBenchmarkReport" , autospec = True )
6258 @mock .patch .object (
6359 reporter , "SaveBenchmarkResults" , autospec = True , return_value = (None , None )
6460 )
65- @flagsaver .flagsaver (
66- benchmark_dir = absltest .get_default_test_tmpdir (),
67- )
61+ @flagsaver .flagsaver
6862 def testRun (
6963 self ,
7064 mock_save_benchmark_results ,
@@ -73,6 +67,7 @@ def testRun(
7367 mock_execute ,
7468 mock_get_subbenchmarks ,
7569 ):
70+ FLAGS .benchmark_dir = self .temp_dir .full_path
7671 mock_get_subbenchmarks .return_value = ["BM_Test1" , "BM_Test2" ]
7772 mock_execute .return_value = result .Result (
7873 benchmark = "fake_bench (BM_Test1)" ,
@@ -83,9 +78,7 @@ def testRun(
8378 bm_cpu_time = 0.01 ,
8479 result = "fake_result" ,
8580 )
86- self .create_tempfile (
87- os .path .join (absltest .get_default_test_tmpdir (), "fake_bench" )
88- )
81+ self .create_tempfile (os .path .join (self .temp_dir .full_path , "fake_bench" ))
8982
9083 def fake_utilization (unused_cpus ):
9184 # Return 0% for the first call, then 55% for the rest.
@@ -102,7 +95,7 @@ def fake_utilization(unused_cpus):
10295 utilization = 0.5 ,
10396 duration = 0.1 ,
10497 repetitions = 1 ,
105- temp_parent_root = absltest . get_default_test_tmpdir () ,
98+ temp_parent_root = self . temp_dir . full_path ,
10699 keep_raw_data = True ,
107100 benchmark_perf_counters = "" ,
108101 benchmark_threads = {},
@@ -116,6 +109,7 @@ def fake_utilization(unused_cpus):
116109 )
117110 self .pb .Run ()
118111 mock_execute .assert_called_once ()
112+ mock_save_benchmark_results .assert_called_once ()
119113
120114 @mock .patch .object (parallel_bench_lib .ParallelBench , "_PreRun" , autospec = True )
121115 @mock .patch .object (
@@ -166,9 +160,9 @@ def test_run_multiple_repetitions(
166160 self .assertEqual (args [3 ], 2 )
167161
168162 def test_set_extra_benchmark_flags (self ):
163+ self .pb .perf_counters = ["instructions" ]
169164 self .assertEqual (
170- parallel_bench_lib ._SetExtraBenchmarkFlags (
171- benchmark_perf_counters = ["instructions" ],
165+ self .pb ._SetExtraBenchmarkFlags (
172166 benchmark_repetitions = 10 ,
173167 benchmark_min_time = "10s" ,
174168 ),
@@ -179,9 +173,10 @@ def test_set_extra_benchmark_flags(self):
179173 ],
180174 )
181175
176+ self .pb .perf_counters = ["instructions" , "cycles" ]
177+
182178 self .assertEqual (
183- parallel_bench_lib ._SetExtraBenchmarkFlags (
184- benchmark_perf_counters = ["instructions" , "cycles" ],
179+ self .pb ._SetExtraBenchmarkFlags (
185180 benchmark_repetitions = 0 ,
186181 benchmark_min_time = "" ,
187182 ),
@@ -197,9 +192,7 @@ def test_set_extra_benchmark_flags(self):
197192 @mock .patch .object (
198193 reporter , "SaveBenchmarkResults" , autospec = True , return_value = (None , None )
199194 )
200- @flagsaver .flagsaver (
201- benchmark_dir = absltest .get_default_test_tmpdir (),
202- )
195+ @flagsaver .flagsaver
203196 def testRunThreads (
204197 self ,
205198 mock_save_benchmark_results ,
@@ -208,6 +201,7 @@ def testRunThreads(
208201 mock_execute ,
209202 mock_get_subbenchmarks ,
210203 ):
204+ FLAGS .benchmark_dir = self .temp_dir .full_path
211205 mock_get_subbenchmarks .return_value = ["BM_Test1" ]
212206 mock_execute .return_value = result .Result (
213207 benchmark = "fake_bench (BM_Test1)" ,
@@ -218,9 +212,7 @@ def testRunThreads(
218212 bm_cpu_time = 0.01 ,
219213 result = "fake_result" ,
220214 )
221- self .create_tempfile (
222- os .path .join (absltest .get_default_test_tmpdir (), "fake_bench" )
223- )
215+ self .create_tempfile (os .path .join (self .temp_dir .full_path , "fake_bench" ))
224216
225217 def fake_utilization (unused_cpus ):
226218 # Return 0% for the first call, then 55% for the rest.
@@ -237,7 +229,7 @@ def fake_utilization(unused_cpus):
237229 utilization = 0.5 ,
238230 duration = 0.1 ,
239231 repetitions = 1 ,
240- temp_parent_root = absltest . get_default_test_tmpdir () ,
232+ temp_parent_root = self . temp_dir . full_path ,
241233 keep_raw_data = True ,
242234 benchmark_perf_counters = "" ,
243235 benchmark_threads = {"BM_Test1" : 2 },
@@ -251,6 +243,7 @@ def fake_utilization(unused_cpus):
251243 )
252244 self .pb .Run ()
253245 mock_execute .assert_called_once ()
246+ mock_save_benchmark_results .assert_called_once ()
254247
255248 def test_convert_to_dataframe (self ):
256249 # First entries are fake durations, the second entries are real durations.
0 commit comments