@@ -34,15 +34,6 @@ struct TestConfig {
34
34
/// The delimiter to use when printing output.
35
35
let delim : String
36
36
37
- /// The filters applied to our test names.
38
- let filters : [ String ]
39
-
40
- /// The tags that we want to run
41
- let tags : Set < BenchmarkCategory >
42
-
43
- /// Tests tagged with any of these will not be executed
44
- let skipTags : Set < BenchmarkCategory >
45
-
46
37
/// The scalar multiple of the amount of times a test should be run. This
47
38
/// enables one to cause tests to run for N iterations longer than they
48
39
/// normally would. This is useful when one wishes for a test to run for a
@@ -65,20 +56,20 @@ struct TestConfig {
65
56
let afterRunSleep : Int ?
66
57
67
58
/// The list of tests to run.
68
- var tests = [ ( index: String, info: BenchmarkInfo) ] ( )
59
+ let tests : [ ( index: String , info: BenchmarkInfo ) ]
69
60
70
61
let action : TestAction
71
62
72
- init ( ) throws {
63
+ init ( _ registeredBenchmarks : [ BenchmarkInfo ] ) throws {
73
64
74
65
struct PartialTestConfig {
75
66
var delim : String ?
76
- var filters : [ String ] ?
77
67
var tags , skipTags : Set < BenchmarkCategory > ?
78
68
var iterationScale , numSamples , afterRunSleep : Int ?
79
69
var fixedNumIters : UInt ?
80
70
var verbose : Bool ?
81
71
var action : TestAction ?
72
+ var filters : [ String ] ?
82
73
}
83
74
var c = PartialTestConfig ( )
84
75
@@ -91,8 +82,6 @@ struct TestConfig {
91
82
throw ArgumentError . general ( " Failed to parse arguments " )
92
83
}
93
84
94
- filters = benchArgs. positionalArgs
95
-
96
85
func checked< T> (
97
86
_ parse: ( String ) throws -> T ? ,
98
87
_ value: String ,
@@ -134,6 +123,7 @@ struct TestConfig {
134
123
try checked ( { BenchmarkCategory ( rawValue: $0) } , $0) } )
135
124
}
136
125
126
+ // Parse command line arguments
137
127
try optionalArg ( " --iter-scale " , \. iterationScale) { Int ( $0) }
138
128
try optionalArg ( " --num-iters " , \. fixedNumIters) { UInt ( $0) }
139
129
try optionalArg ( " --num-samples " , \. numSamples) { Int ( $0) }
@@ -146,32 +136,62 @@ struct TestConfig {
146
136
try optionalArg ( " --list " , \. action, defaultValue: . listTests)
147
137
try optionalArg ( " --help " , \. action, defaultValue: . help( validOptions) )
148
138
139
+ c. filters = benchArgs. positionalArgs
140
+
149
141
// Configure from the command line arguments, filling in the defaults.
150
142
delim = c. delim ?? " , "
151
- self . tags = c. tags ?? [ ]
152
- skipTags = c. skipTags ?? [ . unstable, . skip]
153
143
iterationScale = c. iterationScale ?? 1
154
144
fixedNumIters = c. fixedNumIters ?? 0
155
145
numSamples = c. numSamples ?? 1
156
146
verbose = c. verbose ?? false
157
147
afterRunSleep = c. afterRunSleep
158
148
action = c. action ?? . run
159
- // TODO: filters, tests
149
+ tests = TestConfig . filterTests ( registeredBenchmarks,
150
+ filters: c. filters ?? [ ] ,
151
+ tags: c. tags ?? [ ] ,
152
+ skipTags: c. skipTags ?? [ . unstable, . skip] )
153
+
154
+ if verbose {
155
+ let testList = tests. map ( { $0. 1 . name } ) . joined ( separator: " , " )
156
+ print ( """
157
+ --- CONFIG ---
158
+ NumSamples: \( numSamples)
159
+ Verbose: \( verbose)
160
+ IterScale: \( iterationScale)
161
+ FixedIters: \( fixedNumIters)
162
+ Tests Filter: \( c. filters ?? [ ] )
163
+ Tests to run: \( testList)
164
+
165
+ --- DATA --- \n
166
+ """ )
167
+ }
160
168
}
161
169
162
- mutating func findTestsToRun( ) {
163
- registeredBenchmarks. sort ( )
170
+ /// Returns the list of tests to run.
171
+ ///
172
+ /// - Parameters:
173
+ /// - registeredBenchmarks: List of all performance tests to be filtered.
174
+ /// - filters: List of explicitly specified tests to run. These can be
175
+ /// specified either by a test name or a test number.
176
+ /// - tags: Run tests tagged with all of these categories.
177
+ /// - skipTags: Don't run tests tagged with any of these categories.
178
+ /// - Returns: An array of test number and benchmark info tuples satisfying
179
+ /// specified filtering conditions.
180
+ static func filterTests(
181
+ _ registeredBenchmarks: [ BenchmarkInfo ] ,
182
+ filters: [ String ] ,
183
+ tags: Set < BenchmarkCategory > ,
184
+ skipTags: Set < BenchmarkCategory >
185
+ ) -> [ ( index: String , info: BenchmarkInfo ) ] {
164
186
let indices = Dictionary ( uniqueKeysWithValues:
165
- zip ( registeredBenchmarks. map { $0. name } ,
187
+ zip ( registeredBenchmarks. sorted ( ) . map { $0. name } ,
166
188
( 1 ... ) . lazy. map { String ( $0) } ) )
167
189
let benchmarkNamesOrIndices = Set ( filters)
168
- // needed so we don't capture an ivar of a mutable inout self.
169
- let ( _tags, _skipTags) = ( tags, skipTags)
170
190
171
- tests = registeredBenchmarks. filter { benchmark in
191
+ return registeredBenchmarks. filter { benchmark in
172
192
if benchmarkNamesOrIndices. isEmpty {
173
- return benchmark. tags. isSuperset ( of: _tags ) &&
174
- benchmark. tags. isDisjoint ( with: _skipTags )
193
+ return benchmark. tags. isSuperset ( of: tags ) &&
194
+ benchmark. tags. isDisjoint ( with: skipTags )
175
195
} else {
176
196
return benchmarkNamesOrIndices. contains ( benchmark. name) ||
177
197
benchmarkNamesOrIndices. contains ( indices [ benchmark. name] !)
@@ -384,23 +404,6 @@ func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
384
404
maxRSS: UInt64 ( sampler. measureMemoryUsage ( ) ) )
385
405
}
386
406
387
- func printRunInfo( _ c: TestConfig ) {
388
- if c. verbose {
389
- print ( " --- CONFIG --- " )
390
- print ( " NumSamples: \( c. numSamples) " )
391
- print ( " Verbose: \( c. verbose) " )
392
- print ( " IterScale: \( c. iterationScale) " )
393
- if c. fixedNumIters != 0 {
394
- print ( " FixedIters: \( c. fixedNumIters) " )
395
- }
396
- print ( " Tests Filter: \( c. filters) " )
397
- print ( " Tests to run: " , terminator: " " )
398
- print ( c. tests. map ( { $0. 1 . name } ) . joined ( separator: " , " ) )
399
- print ( " " )
400
- print ( " --- DATA --- " )
401
- }
402
- }
403
-
404
407
/// Execute benchmarks and continuously report the measurement results.
405
408
func runBenchmarks( _ c: TestConfig ) {
406
409
let withUnit = { $0 + " (us) " }
@@ -440,24 +443,21 @@ func runBenchmarks(_ c: TestConfig) {
440
443
441
444
public func main( ) {
442
445
do {
443
- var config = try TestConfig ( )
446
+ let config = try TestConfig ( registeredBenchmarks )
444
447
switch ( config. action) {
445
448
case let . help( validOptions) :
446
449
print ( " Valid options: " )
447
450
for v in validOptions {
448
451
print ( " \( v) " )
449
452
}
450
453
case . listTests:
451
- config. findTestsToRun ( )
452
454
print ( " # \( config. delim) Test \( config. delim) [Tags] " )
453
455
for (index, t) in config. tests {
454
456
let testDescription = [ String ( index) , t. name, t. tags. sorted ( ) . description]
455
457
. joined ( separator: config. delim)
456
458
print ( testDescription)
457
459
}
458
460
case . run:
459
- config. findTestsToRun ( )
460
- printRunInfo ( config)
461
461
runBenchmarks ( config)
462
462
if let x = config. afterRunSleep {
463
463
sleep ( UInt32 ( x) )
0 commit comments