iOS app launch time measurement

后端 未结 6 1796
醉梦人生
醉梦人生 2021-02-15 17:19

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

6条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-15 18:16

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

    #import 
    
    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;
    }
    

    You can also do this in swift, but that would be wordier. Example can be found in CwlUtils.

    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

提交回复
热议问题