Search in sources :

Example 1 with BeanLoadCostBeanFactory

use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by sofastack.

the class ModuleLogOutputStage method logInfoBeanCost.

private void logInfoBeanCost(StringBuilder stringBuilder, List<DeploymentDescriptor> deploys) {
    long totalTime = 0;
    long realStart = 0;
    long realEnd = 0;
    stringBuilder.append("\n").append("Spring bean load time cost list").append("(").append(deploys.size()).append(") >>>>>>>");
    StringBuilder sb = new StringBuilder();
    int size = deploys.size();
    for (int i = 0; i < size; ++i) {
        String prefix = (i == size - 1) ? SYMBOLIC2 : SYMBOLIC1;
        String indexPrefix = (i == size - 1) ? EMPTY_INDEX_PREFIX : INDENT_PREFIX;
        DeploymentDescriptor dd = deploys.get(i);
        BeanFactory beanFactory = ((ConfigurableApplicationContext) dd.getApplicationContext()).getBeanFactory();
        if (realStart == 0 || dd.getStartTime() < realStart) {
            realStart = dd.getStartTime();
        }
        if (realEnd == 0 || (dd.getStartTime() + dd.getElapsedTime()) > realEnd) {
            realEnd = dd.getStartTime() + dd.getElapsedTime();
        }
        totalTime += dd.getElapsedTime();
        if (beanFactory instanceof BeanLoadCostBeanFactory) {
            sb.append(prefix).append("[Module] ").append(dd.getName()).append(" [").append(dd.getElapsedTime()).append(" ms]\n");
            sb.append(((BeanLoadCostBeanFactory) beanFactory).outputBeanStats(indexPrefix));
        }
    }
    stringBuilder.append(" [totalTime = ").append(totalTime).append(" ms, realTime = ").append(realEnd - realStart).append(" ms]\n").append(sb);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory)

Example 2 with BeanLoadCostBeanFactory

use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by sofastack.

the class AsyncInitBeanDefinitionDecoratorTest method testIsleModule.

@Test
public void testIsleModule() {
    String moduleName = "testModule";
    BeanLoadCostBeanFactory beanFactory = new BeanLoadCostBeanFactory(10, moduleName);
    Assert.assertTrue(AsyncInitBeanDefinitionDecorator.isBeanLoadCostBeanFactory(beanFactory.getClass()));
    Assert.assertEquals(moduleName, AsyncInitBeanDefinitionDecorator.getModuleNameFromBeanFactory(beanFactory));
}
Also used : BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) Test(org.junit.Test)

Example 3 with BeanLoadCostBeanFactory

use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by sofastack.

the class BeanHierarchyTest method test.

@Test
public void test() throws Exception {
    ApplicationRuntimeModel application = new ApplicationRuntimeModel();
    application.setAppName(this.getClass().getName());
    application.setModuleDeploymentValidator(new DefaultModuleDeploymentValidator());
    DeploymentDescriptorConfiguration deploymentDescriptorConfiguration = new DeploymentDescriptorConfiguration(Collections.singletonList(SofaBootConstants.MODULE_NAME), Collections.singletonList(SofaBootConstants.REQUIRE_MODULE));
    Properties props = new Properties();
    props.setProperty(SofaBootConstants.MODULE_NAME, "com.alipay.module");
    File moduleDirectory = new File("target/test-classes/module/sofa-module.properties");
    DeploymentDescriptor dd = DeploymentBuilder.build(moduleDirectory.toURI().toURL(), props, deploymentDescriptorConfiguration, this.getClass().getClassLoader());
    Assert.assertTrue(dd instanceof FileDeploymentDescriptor);
    Assert.assertTrue(application.isModuleDeployment(dd));
    application.addDeployment(dd);
    refreshApplication(application);
    BeanFactory beanFactory = ((ConfigurableApplicationContext) dd.getApplicationContext()).getBeanFactory();
    for (BeanStat bn : ((BeanLoadCostBeanFactory) beanFactory).getBeanStats()) {
        if (bn.getBeanClassName().contains("testService")) {
            Assert.assertEquals(3, bn.getChildren().size());
            for (BeanStat cbn : bn.getChildren()) {
                if (cbn.getChildren().size() > 1) {
                    Assert.assertEquals(3, cbn.getChildren().size());
                }
            }
        }
    }
    ApplicationContext applicationContext = application.getResolvedDeployments().get(0).getApplicationContext();
    String moduleName = applicationContext.getId();
    Assert.assertEquals("com.alipay.module", moduleName);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DeploymentDescriptorConfiguration(com.alipay.sofa.isle.deployment.DeploymentDescriptorConfiguration) FileDeploymentDescriptor(com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties) Properties(java.util.Properties) FileDeploymentDescriptor(com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor) ApplicationRuntimeModel(com.alipay.sofa.isle.ApplicationRuntimeModel) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) BeanStat(com.alipay.sofa.boot.startup.BeanStat) DefaultModuleDeploymentValidator(com.alipay.sofa.isle.deployment.impl.DefaultModuleDeploymentValidator) File(java.io.File) Test(org.junit.Test)

Example 4 with BeanLoadCostBeanFactory

use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by alipay.

the class BeanHierarchyTest method test.

@Test
public void test() throws Exception {
    ApplicationRuntimeModel application = new ApplicationRuntimeModel();
    application.setAppName(this.getClass().getName());
    application.setModuleDeploymentValidator(new DefaultModuleDeploymentValidator());
    DeploymentDescriptorConfiguration deploymentDescriptorConfiguration = new DeploymentDescriptorConfiguration(Collections.singletonList(SofaBootConstants.MODULE_NAME), Collections.singletonList(SofaBootConstants.REQUIRE_MODULE));
    Properties props = new Properties();
    props.setProperty(SofaBootConstants.MODULE_NAME, "com.alipay.module");
    File moduleDirectory = new File("target/test-classes/module/sofa-module.properties");
    DeploymentDescriptor dd = DeploymentBuilder.build(moduleDirectory.toURI().toURL(), props, deploymentDescriptorConfiguration, this.getClass().getClassLoader());
    Assert.assertTrue(dd instanceof FileDeploymentDescriptor);
    Assert.assertTrue(application.isModuleDeployment(dd));
    application.addDeployment(dd);
    refreshApplication(application);
    BeanFactory beanFactory = ((ConfigurableApplicationContext) dd.getApplicationContext()).getBeanFactory();
    for (BeanStat bn : ((BeanLoadCostBeanFactory) beanFactory).getBeanStats()) {
        if (bn.getBeanClassName().contains("testService")) {
            Assert.assertEquals(3, bn.getChildren().size());
            for (BeanStat cbn : bn.getChildren()) {
                if (cbn.getChildren().size() > 1) {
                    Assert.assertEquals(3, cbn.getChildren().size());
                }
            }
        }
    }
    ApplicationContext applicationContext = application.getResolvedDeployments().get(0).getApplicationContext();
    String moduleName = applicationContext.getId();
    Assert.assertEquals("com.alipay.module", moduleName);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DeploymentDescriptorConfiguration(com.alipay.sofa.isle.deployment.DeploymentDescriptorConfiguration) FileDeploymentDescriptor(com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor) DeploymentDescriptor(com.alipay.sofa.isle.deployment.DeploymentDescriptor) SofaModuleProperties(com.alipay.sofa.isle.spring.config.SofaModuleProperties) Properties(java.util.Properties) FileDeploymentDescriptor(com.alipay.sofa.isle.deployment.impl.FileDeploymentDescriptor) ApplicationRuntimeModel(com.alipay.sofa.isle.ApplicationRuntimeModel) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) BeanStat(com.alipay.sofa.boot.startup.BeanStat) DefaultModuleDeploymentValidator(com.alipay.sofa.isle.deployment.impl.DefaultModuleDeploymentValidator) File(java.io.File) Test(org.junit.Test)

Example 5 with BeanLoadCostBeanFactory

use of com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory in project sofa-boot by alipay.

the class StartupSpringContextInstallStage method doRefreshSpringContext.

@Override
protected void doRefreshSpringContext(DeploymentDescriptor deployment, ApplicationRuntimeModel application) {
    ModuleStat moduleStat = new ModuleStat();
    moduleStat.setName(deployment.getModuleName());
    moduleStat.setStartTime(System.currentTimeMillis());
    super.doRefreshSpringContext(deployment, application);
    moduleStat.setEndTime(System.currentTimeMillis());
    moduleStat.setCost(moduleStat.getEndTime() - moduleStat.getStartTime());
    moduleStat.setThreadName(Thread.currentThread().getName());
    ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) deployment.getApplicationContext();
    ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
    if (beanFactory instanceof BeanLoadCostBeanFactory) {
        moduleStat.setChildren(((BeanLoadCostBeanFactory) beanFactory).getBeanStats());
    }
    contextRefreshStageStat.addChild(moduleStat);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ModuleStat(com.alipay.sofa.boot.startup.ModuleStat) BeanLoadCostBeanFactory(com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

BeanLoadCostBeanFactory (com.alipay.sofa.isle.spring.factory.BeanLoadCostBeanFactory)10 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 DeploymentDescriptor (com.alipay.sofa.isle.deployment.DeploymentDescriptor)4 SofaModuleProperties (com.alipay.sofa.isle.spring.config.SofaModuleProperties)4 Test (org.junit.Test)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 BeanStat (com.alipay.sofa.boot.startup.BeanStat)2 ModuleStat (com.alipay.sofa.boot.startup.ModuleStat)2 ApplicationRuntimeModel (com.alipay.sofa.isle.ApplicationRuntimeModel)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 SofaModuleApplicationContext (com.alipay.sofa.isle.spring.context.SofaModuleApplicationContext)2 File (java.io.File)2 Properties (java.util.Properties)2 PropertyEditorRegistrar (org.springframework.beans.PropertyEditorRegistrar)2 PropertyEditorRegistry (org.springframework.beans.PropertyEditorRegistry)2 QualifierAnnotationAutowireCandidateResolver (org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)2