diff --git a/Source/SwiftLintFramework/Helpers/CustomRuleTimer.swift b/Source/SwiftLintFramework/Helpers/CustomRuleTimer.swift new file mode 100644 index 000000000..af131bb26 --- /dev/null +++ b/Source/SwiftLintFramework/Helpers/CustomRuleTimer.swift @@ -0,0 +1,33 @@ +import Foundation + +/// Utility to measure the time spent in each custom rule. +public final class CustomRuleTimer { + private let lock = NSLock() + private var ruleIDForTimes = [String: [TimeInterval]]() + private var shouldRecord = false + + /// Singleton. + public static let shared = CustomRuleTimer() + + /// Tell the timer it should record time spent in rules. + public func activate() { + shouldRecord = true + } + + /// Return all time spent for each custom rule, keyed by rule ID. + public func dump() -> [String: TimeInterval] { + ruleIDForTimes.mapValues { $0.reduce(0, +) } + } + + /// Register time spent evaluating a rule with the specified ID. + /// + /// - parameter time: The time interval spent evaluating this rule ID. + /// - parameter ruleID: The ID of the rule that was evaluated. + func register(time: TimeInterval, forRuleID ruleID: String) { + guard shouldRecord else { return } + + lock.lock() + defer { lock.unlock() } + ruleIDForTimes[ruleID, default: []].append(time) + } +} diff --git a/Source/SwiftLintFramework/Rules/Style/CustomRules.swift b/Source/SwiftLintFramework/Rules/Style/CustomRules.swift index ec3d1441e..dedd34738 100644 --- a/Source/SwiftLintFramework/Rules/Style/CustomRules.swift +++ b/Source/SwiftLintFramework/Rules/Style/CustomRules.swift @@ -1,29 +1,5 @@ import Foundation -public final class CustomRuleTimer { - private let lock = NSLock() - private var ruleIDForTimes = [String: [TimeInterval]]() - fileprivate var shouldRecord = false - - public static let shared = CustomRuleTimer() - - public func activate() { - shouldRecord = true - } - - func register(time: TimeInterval, forRuleID ruleID: String) { - guard shouldRecord else { return } - - lock.lock() - defer { lock.unlock() } - ruleIDForTimes[ruleID, default: []].append(time) - } - - public func dump() -> [String: TimeInterval] { - ruleIDForTimes.mapValues { $0.reduce(0, +) } - } -} - private extension Region { func isRuleDisabled(customRuleIdentifier: String) -> Bool { return disabledRuleIdentifiers.contains(RuleIdentifier(customRuleIdentifier))