perf[zfoo]: 增加模块启动耗时检查

This commit is contained in:
jaysunxiao
2021-08-28 15:40:50 +08:00
parent 575b53142d
commit 2b373e87bb
5 changed files with 56 additions and 1 deletions
@@ -25,6 +25,7 @@ import com.zfoo.net.task.TaskManager;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.util.ReflectionUtils;
import com.zfoo.scheduler.SchedulerContext;
import com.zfoo.scheduler.model.StopWatch;
import com.zfoo.util.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -91,6 +92,7 @@ public class NetContext implements ApplicationListener<ApplicationContextEvent>,
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
if (event instanceof ContextRefreshedEvent) {
var stopWatch = new StopWatch();
NetContext.instance = this;
instance.applicationContext = event.getApplicationContext();
instance.configManager = applicationContext.getBean(IConfigManager.class);
@@ -102,6 +104,7 @@ public class NetContext implements ApplicationListener<ApplicationContextEvent>,
instance.packetService.init();
instance.configManager.initRegistry();
logger.info("net start success and cost [{}] second", stopWatch.costSecond());
} else if (event instanceof ContextClosedEvent) {
shutdownBefore();
shutdownAfter();
@@ -20,6 +20,7 @@ import com.zfoo.orm.model.accessor.IAccessor;
import com.zfoo.orm.model.query.IQuery;
import com.zfoo.protocol.util.ReflectionUtils;
import com.zfoo.scheduler.SchedulerContext;
import com.zfoo.scheduler.model.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
@@ -76,6 +77,7 @@ public class OrmContext implements ApplicationListener<ApplicationContextEvent>,
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
if (event instanceof ContextRefreshedEvent) {
var stopWatch = new StopWatch();
OrmContext.instance = this;
instance.applicationContext = event.getApplicationContext();
@@ -86,6 +88,8 @@ public class OrmContext implements ApplicationListener<ApplicationContextEvent>,
instance.ormManager.initBefore();
instance.ormManager.inject();
instance.ormManager.initAfter();
logger.info("orm start success and cost [{}] second", stopWatch.costSecond());
} else if (event instanceof ContextClosedEvent) {
shutdownBefore();
shutdownBetween();
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2020 The zfoo Authors
* 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.
*/
package com.zfoo.scheduler.model;
import com.zfoo.scheduler.util.TimeUtils;
/**
* @author jaysunxiao
* @version 3.0
*/
public class StopWatch {
private long startTime = TimeUtils.currentTimeMillis();
public long cost() {
return TimeUtils.currentTimeMillis() - startTime;
}
public float costSecond() {
return (TimeUtils.currentTimeMillis() - startTime) / 1000F;
}
public long getStartTime() {
return startTime;
}
}
+6 -1
View File
@@ -105,7 +105,12 @@
<dependencies>
<!-- 依赖的utils类库 -->
<dependency>
<groupId>com.zfoo</groupId>
<artifactId>scheduler</artifactId>
<version>${zfoo.util.version}</version>
</dependency>
<dependency>
<groupId>com.zfoo</groupId>
<artifactId>util</artifactId>
@@ -14,8 +14,11 @@
package com.zfoo.storage;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.scheduler.model.StopWatch;
import com.zfoo.storage.interpreter.IResourceReader;
import com.zfoo.storage.manager.IStorageManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
@@ -31,6 +34,8 @@ import org.springframework.core.convert.ConversionService;
*/
public class StorageContext implements ApplicationListener<ApplicationContextEvent>, Ordered {
private static final Logger logger = LoggerFactory.getLogger(StorageContext.class);
private static StorageContext instance;
private ApplicationContext applicationContext;
@@ -69,6 +74,7 @@ public class StorageContext implements ApplicationListener<ApplicationContextEve
public void onApplicationEvent(ApplicationContextEvent event) {
if (event instanceof ContextRefreshedEvent) {
var stopWatch = new StopWatch();
// 初始化上下文
StorageContext.instance = this;
instance.applicationContext = event.getApplicationContext();
@@ -84,6 +90,7 @@ public class StorageContext implements ApplicationListener<ApplicationContextEve
// 移除没有被引用的不必要资源,为了节省服务器内存
instance.storageManager.initAfter();
logger.info("storage start success and cost [{}] second", stopWatch.costSecond());
} else if (event instanceof ContextClosedEvent) {
}