Search in sources :

Example 6 with ChildrenStat

use of com.alipay.sofa.boot.startup.ChildrenStat in project sofa-boot by sofastack.

the class StartupContextRefreshedListener method start.

@Override
public void start() {
    StartupReporter startupReporter;
    try {
        startupReporter = applicationContext.getBean(StartupReporter.class);
    } catch (NoSuchBeanDefinitionException e) {
        // just happen in unit tests
        SofaLogger.warn("Failed to found bean StartupReporter", e);
        return;
    }
    // init ContextRefreshStageStat
    ChildrenStat<ModuleStat> stat = new ChildrenStat<>();
    stat.setName(APPLICATION_CONTEXT_REFRESH_STAGE);
    stat.setEndTime(System.currentTimeMillis());
    // build root ModuleStat
    ModuleStat rootModuleStat = new ModuleStat();
    rootModuleStat.setName(ROOT_MODULE_NAME);
    rootModuleStat.setEndTime(stat.getEndTime());
    rootModuleStat.setThreadName(Thread.currentThread().getName());
    // getBeanStatList from BeanCostBeanPostProcessor
    BeanCostBeanPostProcessor beanCostBeanPostProcessor = applicationContext.getBean(BeanCostBeanPostProcessor.class);
    rootModuleStat.setChildren((beanCostBeanPostProcessor.getBeanStatList()));
    // report ContextRefreshStageStat
    stat.addChild(rootModuleStat);
    startupReporter.addCommonStartupStat(stat);
}
Also used : StartupReporter(com.alipay.sofa.startup.StartupReporter) ModuleStat(com.alipay.sofa.boot.startup.ModuleStat) ChildrenStat(com.alipay.sofa.boot.startup.ChildrenStat) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 7 with ChildrenStat

use of com.alipay.sofa.boot.startup.ChildrenStat in project sofa-boot by sofastack.

the class IsleCheckStartupReporterTest method testStartupReporter.

@Test
public void testStartupReporter() {
    Assert.assertNotNull(startupReporter);
    StartupReporter.StartupStaticsModel startupStaticsModel = startupReporter.report();
    Assert.assertNotNull(startupStaticsModel);
    Assert.assertEquals(7, startupStaticsModel.getStageStats().size());
    BaseStat isleModelCreatingStage = startupReporter.getStageNyName(BootStageConstants.ISLE_MODEL_CREATING_STAGE);
    Assert.assertNotNull(isleModelCreatingStage);
    Assert.assertTrue(isleModelCreatingStage.getCost() > 0);
    BaseStat isleSpringContextInstallStage = startupReporter.getStageNyName(BootStageConstants.ISLE_SPRING_CONTEXT_INSTALL_STAGE);
    Assert.assertNotNull(isleSpringContextInstallStage);
    Assert.assertTrue(isleSpringContextInstallStage.getCost() > 0);
    Assert.assertTrue(isleSpringContextInstallStage instanceof ChildrenStat);
    Assert.assertEquals(1, ((ChildrenStat<?>) isleSpringContextInstallStage).getChildren().size());
    ModuleStat moduleStat = (ModuleStat) ((ChildrenStat<?>) isleSpringContextInstallStage).getChildren().get(0);
    Assert.assertNotNull(moduleStat);
    Assert.assertEquals("testModule", moduleStat.getName());
    Assert.assertTrue(moduleStat.getEndTime() > moduleStat.getStartTime());
    Assert.assertEquals(moduleStat.getCost(), moduleStat.getEndTime() - moduleStat.getStartTime());
    List<BeanStat> beanStats = moduleStat.getChildren();
    Assert.assertNotNull(beanStats);
    Assert.assertTrue(beanStats.size() >= 4);
    // test parent bean
    BeanStat parentBeanStat = beanStats.stream().filter(beanStat -> beanStat.getBeanClassName().contains("(parent)")).findFirst().orElse(null);
    Assert.assertNotNull(parentBeanStat);
    Assert.assertEquals(CHILD_INIT_TIME + PARENT_INIT_TIM, parentBeanStat.getRefreshElapsedTime(), 20);
    Assert.assertEquals(PARENT_INIT_TIM, parentBeanStat.getRealRefreshElapsedTime(), 20);
    Assert.assertEquals(PARENT_INIT_TIM, parentBeanStat.getAfterPropertiesSetTime(), 20);
    Assert.assertEquals(1, parentBeanStat.getChildren().size());
    Assert.assertEquals(ParentBean.class.getName() + " (parent)", parentBeanStat.getBeanClassName());
    // test child bean
    BeanStat childBeanStat = parentBeanStat.getChildren().get(0);
    Assert.assertNotNull(childBeanStat);
    Assert.assertEquals(CHILD_INIT_TIME, childBeanStat.getRealRefreshElapsedTime(), 15);
    Assert.assertEquals(CHILD_INIT_TIME, childBeanStat.getInitTime(), 10);
    Assert.assertEquals(0, childBeanStat.getChildren().size());
    Assert.assertEquals(ChildBean.class.getName() + " (child)", childBeanStat.getBeanClassName());
    // test sofa service
    BeanStat serviceBeanStat = beanStats.stream().filter(beanStat -> beanStat.getBeanClassName().contains("ServiceFactoryBean")).findFirst().orElse(null);
    Assert.assertNotNull(serviceBeanStat);
    Assert.assertTrue(serviceBeanStat.getRefreshElapsedTime() > 0);
    Assert.assertEquals(ServiceFactoryBean.class.getName() + " (sample)", serviceBeanStat.getBeanClassName());
    Assert.assertEquals("com.alipay.sofa.startup.test.beans.facade.SampleService", serviceBeanStat.getInterfaceType());
    // test sofa reference
    BeanStat referenceBeanStat = beanStats.stream().filter(beanStat -> beanStat.getBeanClassName().contains("ReferenceFactoryBean")).findFirst().orElse(null);
    Assert.assertNotNull(referenceBeanStat);
    Assert.assertTrue(referenceBeanStat.getRefreshElapsedTime() > 0);
    Assert.assertEquals(ReferenceFactoryBean.class.getName() + " (reference)", referenceBeanStat.getBeanClassName());
    Assert.assertEquals("com.alipay.sofa.startup.test.beans.facade.TestService", referenceBeanStat.getInterfaceType());
    // test extension bean
    BeanStat extensionBeanStat = beanStats.stream().filter(beanStat -> beanStat.getBeanClassName().contains("ExtensionFactoryBean")).findFirst().orElse(null);
    Assert.assertNotNull(extensionBeanStat);
    Assert.assertTrue(extensionBeanStat.getRefreshElapsedTime() > 0);
    Assert.assertEquals(ExtensionFactoryBean.class.getName(), extensionBeanStat.getBeanClassName());
    Assert.assertEquals("ExtensionPointTarget: extension", extensionBeanStat.getExtensionProperty());
    // test extension point bean
    BeanStat extensionPointBeanStat = beanStats.stream().filter(beanStat -> beanStat.getBeanClassName().contains("ExtensionPointFactoryBean")).findFirst().orElse(null);
    Assert.assertNotNull(extensionPointBeanStat);
    Assert.assertTrue(extensionPointBeanStat.getRefreshElapsedTime() > 0);
    Assert.assertEquals(ExtensionPointFactoryBean.class.getName(), extensionPointBeanStat.getBeanClassName());
    Assert.assertEquals("ExtensionPointTarget: extension", extensionPointBeanStat.getExtensionProperty());
}
Also used : StartupReporter(com.alipay.sofa.startup.StartupReporter) ChildrenStat(com.alipay.sofa.boot.startup.ChildrenStat) ModuleStat(com.alipay.sofa.boot.startup.ModuleStat) BeanStat(com.alipay.sofa.boot.startup.BeanStat) BaseStat(com.alipay.sofa.boot.startup.BaseStat) ExtensionFactoryBean(com.alipay.sofa.runtime.ext.spring.ExtensionFactoryBean) ExtensionPointFactoryBean(com.alipay.sofa.runtime.ext.spring.ExtensionPointFactoryBean) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with ChildrenStat

use of com.alipay.sofa.boot.startup.ChildrenStat in project sofa-boot by sofastack.

the class SimpleStartupReporterTest method testStartupReporter.

@Test
public void testStartupReporter() {
    Assert.assertNotNull(startupReporter);
    StartupReporter.StartupStaticsModel startupStaticsModel = startupReporter.report();
    Assert.assertNotNull(startupStaticsModel);
    Assert.assertEquals(5, startupStaticsModel.getStageStats().size());
    Assert.assertTrue(startupStaticsModel.getApplicationBootTime() > 0);
    Assert.assertTrue(startupStaticsModel.getApplicationBootElapsedTime() > 0);
    Assert.assertEquals("StartupTest", startupStaticsModel.getAppName());
    BaseStat jvmStartingStage = startupReporter.getStageNyName(BootStageConstants.JVM_STARTING_STAGE);
    Assert.assertNotNull(jvmStartingStage);
    Assert.assertTrue(jvmStartingStage.getCost() > 0);
    BaseStat environmentPrepareStage = startupReporter.getStageNyName(BootStageConstants.ENVIRONMENT_PREPARE_STAGE);
    Assert.assertNotNull(environmentPrepareStage);
    Assert.assertTrue(environmentPrepareStage.getCost() > 0);
    Assert.assertEquals(jvmStartingStage.getEndTime(), environmentPrepareStage.getStartTime());
    ChildrenStat<?> applicationContextPrepareStage = (ChildrenStat<?>) startupReporter.getStageNyName(BootStageConstants.APPLICATION_CONTEXT_PREPARE_STAGE);
    Assert.assertNotNull(applicationContextPrepareStage);
    Assert.assertTrue(applicationContextPrepareStage.getChildren().isEmpty());
    Assert.assertTrue(applicationContextPrepareStage.getCost() > 0);
    Assert.assertEquals(environmentPrepareStage.getEndTime(), applicationContextPrepareStage.getStartTime());
    BaseStat applicationContextLoadStage = startupReporter.getStageNyName(BootStageConstants.APPLICATION_CONTEXT_LOAD_STAGE);
    Assert.assertNotNull(applicationContextLoadStage);
    Assert.assertTrue(applicationContextLoadStage.getCost() > 0);
    Assert.assertEquals(applicationContextPrepareStage.getEndTime(), applicationContextLoadStage.getStartTime());
    BaseStat applicationContextRefreshStage = startupReporter.getStageNyName(BootStageConstants.APPLICATION_CONTEXT_REFRESH_STAGE);
    Assert.assertNotNull(applicationContextRefreshStage);
    Assert.assertTrue(applicationContextRefreshStage.getCost() > 0);
    Assert.assertEquals(applicationContextLoadStage.getEndTime(), applicationContextRefreshStage.getStartTime());
    Assert.assertTrue(applicationContextRefreshStage instanceof ChildrenStat);
    Assert.assertEquals(1, ((ChildrenStat<?>) applicationContextRefreshStage).getChildren().size());
    ModuleStat moduleStat = (ModuleStat) ((ChildrenStat<?>) applicationContextRefreshStage).getChildren().get(0);
    Assert.assertNotNull(moduleStat);
    Assert.assertEquals(StartupContextRefreshedListener.ROOT_MODULE_NAME, moduleStat.getName());
    Assert.assertTrue(moduleStat.getEndTime() > moduleStat.getStartTime());
    Assert.assertEquals(moduleStat.getCost(), moduleStat.getEndTime() - moduleStat.getStartTime());
    List<BeanStat> beanStats = moduleStat.getChildren();
    Assert.assertNotNull(beanStats);
    Assert.assertTrue(beanStats.size() >= 1);
    BeanStat initBeanStat = beanStats.stream().filter(beanStat -> beanStat.getBeanClassName().contains("InitCostBean")).findFirst().orElse(null);
    Assert.assertNotNull(initBeanStat);
    Assert.assertEquals(initBeanStat.getRefreshElapsedTime(), initBeanStat.getBeanRefreshEndTime() - initBeanStat.getBeanRefreshStartTime());
    Assert.assertEquals(InitCostBean.INIT_COST_TIME, initBeanStat.getRealRefreshElapsedTime(), 20);
    Assert.assertNull(initBeanStat.getBeanType());
    Assert.assertEquals(InitCostBean.class.getName() + " (initCostBean)", initBeanStat.getBeanClassName());
    Assert.assertTrue(initBeanStat.getChildren().isEmpty());
    Assert.assertEquals(0, initBeanStat.getAfterPropertiesSetTime());
    Assert.assertEquals(0, initBeanStat.getInitTime());
    Assert.assertNull(initBeanStat.getInterfaceType());
    Assert.assertNull(initBeanStat.getExtensionProperty());
}
Also used : StartupReporter(com.alipay.sofa.startup.StartupReporter) ChildrenStat(com.alipay.sofa.boot.startup.ChildrenStat) ModuleStat(com.alipay.sofa.boot.startup.ModuleStat) BeanStat(com.alipay.sofa.boot.startup.BeanStat) BaseStat(com.alipay.sofa.boot.startup.BaseStat) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ChildrenStat (com.alipay.sofa.boot.startup.ChildrenStat)8 ModuleStat (com.alipay.sofa.boot.startup.ModuleStat)8 StartupReporter (com.alipay.sofa.startup.StartupReporter)8 BaseStat (com.alipay.sofa.boot.startup.BaseStat)4 BeanStat (com.alipay.sofa.boot.startup.BeanStat)4 Test (org.junit.Test)4 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 ExtensionFactoryBean (com.alipay.sofa.runtime.ext.spring.ExtensionFactoryBean)2 ExtensionPointFactoryBean (com.alipay.sofa.runtime.ext.spring.ExtensionPointFactoryBean)2