// // SDLQPSCounter.swift // Tun // // Created by 安礼成 on 2024/4/16. // import Foundation // 计数器,用来统计qps class SDLQPSCounter: @unchecked Sendable { private var count = 0 private let timer: DispatchSourceTimer private let label: String private let queue = DispatchQueue(label: "com.punchnet.qps") init(label: String) { self.label = label timer = DispatchSource.makeTimerSource(queue: queue) timer.schedule(deadline: .now(), repeating: .seconds(1), leeway: .milliseconds(100)) timer.setEventHandler { [weak self] in guard let self = self else { return } NSLog("[\(self.label)] QPS: \(self.count)") self.count = 0 } timer.resume() } func increment(num: Int = 1) { queue.async { self.count += num } } deinit { timer.cancel() } }