Created
February 21, 2020 08:24
-
-
Save marslin1220/a70e96f3f60afbb9577475bdef872864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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