[Add] 增加APP启动时长计算
Signed-off-by: donghongyu <donghongyu@zhidaoauto.com>
This commit is contained in:
@@ -87,6 +87,7 @@ dependencies {
|
||||
implementation rootProject.ext.dependencies.moduleservice
|
||||
|
||||
implementation rootProject.ext.dependencies.mogo_core_data
|
||||
implementation rootProject.ext.dependencies.mogo_core_utils
|
||||
implementation rootProject.ext.dependencies.mogo_core_function_obu_mogo
|
||||
implementation rootProject.ext.dependencies.mogo_core_function_smp
|
||||
implementation rootProject.ext.dependencies.mogo_core_function_hmi
|
||||
@@ -123,6 +124,7 @@ dependencies {
|
||||
implementation project(':modules:mogo-module-service')
|
||||
|
||||
implementation project(':core:mogo-core-data')
|
||||
implementation project(':core:mogo-core-utils')
|
||||
implementation project(':core:function-impl:mogo-core-function-obu-mogo')
|
||||
implementation project(':core:function-impl:mogo-core-function-smp')
|
||||
implementation project(':core:function-impl:mogo-core-function-hmi')
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.mogo.commons.network.Utils;
|
||||
import com.mogo.eagle.core.data.constants.MoGoConfig;
|
||||
import com.mogo.eagle.core.data.constants.MogoServicePaths;
|
||||
import com.mogo.eagle.core.data.map.MogoLocation;
|
||||
import com.mogo.eagle.core.utilcode.mogo.AppLaunchTimeUtils;
|
||||
import com.mogo.map.MapApiPath;
|
||||
import com.mogo.module.carchatting.card.CallChatConstant;
|
||||
import com.mogo.module.common.MogoApisHandler;
|
||||
@@ -344,6 +345,10 @@ public abstract class MainMoGoApplication extends AbsMogoApplication {
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
super.attachBaseContext(base);
|
||||
/**如果是主进程**/
|
||||
if (ProcessUtils.isMainProcess(this)) {
|
||||
AppLaunchTimeUtils.beginTimeCalculate(AppLaunchTimeUtils.COLD_START);
|
||||
}
|
||||
BoostMultiDex.install(base);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.mogo.eagle.core.utilcode.mogo;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* 应用启动计时工具
|
||||
*/
|
||||
public class AppLaunchTimeUtils {
|
||||
private static HashMap<String, Long> sCalTimeMap = new HashMap<>();
|
||||
public static final String COLD_START = "cold_start";
|
||||
public static final String HOT_START = "hot_start";
|
||||
public static long sColdStartTime = 0;
|
||||
|
||||
/**
|
||||
* 记录某个事件的开始时间
|
||||
*
|
||||
* @param key 事件名称
|
||||
*/
|
||||
public static void beginTimeCalculate(String key) {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
sCalTimeMap.put(key, currentTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个事件的运行时间
|
||||
*
|
||||
* @param key 事件名称
|
||||
* @return 返回某个事件的运行时间,调用这个方法之前没有调用 {@link #beginTimeCalculate(String)} 则返回-1
|
||||
*/
|
||||
public static long getTimeCalculate(String key) {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
Long beginTime = sCalTimeMap.get(key);
|
||||
if (beginTime == null) {
|
||||
return -1;
|
||||
} else {
|
||||
sCalTimeMap.remove(key);
|
||||
return currentTime - beginTime;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除某个时间运行时间计时
|
||||
*
|
||||
* @param key 事件名称
|
||||
*/
|
||||
public static void clearTimeCalculate(String key) {
|
||||
sCalTimeMap.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除启动时间计时
|
||||
*/
|
||||
public static void clearStartTimeCalculate() {
|
||||
clearTimeCalculate(HOT_START);
|
||||
clearTimeCalculate(COLD_START);
|
||||
sColdStartTime = 0;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import com.mogo.commons.mvp.MvpActivity;
|
||||
import com.mogo.commons.mvp.MvpFragment;
|
||||
import com.mogo.eagle.core.data.constants.MoGoFragmentPaths;
|
||||
import com.mogo.eagle.core.data.map.MogoLocation;
|
||||
import com.mogo.eagle.core.utilcode.mogo.AppLaunchTimeUtils;
|
||||
import com.mogo.eagle.core.utilcode.mogo.toast.ResourcesHelper;
|
||||
import com.mogo.map.location.IMogoLocationListener;
|
||||
import com.mogo.map.uicontroller.EnumMapUI;
|
||||
@@ -154,6 +155,7 @@ public class MainActivity extends MvpActivity<MainView, MainPresenter> implement
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
calculateStartTime();
|
||||
ContextHolderUtil.holdContext(this);
|
||||
mPresenter.postLoadModuleMsg();
|
||||
NetworkUtils.listenNetStrength(this);
|
||||
@@ -163,6 +165,36 @@ public class MainActivity extends MvpActivity<MainView, MainPresenter> implement
|
||||
mPresenter.checkPermission(this);
|
||||
}
|
||||
|
||||
private void calculateStartTime() {
|
||||
long coldStartTime = AppLaunchTimeUtils.getTimeCalculate(AppLaunchTimeUtils.COLD_START);
|
||||
// 这里记录的TimeUtils.coldStartTime是指Application启动的时间,最终的冷启动时间等于Application启动时间+热启动时间
|
||||
AppLaunchTimeUtils.sColdStartTime = coldStartTime > 0 ? coldStartTime : 0;
|
||||
AppLaunchTimeUtils.beginTimeCalculate(AppLaunchTimeUtils.HOT_START);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onWindowFocusChanged(boolean hasFocus) {
|
||||
if (hasFocus) {
|
||||
long hotStartTime = AppLaunchTimeUtils.getTimeCalculate(AppLaunchTimeUtils.HOT_START);
|
||||
if (AppLaunchTimeUtils.sColdStartTime > 0 && hotStartTime > 0) {
|
||||
// 真正的冷启动时间 = Application启动时间 + 热启动时间
|
||||
long coldStartTime = AppLaunchTimeUtils.sColdStartTime + hotStartTime;
|
||||
// 过滤掉异常启动时间
|
||||
if (coldStartTime < 50000) {
|
||||
// 上传冷启动时间coldStartTime
|
||||
Logger.i(TAG, "coldStartTime:" + coldStartTime);
|
||||
}
|
||||
} else if (hotStartTime > 0) {
|
||||
// 过滤掉异常启动时间
|
||||
if (hotStartTime < 30000) {
|
||||
// 上传热启动时间hotStartTime
|
||||
Logger.i(TAG, "hotStartTime:" + hotStartTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void init() {
|
||||
if (mServiceApis == null) {
|
||||
mServiceApis = MogoApisHandler.getInstance().getApis();
|
||||
|
||||
Reference in New Issue
Block a user