iOS app launch time measurement

后端 未结 6 1795
醉梦人生
醉梦人生 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 17:59

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

    0 讨论(0)
  • 2021-02-15 18:06

    A simple implementation is to start a StopWatch in the applications main and then grab the time result in the ViewDidAppear method:

    public class Application
    {
        public static Stopwatch StartUpTimer;
    
        static void Main(string[] args)
        {
                StartUpTimer = new Stopwatch();
                StartUpTimer.Start();
                UIApplication.Main(args, null, "AppDelegate");
        }
    }
    
    public class MyUIViewController : UIViewController
    {
        public override void ViewDidAppear (bool animated)
        {
    
            Console.WriteLine (Application.StartUpTimer.ElapsedMilliseconds.ToString () + "ms to startup");
            base.ViewDidAppear (animated);
        }
    }
    
    0 讨论(0)
  • 2021-02-15 18:14

    You can go to edit scheme on Xcode and add a environment variable (DYLD_PRINT_STATISTICS = 1) as shown in the image

    When you run the app the details will be printed on debugger output as below:

    Total pre-main time: 481.88 milliseconds (100.0%)
             dylib loading time:  71.70 milliseconds (14.8%)
            rebase/binding time:  53.66 milliseconds (11.1%)
                ObjC setup time:  40.04 milliseconds (8.3%)
               initializer time: 316.33 milliseconds (65.6%)
               slowest intializers :
                 libSystem.B.dylib :  16.71 milliseconds (3.4%)
    

    Please watch the video for more details.

    0 讨论(0)
  • 2021-02-15 18:16

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

    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

    0 讨论(0)
  • 2021-02-15 18:17

    Since you can't get the exact time when the user touches the app's icon on homescreen, I suggest to start your app from another app, so you can get the exact time your app starts.

    • Register a custom URL scheme in your app
    • create a helper-app that can launch your app by a url
    • log the time when the helper app launches your app (or send the timestamp to your app)
    • in your app log the time when launch is done at a appropriate place (AppDelegate or the main ViewController)
    • if you have sent the timestamp of the launch from the helper app, you can calculate the difference programmatically, otherwise just compare both times in the console
    0 讨论(0)
  • 2021-02-15 18:19

    Start a timer in willFinishLaunchingWithOptions.....

    Stop the timer in applicationDidBecomeActive, or viewDidAppear of the VC you are trying to profile, and NSLog it.

    0 讨论(0)
提交回复
热议问题