Skip to content

Commit

Permalink
Allow for 0 --warmup-iterations (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
shabalind authored Jun 15, 2020
1 parent 18e7ca3 commit d446253
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
9 changes: 7 additions & 2 deletions Sources/Benchmark/BenchmarkArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public struct BenchmarkArguments: ParsableArguments {
if iterations != nil && iterations! <= 0 {
throw ValidationError(positiveNumberError(flag: "--iterations", of: "integer"))
}
if warmupIterations != nil && warmupIterations! <= 0 {
throw ValidationError(positiveNumberError(flag: "--warmup-iterations", of: "integer"))
if warmupIterations != nil && warmupIterations! < 0 {
throw ValidationError(
nonNegativeNumberError(flag: "--warmup-iterations", of: "integer"))
}
if maxIterations != nil && maxIterations! <= 0 {
throw ValidationError(positiveNumberError(flag: "--max-iterations", of: "integer"))
Expand All @@ -99,4 +100,8 @@ public struct BenchmarkArguments: ParsableArguments {
func positiveNumberError(flag: String, of type: String) -> String {
return "Value provided via \(flag) must be a positive \(type)."
}

func nonNegativeNumberError(flag: String, of type: String) -> String {
return "Value provided via \(flag) must be a non-negative \(type)."
}
}
5 changes: 3 additions & 2 deletions Sources/Benchmark/BenchmarkRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public struct BenchmarkRunner {
let totalStart = now()

var warmupState: BenchmarkState? = nil
if let n = settings.warmupIterations {
warmupState = doNIterations(n, benchmark: benchmark, suite: suite, settings: settings)
if settings.warmupIterations > 0 {
warmupState = doNIterations(
settings.warmupIterations, benchmark: benchmark, suite: suite, settings: settings)
}

var state: BenchmarkState
Expand Down
8 changes: 6 additions & 2 deletions Sources/Benchmark/BenchmarkSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ public struct BenchmarkSettings {
}

/// Convenience accessor for WarmupIterations setting.
public var warmupIterations: Int? {
return self[WarmupIterations.self]?.value
public var warmupIterations: Int {
if let value = self[WarmupIterations.self]?.value {
return value
} else {
return 0
}
}

/// Convenience accessor for the Filter setting.
Expand Down
21 changes: 14 additions & 7 deletions Tests/BenchmarkTests/BenchmarkCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ final class BenchmarkCommandTests: XCTestCase {
}
}

func testParseZeroWarmupIterationsError() throws {
AssertFailsToParse(
["--warmup-iterations", "0", "--allow-debug-build"],
with: "Value provided via --warmup-iterations must be a positive integer.")
func testParseZeroWarmupIterations() throws {
AssertParse(["--warmup-iterations", "0", "--allow-debug-build"]) { settings in
XCTAssertEqual(settings.warmupIterations, 0)
}
}

func testParseNoWarmupIterations() throws {
AssertParse(["--allow-debug-build"]) { settings in
XCTAssertEqual(settings.warmupIterations, 0)
}
}

func testParseMaxIterations() throws {
Expand All @@ -87,7 +93,7 @@ final class BenchmarkCommandTests: XCTestCase {

func testParseZeroMaxIterationsError() throws {
AssertFailsToParse(
["--warmup-iterations", "0", "--allow-debug-build"],
["--max-iterations", "0", "--allow-debug-build"],
with: "Value provided via --max-iterations must be a positive integer.")
}

Expand All @@ -110,9 +116,10 @@ final class BenchmarkCommandTests: XCTestCase {
("testParseIterations", testParseIterations),
("testParseZeroIterationsError", testParseZeroIterationsError),
("testParseWarmupIterations", testParseWarmupIterations),
("testParseZeroWarmupIterationsError", testParseZeroWarmupIterationsError),
("testParseZeroWarmupIterations", testParseZeroWarmupIterations),
("testParseNoWarmupIterations", testParseNoWarmupIterations),
("testParseMaxIterations", testParseWarmupIterations),
("testParseZeroMaxIterationsError", testParseZeroWarmupIterationsError),
("testParseZeroMaxIterationsError", testParseZeroMaxIterationsError),
("testParseMinTime", testParseMinTime),
("testParseZeroMinTimeError", testParseZeroMinTimeError),
]
Expand Down

0 comments on commit d446253

Please sign in to comment.