Skip to content

Instantly share code, notes, and snippets.

@marslin1220
Created February 21, 2020 08:24
Show Gist options
  • Save marslin1220/a70e96f3f60afbb9577475bdef872864 to your computer and use it in GitHub Desktop.
Save marslin1220/a70e96f3f60afbb9577475bdef872864 to your computer and use it in GitHub Desktop.
import Foundation
import XCTest
import QuartzCore
// 1.
@available(iOS 11.2.0, *)
class SlowFrameMetric: NSObject, XCTMetric {
private static let slowFrameThresholdInMiliSecond = 0.017
private let frameTimeintervalCollector = FrameTimeintervalCollector()
func reportMeasurements(from startTime: XCTPerformanceMeasurementTimestamp,
to endTime: XCTPerformanceMeasurementTimestamp) throws -> [XCTPerformanceMeasurement] {
// Note: When setting invocationOptions of XCTMeasureOptions in test cases,
// the `startTime` and the `endTime` would be nil.
let filteredFrameTickTimes: Set<FrameTickTime>
let frameTickTimeSet = frameTimeintervalCollector.frameTickTimeSet
if startTime == nil || endTime == nil { //< 2.
filteredFrameTickTimes = frameTickTimeSet
frameTimeintervalCollector.reset() //< 3.
} else {
// 4.
filteredFrameTickTimes = frameTickTimeSet.filter({ frameTickTime -> Bool in
endTime.date >= frameTickTime.date && frameTickTime.date >= startTime.date
})
}
guard !filteredFrameTickTimes.isEmpty else { throw FPSMetricError.frameTickTimeIsEmpty }
// 5.
let slowFrameTickTimes = filteredFrameTickTimes.filter {
$0.frameTimeinterval > SlowFrameMetric.slowFrameThresholdInMiliSecond
}
let slowFramePrecentage = Double(slowFrameTickTimes.count) / Double(filteredFrameTickTimes.count)
return [XCTPerformanceMeasurement(identifier: "com.grindrguy.grindrx_SlowFrameMetric",
displayName: "Slow Frame",
doubleValue: slowFramePrecentage,
unitSymbol: "%")]
}
//Mark - Optional functions
func didStopMeasuring() {
frameTimeintervalCollector.displayLink.isPaused = true
}
func willBeginMeasuring() {
frameTimeintervalCollector.displayLink.isPaused = false
}
}
extension SlowFrameMetric: NSCopying {
func copy(with zone: NSZone? = nil) -> Any {
return SlowFrameMetric()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment