@@ -70,34 +70,91 @@ public enum BenchmarkCategory : String {
7070 case skip
7171}
7272
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+
7396public struct BenchmarkInfo {
7497 /// The name of the benchmark that should be displayed by the harness.
7598 public var name : String
7699
100+ /// Shadow static variable for runFunction.
101+ private var _runFunction : ( Int ) -> ( )
102+
77103 /// 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+ }
79110
80111 /// A set of category tags that describe this benchmark. This is used by the
81112 /// harness to allow for easy slicing of the set of benchmarks along tag
82113 /// boundaries, e.x.: run all string benchmarks or ref count benchmarks, etc.
83114 public var tags : [ BenchmarkCategory ]
84115
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+
85122 /// An optional function that if non-null is run before benchmark samples
86123 /// 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 : ( ( ) -> ( ) ) ?
88133
89134 /// An optional function that if non-null is run immediately after a sample is
90135 /// taken.
91- public var tearDownFunction : ( ( ) -> ( ) ) ?
136+ public var tearDownFunction : ( ( ) -> ( ) ) ? {
137+ if !shouldRun {
138+ return nil
139+ }
140+ return _tearDownFunction
141+ }
92142
93143 public init ( name: String , runFunction: @escaping ( Int ) -> ( ) , tags: [ BenchmarkCategory ] ,
94144 setUpFunction: ( ( ) -> ( ) ) ? = nil ,
95- tearDownFunction: ( ( ) -> ( ) ) ? = nil ) {
145+ tearDownFunction: ( ( ) -> ( ) ) ? = nil ,
146+ unsupportedPlatforms: BenchmarkPlatformSet = [ ] ) {
96147 self . name = name
97- self . runFunction = runFunction
148+ self . _runFunction = runFunction
98149 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)
101158 }
102159}
103160
0 commit comments