punchnet-macos/Tun/SDLQPSCounter.swift
2025-05-12 16:25:33 +08:00

38 lines
908 B
Swift
Raw 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 {
private var count = 0
private let timer: DispatchSourceTimer
private let label: String
init(label: String) {
self.label = label
timer = DispatchSource.makeTimerSource(queue: DispatchQueue(label: "com.yourapp.qps"))
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) {
DispatchQueue(label: "com.yourapp.qps").async {
self.count += num
}
}
deinit {
timer.cancel()
}
}