38 lines
908 B
Swift
38 lines
908 B
Swift
//
|
||
// 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()
|
||
}
|
||
}
|