@@ -290,19 +290,65 @@ def mock_run(test):
290
290
self .assertEquals (test , 'b1' )
291
291
return PerformanceTestResult (
292
292
'3,b1,1,123,123,123,0,123,888' .split (',' ))
293
- driver = Stub (tests = ['b1' ], log_file = None )
294
- driver .run_independent_samples = mock_run
295
293
run_benchmarks = Benchmark_Driver .run_benchmarks
294
+ driver = Stub (tests = ['b1' ], args = Stub (output_dir = None ))
295
+ driver .run_independent_samples = mock_run
296
296
297
297
with captured_output () as (out , _ ):
298
- run_benchmarks (driver )
299
- self .assertEquals ('\n ' .join ("""
300
- #,TEST,SAMPLES,MIN(μs),MAX(μs),MEAN(μs),SD(μs),MEDIAN(μs),MAX_RSS(B)
301
- 3,b1,1,123,123,123,0,123,888
302
-
303
- Totals,1
298
+ formatted_output = run_benchmarks (driver )
304
299
305
- """ .splitlines ()[1 :]), out .getvalue ()) # removes 1st \n from multiline string
300
+ self .assertEquals (
301
+ out .getvalue (),
302
+ '#,TEST,SAMPLES,MIN(μs),MAX(μs),MEAN(μs),SD(μs),MEDIAN(μs),' +
303
+ 'MAX_RSS(B)\n ' +
304
+ '3,b1,1,123,123,123,0,123,888\n \n Totals,1\n ' )
305
+ self .assertEquals (formatted_output ,
306
+ '3,b1,1,123,123,123,0,123,888\n ' +
307
+ '\n Totals,1' )
308
+
309
+ driver .args .output_dir = 'logs/'
310
+ with captured_output () as (out , _ ):
311
+ run_benchmarks (driver )
312
+ self .assertEquals (
313
+ out .getvalue (),
314
+ ' # TEST SAMPLES MIN(μs) MAX(μs)' +
315
+ ' MEAN(μs) SD(μs) MEDIAN(μs) MAX_RSS(B)\n ' +
316
+ ' 3 b1 1 123 123' +
317
+ ' 123 0 123 888\n ' +
318
+ ' Totals 1 ' +
319
+ ' \n ' )
320
+
321
+ def test_log_results (self ):
322
+ """Create log directory if it doesn't exist and write the log file."""
323
+ def assert_log_written (out , log_file , content ):
324
+ self .assertEquals (out .getvalue (),
325
+ 'Logging results to: ' + log_file + '\n ' )
326
+ with open (log_file , 'rU' ) as f :
327
+ text = f .read ()
328
+ self .assertEquals (text , "formatted output" )
329
+
330
+ try :
331
+ import tempfile # setUp
332
+ temp_dir = tempfile .mkdtemp ()
333
+ log_dir = os .path .join (temp_dir , 'sub-dir/' )
334
+ log_results = Benchmark_Driver .log_results
335
+
336
+ self .assertFalse (os .path .exists (log_dir ))
337
+ content = "formatted output"
338
+ log_file = os .path .join (log_dir , '1.log' )
339
+ with captured_output () as (out , _ ):
340
+ log_results (log_file , content )
341
+ assert_log_written (out , log_file , content )
342
+
343
+ self .assertTrue (os .path .exists (log_dir ))
344
+ log_file = os .path .join (log_dir , '2.log' )
345
+ with captured_output () as (out , _ ):
346
+ log_results (log_file , content )
347
+ assert_log_written (out , log_file , content )
348
+
349
+ finally :
350
+ import shutil # tearDown
351
+ shutil .rmtree (temp_dir )
306
352
307
353
308
354
class BenchmarkDriverMock (Mock ):
0 commit comments