应用启动框架AppStartup sample增加耗时统计

Signed-off-by: sunxuhui <sunxuhui7@huawei.com>
This commit is contained in:
sunxuhui 2024-07-30 11:44:28 +08:00
parent a74915efdb
commit 6f575f79f9
4 changed files with 59 additions and 1 deletions

View File

@ -1,7 +1,8 @@
import StartupTask from '@ohos.app.appstartup.StartupTask';
import common from '@ohos.app.ability.common';
import { KvManagerUtil } from '../util/SingleKVStore';
import { KvManagerUtil, TimeUtils } from '../util/SingleKVStore';
import distributedKVStore from '@ohos.data.distributedKVStore';
import systemDateTime from '@ohos.systemDateTime';
@Sendable
export default class KvManagerUtilTask extends StartupTask {
@ -10,6 +11,8 @@ export default class KvManagerUtilTask extends StartupTask {
}
async init(context: common.AbilityStageContext): Promise<distributedKVStore.KVManager> {
let time = systemDateTime.getTime(false);
TimeUtils.startTime = time;
return KvManagerUtil.getInstance(context);
}

View File

@ -3,6 +3,9 @@ import StartupConfigEntry from '@ohos.app.appstartup.StartupConfigEntry';
import StartupListener from '@ohos.app.appstartup.StartupListener';
import hilog from '@ohos.hilog';
import { BusinessError } from '@ohos.base'
import systemDateTime from '@ohos.systemDateTime';
import Logger from '../util/Logger';
import { TimeUtils } from '../util/SingleKVStore';
export default class MyStartupConfigEntry extends StartupConfigEntry {
onConfig() {
@ -12,6 +15,9 @@ export default class MyStartupConfigEntry extends StartupConfigEntry {
if (error) {
hilog.info(0x0000, 'testTag', 'onCompletedCallback: %{public}d, message: %{public}s', error.code, error.message);
} else {
let time = systemDateTime.getTime(false);
TimeUtils.endTime = time;
Logger.info('testTag startup init -> success time--:' + TimeUtils.countTime() + 'ms');
hilog.info(0x0000, 'testTag', `onCompletedCallback: success.`);
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import hilog from '@ohos.hilog';
const DOMAIN: number = 0xF811;
const PREFIX = 'Sample_AppStartup';
const FORMAT = '%{public}s, %{public}s';
class Logger {
debug(...args: string[]): void {
hilog.debug(DOMAIN, PREFIX, FORMAT, args);
}
info(...args: string[]): void {
hilog.info(DOMAIN, PREFIX, FORMAT, args);
}
warn(...args: string[]): void {
hilog.warn(DOMAIN, PREFIX, FORMAT, args);
}
error(...args: string[]): void {
hilog.error(DOMAIN, PREFIX, FORMAT, args);
}
}
export default new Logger();

View File

@ -92,4 +92,13 @@ export class RdbStoreUtil {
});
}
}
export class TimeUtils {
static startTime: number = 0;
static endTime: number = 0;
static countTime(): number {
return this.endTime - this.startTime;
}
}