diff --git a/Sources/SwiftDocC/Benchmark/Benchmark.swift b/Sources/SwiftDocC/Benchmark/Benchmark.swift index 06d1d02800..77376234e9 100644 --- a/Sources/SwiftDocC/Benchmark/Benchmark.swift +++ b/Sources/SwiftDocC/Benchmark/Benchmark.swift @@ -29,12 +29,10 @@ public class Benchmark: Encodable { } /// The shared instance to use for logging. - public static let main: Benchmark = { - return Benchmark( - isEnabled: ProcessInfo.processInfo.environment["DOCC_BENCHMARK"] == "YES", - metricsFilter: ProcessInfo.processInfo.environment["DOCC_BENCHMARK_FILTER"] - ) - }() + public static let main: Benchmark = Benchmark( + isEnabled: ProcessInfo.processInfo.environment["DOCC_BENCHMARK"] == "YES", + metricsFilter: ProcessInfo.processInfo.environment["DOCC_BENCHMARK_FILTER"] + ) /// The benchmark timestamp. public let date = Date() @@ -71,55 +69,55 @@ public class Benchmark: Encodable { try container.encode(platform, forKey: .platform) let metrics = self.metrics.compactMap { log -> BenchmarkResult? in - let id = (log as? DynamicallyIdentifiableMetric)?.identifier ?? type(of: log).identifier - let displayName = (log as? DynamicallyIdentifiableMetric)?.displayName ?? type(of: log).displayName guard let result = log.result else { return nil } + let id = (log as? DynamicallyIdentifiableMetric)?.identifier ?? type(of: log).identifier + let displayName = (log as? DynamicallyIdentifiableMetric)?.displayName ?? type(of: log).displayName return BenchmarkResult(identifier: id, displayName: displayName, result: result) } try container.encode(metrics, forKey: .metrics) } } +private extension Benchmark { + func shouldLogMetricType(_ metricType: BenchmarkMetric.Type) -> Bool { + return isEnabled && (metricsFilter == nil || metricType.identifier.hasPrefix(metricsFilter!)) + } +} + /// Logs a one-off metric value. /// - Parameter event: The metric to add to the log. public func benchmark(add event: @autoclosure () -> E, benchmarkLog log: Benchmark = .main) where E: BenchmarkMetric { - guard log.isEnabled else { return } - if log.metricsFilter == nil || E.identifier.hasPrefix(log.metricsFilter!) { - log.metrics.append(event()) - } + guard log.shouldLogMetricType(E.self) else { return } + + log.metrics.append(event()) } /// Starts the given metric. /// - Parameter event: The metric to start. public func benchmark(begin event: @autoclosure () -> E, benchmarkLog log: Benchmark = .main) -> E? where E: BenchmarkBlockMetric { - guard log.isEnabled else { return nil } - - if log.metricsFilter == nil || E.identifier.hasPrefix(log.metricsFilter!) { - let event = event() - event.begin() - return event - } - return nil + guard log.shouldLogMetricType(E.self) else { return nil } + + let event = event() + event.begin() + return event } /// Ends the given metric and adds it to the log. /// - Parameter event: The metric to end and log. public func benchmark(end event: @autoclosure () -> E?, benchmarkLog log: Benchmark = .main) where E: BenchmarkBlockMetric { - guard log.isEnabled, let event = event() else { return } - - if log.metricsFilter == nil || E.identifier.hasPrefix(log.metricsFilter!) { - event.end() - log.metrics.append(event) - } + guard log.shouldLogMetricType(E.self), let event = event() else { return } + + event.end() + log.metrics.append(event) } /// Ends the given metric and adds it to the log. /// - Parameter event: The metric to end and log. @discardableResult public func benchmark(wrap event: @autoclosure () -> E, benchmarkLog log: Benchmark = .main, body: () throws -> Result) rethrows -> Result where E: BenchmarkBlockMetric { - if log.isEnabled && (log.metricsFilter == nil || E.identifier.hasPrefix(log.metricsFilter!)) { + if log.shouldLogMetricType(E.self) { let event = event() event.begin() let result = try body()