Forked from DenTelezhkin/MeasureAppStartupTime.swift
Created
September 25, 2022 00:48
-
-
Save davidleee/2c1e92aa4f0b35cca5a04a3e4399982f to your computer and use it in GitHub Desktop.
Measure iOS app startup time, in seconds, from the time user tapped an icon on the home screen (using time, when app process was created). Swift 4.
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
// Returns number of seconds passed between time when process was created and function was called | |
func measureAppStartUpTime() -> Double { | |
var kinfo = kinfo_proc() | |
var size = MemoryLayout<kinfo_proc>.stride | |
var mib : [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()] | |
sysctl(&mib, u_int(mib.count), &kinfo, &size, nil, 0) | |
let start_time = kinfo.kp_proc.p_starttime | |
var time : timeval = timeval(tv_sec: 0, tv_usec: 0) | |
gettimeofday(&time, nil) | |
let currentTimeMilliseconds = Double(Int64(time.tv_sec) * 1000) + Double(time.tv_usec) / 1000.0 | |
let processTimeMilliseconds = Double(Int64(start_time.tv_sec) * 1000) + Double(start_time.tv_usec) / 1000.0 | |
return (currentTimeMilliseconds - processTimeMilliseconds) / 1000.0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment