Search in sources :

Example 6 with BeanStat

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

the class BeanCostBeanPostProcessor method postProcessBeforeInitialization.

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (!skipSofaBean || isNotSofaBean(bean)) {
        BeanStat beanStat = new BeanStat();
        beanStat.setName(beanName);
        String beanClassName = getBeanName(bean, beanName);
        beanStat.setBeanClassName(beanClassName);
        beanStat.startRefresh();
        beanInitCostMap.put(beanClassName, beanStat);
    }
    return bean;
}
Also used : BeanStat(com.alipay.sofa.boot.startup.BeanStat)

Example 7 with BeanStat

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

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)

Example 8 with BeanStat

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

the class BeanLoadCostBeanFactory method createBean.

@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException {
    Stack<BeanStat> parentStack = parentStackThreadLocal.get();
    BeanStat bs = new BeanStat();
    if (parentStack == null) {
        parentStack = new Stack<>();
        parentStackThreadLocal.set(parentStack);
    }
    if (!parentStack.empty()) {
        parentStack.peek().addChild(bs);
    }
    parentStack.push(bs);
    bs.startRefresh();
    Object object = super.createBean(beanName, mbd, args);
    bs.finishRefresh();
    if (mbd.getBeanClassName() == null) {
        bs.setBeanClassName("Factory (" + mbd.getFactoryBeanName() + ")");
    } else {
        if (mbd.getBeanClassName().contains("ExtensionPointFactoryBean") || mbd.getBeanClassName().contains("ExtensionFactoryBean")) {
            bs.setExtensionProperty(object.toString());
        }
        if (object instanceof ServiceFactoryBean) {
            bs.setBeanClassName(mbd.getBeanClassName() + " (" + ((ServiceFactoryBean) object).getBeanId() + ")");
            bs.setInterfaceType(((ServiceFactoryBean) object).getInterfaceType());
        } else if (object instanceof ReferenceFactoryBean) {
            bs.setBeanClassName(mbd.getBeanClassName() + " (" + beanName + ")");
            bs.setInterfaceType(((ReferenceFactoryBean) object).getInterfaceType());
        } else {
            bs.setBeanClassName(mbd.getBeanClassName() + " (" + beanName + ")");
            if (beanName.contains(mbd.getBeanClassName())) {
                bs.setBeanClassName(mbd.getBeanClassName());
            }
        }
    }
    if (beanStatExtension != null) {
        beanStatExtension.customBeanStat(beanName, mbd, args, bs);
    }
    parentStack.pop();
    if (parentStack.empty() && bs.getRefreshElapsedTime() > beanLoadCost) {
        beanStats.add(bs);
    }
    return object;
}
Also used : ServiceFactoryBean(com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean) ReferenceFactoryBean(com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean) BeanStat(com.alipay.sofa.boot.startup.BeanStat)

Example 9 with BeanStat

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

the class BeanCostBeanPostProcessor method postProcessBeforeInitialization.

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (!skipSofaBean || isNotSofaBean(bean)) {
        BeanStat beanStat = new BeanStat();
        String beanClassName = getBeanName(bean, beanName);
        beanStat.setBeanClassName(beanClassName);
        beanStat.startRefresh();
        beanInitCostMap.put(beanClassName, beanStat);
    }
    return bean;
}
Also used : BeanStat(com.alipay.sofa.boot.startup.BeanStat)

Example 10 with BeanStat

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

the class BeanCostBeanPostProcessor method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (!skipSofaBean || isNotSofaBean(bean)) {
        String beanClassName = getBeanName(bean, beanName);
        BeanStat beanStat = beanInitCostMap.remove(beanClassName);
        if (beanStat != null) {
            beanStat.finishRefresh();
            if (beanStat.getRefreshElapsedTime() > beanLoadCost) {
                beanStat.finishRefresh();
                beanStatList.add(beanStat);
            }
        }
    }
    return bean;
}
Also used : BeanStat(com.alipay.sofa.boot.startup.BeanStat)

Aggregations

BeanStat (com.alipay.sofa.boot.startup.BeanStat)12 Test (org.junit.Test)6 BaseStat (com.alipay.sofa.boot.startup.BaseStat)4 ChildrenStat (com.alipay.sofa.boot.startup.ChildrenStat)4 ModuleStat (com.alipay.sofa.boot.startup.ModuleStat)4 StartupReporter (com.alipay.sofa.startup.StartupReporter)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 ApplicationRuntimeModel (com.alipay.sofa.isle.ApplicationRuntimeModel)2 DeploymentDescriptor (com.alipay.sofa.isle.deployment.DeploymentDescriptor)2 DeploymentDescriptorConfiguration (com.alipay.sofa.isle.deployment.DeploymentDescriptorConfiguration)2 DefaultModuleDeploymentValidator (com.alipay.sofa.isle.deployment.impl.DefaultModuleDeploymentValidator)2 FileDeploymentDescriptor (com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor)2 SofaModuleProperties (com.alipay.sofa.isle.spring.config.SofaModuleProperties)2 BeanLoadCostBeanFactory (com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory)2 ExtensionFactoryBean (com.alipay.sofa.runtime.ext.spring.ExtensionFactoryBean)2 ExtensionPointFactoryBean (com.alipay.sofa.runtime.ext.spring.ExtensionPointFactoryBean)2 ReferenceFactoryBean (com.alipay.sofa.runtime.spring.factory.ReferenceFactoryBean)2 ServiceFactoryBean (com.alipay.sofa.runtime.spring.factory.ServiceFactoryBean)2 File (java.io.File)2 Properties (java.util.Properties)2