Last active
March 23, 2023 13:15
-
-
Save krzysztofzablocki/85a3057cea7195780aa9d5cd51d58cfa to your computer and use it in GitHub Desktop.
TCA NOOP Action round trip
This file contains hidden or 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
/// Add a noop action into your main reducer, don't do anything in that action, just return .none | |
/// __run the app in RELEASE to get all optimizations enabled __ | |
/// Add this code at the end of your app finish launching delegate call (or onAppear on main view if not using delegate) | |
// The Interquartile Range (IQR) method is a robust technique used in statistics to measure the spread of data and identify outliers. It is less sensitive to extreme values compared to other methods like standard deviation | |
func removeOutliers(from array: [Double]) -> [Double] { | |
func quartiles(of array: [Double]) -> (Double, Double) { | |
let sortedArray = array.sorted() | |
let count = sortedArray.count | |
let q1 = median(of: Array(sortedArray[0 ..< (count / 2)])) | |
let q3 = median(of: Array(sortedArray[((count + 1) / 2)...])) | |
return (q1, q3) | |
} | |
func interquartileRange(of array: [Double]) -> Double { | |
let (q1, q3) = quartiles(of: array) | |
return q3 - q1 | |
} | |
let (q1, q3) = quartiles(of: array) | |
let iqr = q3 - q1 | |
let lowerBound = q1 - 1.5 * iqr | |
let upperBound = q3 + 1.5 * iqr | |
return array.filter { $0 >= lowerBound && $0 <= upperBound } | |
} | |
// Keep track of the measurements | |
var measurements = [Double]() | |
// Create a timer that fires every second | |
let timer = Timer.scheduledTimer(withTimeInterval: measurementTicker, repeats: true) { _ in | |
let start = CFAbsoluteTimeGetCurrent() | |
self.viewStore.send(.noop) | |
let end = CFAbsoluteTimeGetCurrent() | |
let roundtripTime = (end - start) * 1000 | |
measurements.append(roundtripTime) | |
// Check if the measurement interval has elapsed | |
if measurements.count == Int(measurementInterval / measurementTicker) { | |
let filteredMeasurements = removeOutliers(from: measurements) | |
let averageRoundtripTime = measurements.reduce(0, +) / Double(measurements.count) | |
print("ROUNDTRIP(\(measurementInterval)s): \(averageRoundtripTime) ms, stddev: \(standardDeviation(of: measurements)) ms, median \(median(of: measurements)) ms") | |
// Reset the measurements for the next interval | |
measurements.removeAll() | |
} | |
} | |
// Start the timer after 30s delay | |
timer.fireDate = Date(timeIntervalSinceNow: 30) | |
timer.fire() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment