Skip to content

Commit e2bb5e3

Browse files
authored
Add --quiet flag (#62)
* Add --quiet flag * Update README * Update comments
1 parent 9d4782b commit e2bb5e3

File tree

7 files changed

+33
-11
lines changed

7 files changed

+33
-11
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ more details on what options are available, pass either the `-h` or `--help` com
2525
Example:
2626

2727
```terminal
28-
$ swift run -c release BenchmarkMinimalExample -h
29-
[3/3] Linking BenchmarkMinimalExample
30-
USAGE: benchmark-command [--allow-debug-build] [--filter <filter>] [--filter-not <filter-not>] [--iterations <iterations>] [--warmup-iterations <warmup-iterations>] [--min-time <min-time>] [--max-iterations <max-iterations>] [--time-unit <time-unit>] [--inverse-time-unit <inverse-time-unit>] [--columns <columns>] [--format <format>]
28+
$ swift run -c release BenchmarkMinimalExample --help
29+
USAGE: benchmark-command [--allow-debug-build] [--filter <filter>] [--filter-not <filter-not>] [--iterations <iterations>] [--warmup-iterations <warmup-iterations>] [--min-time <min-time>] [--max-iterations <max-iterations>] [--time-unit <time-unit>] [--inverse-time-unit <inverse-time-unit>] [--columns <columns>] [--format <format>] [--quiet]
3130
3231
OPTIONS:
3332
--allow-debug-build Overrides check to verify optimized build.
@@ -40,13 +39,13 @@ OPTIONS:
4039
Number of warm-up iterations to run.
4140
--min-time <min-time> Minimal time to run when automatically detecting number iterations.
4241
--max-iterations <max-iterations>
43-
Maximum number of iterations to run when automatically detecting number
44-
iterations.
42+
Maximum number of iterations to run when automatically detecting number iterations.
4543
--time-unit <time-unit> Time unit used to report the timing results.
4644
--inverse-time-unit <inverse-time-unit>
4745
Inverse time unit used to report throughput results.
4846
--columns <columns> Comma-separated list of column names to show.
4947
--format <format> Output format (valid values are: json, csv, console, none).
48+
--quiet Only print final benchmark results.
5049
-h, --help Show help information.
5150
5251
$ swift run -c release BenchmarkMinimalExample

Sources/Benchmark/BenchmarkArguments.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public struct BenchmarkArguments: ParsableArguments {
5151
@Option(help: "Output format (valid values are: json, csv, console, none).")
5252
var format: Format.Value?
5353

54+
@Flag(help: "Only print final benchmark results.")
55+
var quiet: Bool
56+
5457
public init() {}
5558

5659
/// Conversion from command-line arguments to benchmark settings.
@@ -88,6 +91,9 @@ public struct BenchmarkArguments: ParsableArguments {
8891
if let value = format {
8992
result.append(Format(value))
9093
}
94+
if quiet {
95+
result.append(Quiet(true))
96+
}
9197

9298
return result
9399
}

Sources/Benchmark/BenchmarkRunner.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ public struct BenchmarkRunner {
3333
self.customDefaults,
3434
self.settings,
3535
])
36-
switch globalSettings.format {
37-
case .none:
36+
if globalSettings.quiet {
3837
self.progress = QuietReporter()
39-
default:
38+
} else {
4039
self.progress = VerboseProgressReporter(output: StderrOutputStream())
4140
}
4241
switch globalSettings.format {

Sources/Benchmark/BenchmarkSetting.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ public struct Format: BenchmarkSetting {
115115
}
116116
}
117117

118+
/// If quiet is set to true, don't show intermediate progress updates.
119+
public struct Quiet: BenchmarkSetting {
120+
public var value: Bool
121+
public init(_ value: Bool) {
122+
self.value = value
123+
}
124+
}
125+
118126
/// An aggregate of all benchmark settings, that deduplicates
119127
/// the settings based on their type. A setting which is defined
120128
/// multiple times, only retains its last set value.
@@ -237,6 +245,15 @@ public struct BenchmarkSettings {
237245
fatalError("format must have a default.")
238246
}
239247
}
248+
249+
/// Convenience accessor for the Quiet setting.
250+
public var quiet: Bool {
251+
if let value = self[Quiet.self]?.value {
252+
return value
253+
} else {
254+
fatalError("quiet must have a default.")
255+
}
256+
}
240257
}
241258

242259
public let defaultSettings: [BenchmarkSetting] = [
@@ -245,4 +262,5 @@ public let defaultSettings: [BenchmarkSetting] = [
245262
TimeUnit(.ns),
246263
InverseTimeUnit(.s),
247264
Format(.console),
265+
Quiet(false),
248266
]

Tests/BenchmarkTests/BenchmarkRunnerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ final class BenchmarkRunnerTests: XCTestCase {
124124

125125
extension BenchmarkRunnerTests {
126126
func run(suites: [BenchmarkSuite], settings: [BenchmarkSetting]) throws -> [BenchmarkResult] {
127-
var allSettings: [BenchmarkSetting] = [Format(.none)]
127+
var allSettings: [BenchmarkSetting] = [Format(.none), Quiet(true)]
128128
allSettings.append(contentsOf: settings)
129129
var runner = BenchmarkRunner(
130130
suites: suites,

Tests/BenchmarkTests/BenchmarkSettingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class BenchmarkSettingTests: XCTestCase {
2424
cli: [BenchmarkSetting],
2525
customDefaults: [BenchmarkSetting] = []
2626
) throws {
27-
var settings: [BenchmarkSetting] = [Format(.none)]
27+
var settings: [BenchmarkSetting] = [Format(.none), Quiet(true)]
2828
settings.append(contentsOf: cli)
2929
var runner = BenchmarkRunner(
3030
suites: [suite], settings: settings, customDefaults: customDefaults)

Tests/BenchmarkTests/CustomBenchmarkTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class CustomBenchmarkTests: XCTestCase {
2323
let suite = BenchmarkSuite(name: "suite")
2424
suite.benchmarks = [benchmark]
2525

26-
var runner = BenchmarkRunner(suites: [suite], settings: [Format(.none)])
26+
var runner = BenchmarkRunner(suites: [suite], settings: [Format(.none), Quiet(true)])
2727

2828
try runner.run()
2929

0 commit comments

Comments
 (0)