Skip to content

Commit 1841dde

Browse files
committed
[benchmark][Gardening] Unified argument parsing
Renamed `optionalArg` to `parseArg` because it can now also handle the positional arguments. Now all the command line arguments are handled through this function. Minor naming improvement of the `filterTests` parameter.
1 parent 48d1558 commit 1841dde

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

benchmark/utils/DriverUtils.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct TestConfig {
6969
var fixedNumIters: UInt?
7070
var verbose: Bool?
7171
var action: TestAction?
72-
var filters: [String]?
72+
var tests: [String]?
7373
}
7474
var c = PartialTestConfig()
7575

@@ -98,19 +98,21 @@ struct TestConfig {
9898
value: value, type: type, argument: argument)
9999
}
100100

101-
func optionalArg<T>(
102-
_ name: String,
101+
func parseArg<T>(
102+
_ name: String?,
103103
_ property: WritableKeyPath<PartialTestConfig, T>,
104104
defaultValue: T? = nil,
105105
parser parse: (String) throws -> T? = { _ in nil }
106106
) throws {
107-
if let value = benchArgs.optionalArgsMap[name] {
107+
if let name = name, let value = benchArgs.optionalArgsMap[name] {
108108
guard !value.isEmpty || defaultValue != nil
109109
else { throw ArgumentError.missingValue(name) }
110110

111111
c[keyPath: property] = (value.isEmpty)
112112
? defaultValue!
113113
: try checked(parse, value, argument:name)
114+
} else if name == nil {
115+
c[keyPath: property] = benchArgs.positionalArgs as! T
114116
}
115117
}
116118

@@ -124,19 +126,18 @@ struct TestConfig {
124126
}
125127

126128
// Parse command line arguments
127-
try optionalArg("--iter-scale", \.iterationScale) { Int($0) }
128-
try optionalArg("--num-iters", \.fixedNumIters) { UInt($0) }
129-
try optionalArg("--num-samples", \.numSamples) { Int($0) }
130-
try optionalArg("--verbose", \.verbose, defaultValue: true)
131-
try optionalArg("--delim", \.delim) { $0 }
132-
try optionalArg("--tags", \PartialTestConfig.tags, parser: tags)
133-
try optionalArg("--skip-tags", \PartialTestConfig.skipTags,
129+
try parseArg("--iter-scale", \.iterationScale) { Int($0) }
130+
try parseArg("--num-iters", \.fixedNumIters) { UInt($0) }
131+
try parseArg("--num-samples", \.numSamples) { Int($0) }
132+
try parseArg("--verbose", \.verbose, defaultValue: true)
133+
try parseArg("--delim", \.delim) { $0 }
134+
try parseArg("--tags", \PartialTestConfig.tags, parser: tags)
135+
try parseArg("--skip-tags", \PartialTestConfig.skipTags,
134136
defaultValue: [], parser: tags)
135-
try optionalArg("--sleep", \.afterRunSleep) { Int($0) }
136-
try optionalArg("--list", \.action, defaultValue: .listTests)
137-
try optionalArg("--help", \.action, defaultValue: .help(validOptions))
138-
139-
c.filters = benchArgs.positionalArgs
137+
try parseArg("--sleep", \.afterRunSleep) { Int($0) }
138+
try parseArg("--list", \.action, defaultValue: .listTests)
139+
try parseArg("--help", \.action, defaultValue: .help(validOptions))
140+
try parseArg(nil, \.tests) // positional arguments
140141

141142
// Configure from the command line arguments, filling in the defaults.
142143
delim = c.delim ?? ","
@@ -147,7 +148,7 @@ struct TestConfig {
147148
afterRunSleep = c.afterRunSleep
148149
action = c.action ?? .run
149150
tests = TestConfig.filterTests(registeredBenchmarks,
150-
filters: c.filters ?? [],
151+
specifiedTests: Set(c.tests ?? []),
151152
tags: c.tags ?? [],
152153
skipTags: c.skipTags ?? [.unstable, .skip])
153154

@@ -159,7 +160,7 @@ struct TestConfig {
159160
Verbose: \(verbose)
160161
IterScale: \(iterationScale)
161162
FixedIters: \(fixedNumIters)
162-
Tests Filter: \(c.filters ?? [])
163+
Tests Filter: \(c.tests ?? [])
163164
Tests to run: \(testList)
164165
165166
--- DATA ---\n
@@ -171,22 +172,21 @@ struct TestConfig {
171172
///
172173
/// - Parameters:
173174
/// - registeredBenchmarks: List of all performance tests to be filtered.
174-
/// - filters: List of explicitly specified tests to run. These can be
175+
/// - specifiedTests: List of explicitly specified tests to run. These can be
175176
/// specified either by a test name or a test number.
176177
/// - tags: Run tests tagged with all of these categories.
177178
/// - skipTags: Don't run tests tagged with any of these categories.
178179
/// - Returns: An array of test number and benchmark info tuples satisfying
179180
/// specified filtering conditions.
180181
static func filterTests(
181182
_ registeredBenchmarks: [BenchmarkInfo],
182-
filters: [String],
183+
specifiedTests: Set<String>,
183184
tags: Set<BenchmarkCategory>,
184185
skipTags: Set<BenchmarkCategory>
185186
) -> [(index: String, info: BenchmarkInfo)] {
186187
let indices = Dictionary(uniqueKeysWithValues:
187188
zip(registeredBenchmarks.sorted().map { $0.name },
188189
(1...).lazy.map { String($0) } ))
189-
let specifiedTests = Set(filters)
190190

191191
func byTags(b: BenchmarkInfo) -> Bool {
192192
return b.tags.isSuperset(of: tags) &&
@@ -197,7 +197,7 @@ struct TestConfig {
197197
specifiedTests.contains(indices[b.name]!)
198198
} // !! "All registeredBenchmarks have been assigned an index"
199199
return registeredBenchmarks
200-
.filter(filters.isEmpty ? byTags : byNamesOrIndices)
200+
.filter(specifiedTests.isEmpty ? byTags : byNamesOrIndices)
201201
.map { (index: indices[$0.name]!, info: $0) }
202202
}
203203
}

0 commit comments

Comments
 (0)