Skip to content

Commit a10b607

Browse files
committed
[benchmark] Refactor log_results
Added tests for `log_results` and the *space-justified-columns* format emited to stdout while logging to file.
1 parent 1d3fa87 commit a10b607

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

benchmark/scripts/Benchmark_Driver

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,9 @@ class BenchmarkDoctor(object):
400400

401401

402402
def log_results(log_file, formatted_output):
403-
try:
404-
os.makedirs(os.path.dirname(log_file))
405-
except OSError:
406-
pass
403+
dir = os.path.dirname(log_file)
404+
if not os.path.exists(dir):
405+
os.makedirs(dir)
407406
print('Logging results to: %s' % log_file)
408407
with open(log_file, 'w') as f:
409408
f.write(formatted_output)
@@ -420,7 +419,7 @@ def run_benchmarks(driver):
420419
# that runs the tests.
421420
os.environ["SWIFT_DETERMINISTIC_HASHING"] = "1"
422421

423-
log = driver.log_file
422+
log = driver.args.output_dir
424423
output = []
425424
headings = ['#', 'TEST', 'SAMPLES', 'MIN(μs)', 'MAX(μs)', 'MEAN(μs)',
426425
'SD(μs)', 'MEDIAN(μs)', 'MAX_RSS(B)']
@@ -449,13 +448,14 @@ def run_benchmarks(driver):
449448
else:
450449
print(totals_output[1:])
451450
formatted_output += totals_output
452-
if log:
453-
log_results(log, formatted_output)
454451
return formatted_output
455452

456453

457454
def run(args):
458-
run_benchmarks(BenchmarkDriver(args))
455+
driver = BenchmarkDriver(args)
456+
output = run_benchmarks(driver)
457+
if args.output_dir:
458+
log_results(driver.log_file, output)
459459
return 0
460460

461461

benchmark/scripts/test_Benchmark_Driver.py

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,65 @@ def mock_run(test):
290290
self.assertEquals(test, 'b1')
291291
return PerformanceTestResult(
292292
'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
295293
run_benchmarks = Benchmark_Driver.run_benchmarks
294+
driver = Stub(tests=['b1'], args=Stub(output_dir=None))
295+
driver.run_independent_samples = mock_run
296296

297297
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)
304299

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\nTotals,1\n')
305+
self.assertEquals(formatted_output,
306+
'3,b1,1,123,123,123,0,123,888\n' +
307+
'\nTotals,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)
306352

307353

308354
class BenchmarkDriverMock(Mock):

0 commit comments

Comments
 (0)