swiftlib_sdlan/Sources/Punchnet/SDLQPSCounter.swift
2025-07-23 12:15:38 +08:00

39 lines
926 B
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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()
}
}