iOS app launch time measurement

Viewed 8369

How can I count the time it takes from the moment the user presses the launch button on the home screen until the moment the app is active (for example, until the viewDidAppear method of the first view controller)?

Id does not need to be programmatically but must be reliable.

Analogously in Android, logcat can be used for this effect.

6 Answers

We used DYLD_PRINT_STATISTICS to measure our app’s pre-main() launch times

enter image description here

There is a possibility to get process start time with C api:

#import <sys/sysctl.h>

static CFTimeInterval processStartTime() {
    size_t len = 4;
    int mib[len];
    struct kinfo_proc kp;

    sysctlnametomib("kern.proc.pid", mib, &len);
    mib[3] = getpid();
    len = sizeof(kp);
    sysctl(mib, 4, &kp, &len, NULL, 0);

    struct timeval startTime = kp.kp_proc.p_un.__p_starttime;
    return startTime.tv_sec + startTime.tv_usec / 1e6;
}

In swift you will have something like this:

private static func processStartTime() -> TimeInterval {
    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 startTime = kinfo.kp_proc.p_starttime
    return TimeInterval(startTime.tv_sec) + TimeInterval(startTime.tv_usec) / 1e6
}

After that you can calculate startup time with the different ways.

For example I do:

let currentTime = CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970
let startupTime = currentTime - processStartTime()

Full post about that I found here

Related