@@ -29,12 +29,10 @@ public class Benchmark: Encodable {
2929 }
3030
3131 /// The shared instance to use for logging.
32- public static let main : Benchmark = {
33- return Benchmark (
34- isEnabled: ProcessInfo . processInfo. environment [ " DOCC_BENCHMARK " ] == " YES " ,
35- metricsFilter: ProcessInfo . processInfo. environment [ " DOCC_BENCHMARK_FILTER " ]
36- )
37- } ( )
32+ public static let main : Benchmark = Benchmark (
33+ isEnabled: ProcessInfo . processInfo. environment [ " DOCC_BENCHMARK " ] == " YES " ,
34+ metricsFilter: ProcessInfo . processInfo. environment [ " DOCC_BENCHMARK_FILTER " ]
35+ )
3836
3937 /// The benchmark timestamp.
4038 public let date = Date ( )
@@ -71,55 +69,55 @@ public class Benchmark: Encodable {
7169 try container. encode ( platform, forKey: . platform)
7270
7371 let metrics = self . metrics. compactMap { log -> BenchmarkResult ? in
74- let id = ( log as? DynamicallyIdentifiableMetric ) ? . identifier ?? type ( of: log) . identifier
75- let displayName = ( log as? DynamicallyIdentifiableMetric ) ? . displayName ?? type ( of: log) . displayName
7672 guard let result = log. result else {
7773 return nil
7874 }
75+ let id = ( log as? DynamicallyIdentifiableMetric ) ? . identifier ?? type ( of: log) . identifier
76+ let displayName = ( log as? DynamicallyIdentifiableMetric ) ? . displayName ?? type ( of: log) . displayName
7977 return BenchmarkResult ( identifier: id, displayName: displayName, result: result)
8078 }
8179 try container. encode ( metrics, forKey: . metrics)
8280 }
8381}
8482
83+ private extension Benchmark {
84+ func shouldLogMetricType( _ metricType: BenchmarkMetric . Type ) -> Bool {
85+ return isEnabled && ( metricsFilter == nil || metricType. identifier. hasPrefix ( metricsFilter!) )
86+ }
87+ }
88+
8589/// Logs a one-off metric value.
8690/// - Parameter event: The metric to add to the log.
8791public func benchmark< E> ( add event: @autoclosure ( ) -> E , benchmarkLog log: Benchmark = . main) where E: BenchmarkMetric {
88- guard log. isEnabled else { return }
89- if log. metricsFilter == nil || E . identifier. hasPrefix ( log. metricsFilter!) {
90- log. metrics. append ( event ( ) )
91- }
92+ guard log. shouldLogMetricType ( E . self) else { return }
93+
94+ log. metrics. append ( event ( ) )
9295}
9396
9497/// Starts the given metric.
9598/// - Parameter event: The metric to start.
9699public func benchmark< E> ( begin event: @autoclosure ( ) -> E , benchmarkLog log: Benchmark = . main) -> E ? where E: BenchmarkBlockMetric {
97- guard log. isEnabled else { return nil }
98-
99- if log. metricsFilter == nil || E . identifier. hasPrefix ( log. metricsFilter!) {
100- let event = event ( )
101- event. begin ( )
102- return event
103- }
104- return nil
100+ guard log. shouldLogMetricType ( E . self) else { return nil }
101+
102+ let event = event ( )
103+ event. begin ( )
104+ return event
105105}
106106
107107/// Ends the given metric and adds it to the log.
108108/// - Parameter event: The metric to end and log.
109109public func benchmark< E> ( end event: @autoclosure ( ) -> E ? , benchmarkLog log: Benchmark = . main) where E: BenchmarkBlockMetric {
110- guard log. isEnabled, let event = event ( ) else { return }
111-
112- if log. metricsFilter == nil || E . identifier. hasPrefix ( log. metricsFilter!) {
113- event. end ( )
114- log. metrics. append ( event)
115- }
110+ guard log. shouldLogMetricType ( E . self) , let event = event ( ) else { return }
111+
112+ event. end ( )
113+ log. metrics. append ( event)
116114}
117115
118116/// Ends the given metric and adds it to the log.
119117/// - Parameter event: The metric to end and log.
120118@discardableResult
121119public func benchmark< E, Result> ( wrap event: @autoclosure ( ) -> E , benchmarkLog log: Benchmark = . main, body: ( ) throws -> Result ) rethrows -> Result where E: BenchmarkBlockMetric {
122- if log. isEnabled && ( log . metricsFilter == nil || E . identifier . hasPrefix ( log . metricsFilter! ) ) {
120+ if log. shouldLogMetricType ( E . self ) {
123121 let event = event ( )
124122 event. begin ( )
125123 let result = try body ( )
0 commit comments