Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions Sources/SwiftDocC/Benchmark/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<E>(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<E>(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<E>(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<E, Result>(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()
Expand Down