简体   繁体   English

Android - 记录启动延迟的问题

[英]Android - Problems with logging the start up latency

I am trying to log the start up latency of my app . 我正在尝试记录我的应用程序的启动延迟 They way I am doing it is setting the start time of the app on Application.onCreate and provide a public method that returns the time. 我这样做的方法是在Application.onCreate上设置应用程序的开始时间,并提供一个返回时间的公共方法。

MyApplication extends Application {
    Date startUpTime;
    //Declare variables
    @Override
    public void onCreate() {
        super.onCreate();
        setStartupTime();
        //other initializations
    }

    private void setStartUpTime() {
        startUpTime = new Date();
    }

    public Date getStartUpTime() {
        return startUpTime;
    }
}

MyActivity extends Activity {
.
.
.
    @Override
    public void onStart(){
        logStartUpLatency();
        //other onStart stuff
    }

    private void logStartUpLatency() {
        Date currentTime = new Date();
        Date startTime = (MyApplication)getApplicationContext().getStartUpTime();
        long latency = currentTime.getTime() - startTIme.getTime();
        Log.d("Start up Latency is ", Long.toString(latency)):

    }

This is how I am testing my start up latency: 这就是我测试启动延迟的方法:

  • adb install myapk adb安装myapk
  • run the app to get the first start up latency. 运行应用程序以获得第一次启动延迟。 I can see the latency logged is correct for the first start 我可以看到记录的延迟对于第一次启动是正确的
  • run the app again to test the start latency. 再次运行应用程序以测试启动延迟。 The latency logged is correct for the start(or any number of subsequent starts) 记录的延迟对于开始(或任何后续启动次数)是正确的
  • Now I increase my app's version code and name by 1. To simulate an upgrade, I used the command adb install -r myapk. 现在我将应用程序的版本代码和名称增加1.为了模拟升级,我使用了命令adb install -r myapk。
  • Now I run the app again to test the first start latency after upgrade, even though it takes 3 seconds, the latency logged is off the charts. 现在我再次运行应用程序以测试升级后的第一个启动延迟,即使需要3秒,记录的延迟也不在图表中。

Does any one know why that might happen? 有人知道为什么会这样吗?

Update 更新

So if I install the apk using "adb install -r myapk", the app isn't going through the Myapplication.onCreate() . 因此,如果我使用“adb install -r myapk”安装apk,则应用程序不会通过Myapplication.onCreate()

I suggest the use of the TimingLogger class. 我建议使用TimingLogger类。 As per the documentation, you can easily track the elapsed time and even add splits in the process. 根据文档,您可以轻松跟踪已用时间,甚至可以在流程中添加拆分。

This 这个

TimingLogger timings = new TimingLogger(TAG, "methodA");
// ... do some work A ...
timings.addSplit("work A");
// ... do some work B ...
timings.addSplit("work B");
// ... do some work C ...
timings.addSplit("work C");
timings.dumpToLog();

produces 产生

D/TAG (3459): methodA: begin
D/TAG (3459): methodA:      9 ms, work A
D/TAG (3459): methodA:      1 ms, work B
D/TAG (3459): methodA:      6 ms, work C
D/TAG (3459): methodA: end, 16 ms

The latency you are calculating is in milliseconds. 您计算的延迟以毫秒为单位。 Date#getTime() returns the number of milliseconds since Jan. 1, 1970, midnight GMT. Date#getTime()返回自1970年1月1日格林威治标准时间午夜以来的毫秒数。

The observed time of 3 seconds is because of the overhead for uninstalling the old and installing the new app build. 观察到的3秒时间是因为卸载旧应用程序和安装新应用程序构建的开销。

So if I install the apk using "adb install -r myapk", the app isn't going through the Myapplication.onCreate(). 因此,如果我使用“adb install -r myapk”安装apk,则应用程序不会通过Myapplication.onCreate()。 So that answers this question. 所以这回答了这个问题。 I will ask a separate question on why installing an application using "adb install -r myapk" and then starting myapk doesn't go through MyApplication.onCreate() 我将问一个单独的问题,为什么使用“adb install -r myapk”安装应用程序,然后启动myapk不会通过MyApplication.onCreate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM