mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
47 lines
839 B
Swift
47 lines
839 B
Swift
import Foundation
|
|
import VGSL
|
|
|
|
public final class TimeMeasure: @unchecked Sendable {
|
|
public struct Time {
|
|
public var value: Int
|
|
public var status: Status
|
|
|
|
public var description: String {
|
|
"\(status.rawValue.capitalized): \(value)"
|
|
}
|
|
}
|
|
|
|
public enum Status: String {
|
|
case cold
|
|
case warm
|
|
}
|
|
|
|
private var startTime: Date?
|
|
private var status: Status = .cold
|
|
|
|
public private(set) var time: Time?
|
|
|
|
init() {}
|
|
|
|
public func updateMeasure<T>(action: () throws -> T) throws -> T {
|
|
start()
|
|
let result = try action()
|
|
stop()
|
|
return result
|
|
}
|
|
|
|
private func start() {
|
|
startTime = Date()
|
|
}
|
|
|
|
private func stop() {
|
|
if let startTime {
|
|
time = Time(
|
|
value: Int(Date().timeIntervalSince(startTime) * 1000),
|
|
status: status
|
|
)
|
|
status = .warm
|
|
}
|
|
}
|
|
}
|