Skip to content

Commit ef1461c

Browse files
committed
[benchmark] Strangle log_results
Moved `log_results` to BenchmarkDriver.
1 parent a10b607 commit ef1461c

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

benchmark/scripts/Benchmark_Driver

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ class BenchmarkDriver(object):
163163
[self.run(test, measure_memory=True)
164164
for _ in range(self.args.iterations)])
165165

166+
def log_results(self, output, log_file=None):
167+
"""Log output to `log_file`.
168+
169+
Creates `args.output_dir` if it doesn't exist yet.
170+
"""
171+
log_file = log_file or self.log_file
172+
dir = os.path.dirname(log_file)
173+
if not os.path.exists(dir):
174+
os.makedirs(dir)
175+
print('Logging results to: %s' % log_file)
176+
with open(log_file, 'w') as f:
177+
f.write(output)
178+
166179

167180
class LoggingReportFormatter(logging.Formatter):
168181
"""Format logs as plain text or with colors on the terminal.
@@ -399,15 +412,6 @@ class BenchmarkDoctor(object):
399412
return 0
400413

401414

402-
def log_results(log_file, formatted_output):
403-
dir = os.path.dirname(log_file)
404-
if not os.path.exists(dir):
405-
os.makedirs(dir)
406-
print('Logging results to: %s' % log_file)
407-
with open(log_file, 'w') as f:
408-
f.write(formatted_output)
409-
410-
411415
def run_benchmarks(driver):
412416
"""Run perf tests individually and return results in a format that's
413417
compatible with `LogParser`.
@@ -455,7 +459,7 @@ def run(args):
455459
driver = BenchmarkDriver(args)
456460
output = run_benchmarks(driver)
457461
if args.output_dir:
458-
log_results(driver.log_file, output)
462+
driver.log_results(output)
459463
return 0
460464

461465

benchmark/scripts/test_Benchmark_Driver.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,19 @@ def assert_log_written(out, log_file, content):
331331
import tempfile # setUp
332332
temp_dir = tempfile.mkdtemp()
333333
log_dir = os.path.join(temp_dir, 'sub-dir/')
334-
log_results = Benchmark_Driver.log_results
334+
driver = BenchmarkDriver(Stub(), tests=[''])
335335

336336
self.assertFalse(os.path.exists(log_dir))
337337
content = "formatted output"
338338
log_file = os.path.join(log_dir, '1.log')
339339
with captured_output() as (out, _):
340-
log_results(log_file, content)
340+
driver.log_results(content, log_file=log_file)
341341
assert_log_written(out, log_file, content)
342342

343343
self.assertTrue(os.path.exists(log_dir))
344344
log_file = os.path.join(log_dir, '2.log')
345345
with captured_output() as (out, _):
346-
log_results(log_file, content)
346+
driver.log_results(content, log_file=log_file)
347347
assert_log_written(out, log_file, content)
348348

349349
finally:

0 commit comments

Comments
 (0)