@@ -70,34 +70,91 @@ public enum BenchmarkCategory : String {
70
70
case skip
71
71
}
72
72
73
+ public struct BenchmarkPlatformSet : OptionSet {
74
+ public let rawValue : Int
75
+
76
+ public init ( rawValue: Int ) {
77
+ self . rawValue = rawValue
78
+ }
79
+
80
+ public static let darwin = BenchmarkPlatformSet ( rawValue: 1 << 0 )
81
+ public static let linux = BenchmarkPlatformSet ( rawValue: 1 << 1 )
82
+
83
+ public static var currentPlatform : BenchmarkPlatformSet {
84
+ #if os(Linux)
85
+ return . linux
86
+ #else
87
+ return . darwin
88
+ #endif
89
+ }
90
+
91
+ public static var allPlatforms : BenchmarkPlatformSet {
92
+ return [ . darwin, . linux]
93
+ }
94
+ }
95
+
73
96
public struct BenchmarkInfo {
74
97
/// The name of the benchmark that should be displayed by the harness.
75
98
public var name : String
76
99
100
+ /// Shadow static variable for runFunction.
101
+ private var _runFunction : ( Int ) -> ( )
102
+
77
103
/// A function that invokes the specific benchmark routine.
78
- public var runFunction : ( Int ) -> ( )
104
+ public var runFunction : ( ( Int ) -> ( ) ) ? {
105
+ if !shouldRun {
106
+ return nil
107
+ }
108
+ return _runFunction
109
+ }
79
110
80
111
/// A set of category tags that describe this benchmark. This is used by the
81
112
/// harness to allow for easy slicing of the set of benchmarks along tag
82
113
/// boundaries, e.x.: run all string benchmarks or ref count benchmarks, etc.
83
114
public var tags : [ BenchmarkCategory ]
84
115
116
+ /// The platforms that this benchmark supports. This is an OptionSet.
117
+ private var unsupportedPlatforms : BenchmarkPlatformSet
118
+
119
+ /// Shadow variable for setUpFunction.
120
+ private var _setUpFunction : ( ( ) -> ( ) ) ?
121
+
85
122
/// An optional function that if non-null is run before benchmark samples
86
123
/// are timed.
87
- public var setUpFunction : ( ( ) -> ( ) ) ?
124
+ public var setUpFunction : ( ( ) -> ( ) ) ? {
125
+ if !shouldRun {
126
+ return nil
127
+ }
128
+ return _setUpFunction
129
+ }
130
+
131
+ /// Shadow static variable for computed property tearDownFunction.
132
+ private var _tearDownFunction : ( ( ) -> ( ) ) ?
88
133
89
134
/// An optional function that if non-null is run immediately after a sample is
90
135
/// taken.
91
- public var tearDownFunction : ( ( ) -> ( ) ) ?
136
+ public var tearDownFunction : ( ( ) -> ( ) ) ? {
137
+ if !shouldRun {
138
+ return nil
139
+ }
140
+ return _tearDownFunction
141
+ }
92
142
93
143
public init ( name: String , runFunction: @escaping ( Int ) -> ( ) , tags: [ BenchmarkCategory ] ,
94
144
setUpFunction: ( ( ) -> ( ) ) ? = nil ,
95
- tearDownFunction: ( ( ) -> ( ) ) ? = nil ) {
145
+ tearDownFunction: ( ( ) -> ( ) ) ? = nil ,
146
+ unsupportedPlatforms: BenchmarkPlatformSet = [ ] ) {
96
147
self . name = name
97
- self . runFunction = runFunction
148
+ self . _runFunction = runFunction
98
149
self . tags = tags
99
- self . setUpFunction = setUpFunction
100
- self . tearDownFunction = tearDownFunction
150
+ self . _setUpFunction = setUpFunction
151
+ self . _tearDownFunction = tearDownFunction
152
+ self . unsupportedPlatforms = unsupportedPlatforms
153
+ }
154
+
155
+ /// Returns true if this benchmark should be run on the current platform.
156
+ var shouldRun : Bool {
157
+ return !unsupportedPlatforms. contains ( . currentPlatform)
101
158
}
102
159
}
103
160
0 commit comments