[英]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: 这就是我测试启动延迟的方法:
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.