Skip to content

Commit e598ee1

Browse files
palimondoDavide Italiano
authored andcommitted
[benchmark] Gardening: DRYer verbose log
1 parent 295b6f4 commit e598ee1

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

benchmark/utils/DriverUtils.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -407,21 +407,20 @@ final class SampleRunner {
407407

408408
/// Invoke the benchmark entry point and return the run time in milliseconds.
409409
func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
410+
func logVerbose(_ msg: @autoclosure () -> String) {
411+
if c.verbose { print(msg()) }
412+
}
410413
var samples = [Int](repeating: 0, count: c.numSamples)
411414

412415
// Before we do anything, check that we actually have a function to
413416
// run. If we don't it is because the benchmark is not supported on
414417
// the platform and we should skip it.
415418
guard let testFn = test.runFunction else {
416-
if c.verbose {
417-
print("Skipping unsupported benchmark \(test.name)!")
418-
}
419+
logVerbose("Skipping unsupported benchmark \(test.name)!")
419420
return nil
420421
}
421422

422-
if c.verbose {
423-
print("Running \(test.name) for \(c.numSamples) samples.")
424-
}
423+
logVerbose("Running \(test.name) for \(c.numSamples) samples.")
425424

426425
let sampler = SampleRunner(c)
427426
test.setUpFunction?()
@@ -438,9 +437,7 @@ func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
438437
/// Number of iterations to make `testFn` run for the desired time.
439438
scale = timePerSample / elapsed_time
440439
} else {
441-
if c.verbose {
442-
print(" Warning: elapsed time is 0. This can be safely ignored if the body is empty.")
443-
}
440+
logVerbose(" Warning: elapsed time is 0!")
444441
scale = 1
445442
}
446443
} else {
@@ -457,16 +454,12 @@ func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
457454

458455
// Rerun the test with the computed scale factor.
459456
if scale > 1 {
460-
if c.verbose {
461-
print(" Measuring with scale \(scale).")
462-
}
457+
logVerbose(" Measuring with scale \(scale).")
463458
elapsed_time = sampler.measure(test.name, fn: testFn, numIters: scale)
464459
}
465460

466461
samples[s] = elapsed_time
467-
if c.verbose {
468-
print(" Sample \(s),\(samples[s])")
469-
}
462+
logVerbose(" Sample \(s),\(samples[s])")
470463
}
471464
test.tearDownFunction?()
472465

0 commit comments

Comments
 (0)