Move `CustomRuleTimer` to its own file (#4938)

And add docs.
This commit is contained in:
JP Simard 2023-04-26 11:56:04 -04:00 committed by GitHub
parent ea6b3b4d64
commit 2544dc79d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 24 deletions

View File

@ -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)
}
}

View File

@ -1,29 +1,5 @@
import Foundation 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 { private extension Region {
func isRuleDisabled(customRuleIdentifier: String) -> Bool { func isRuleDisabled(customRuleIdentifier: String) -> Bool {
return disabledRuleIdentifiers.contains(RuleIdentifier(customRuleIdentifier)) return disabledRuleIdentifiers.contains(RuleIdentifier(customRuleIdentifier))