Search in sources :

Example 1 with BeanStat

use of com.alipay.sofa.boot.startup.BeanStat 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 2 with BeanStat

use of com.alipay.sofa.boot.startup.BeanStat 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 3 with BeanStat

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

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();
    bs.setName(beanName);
    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 4 with BeanStat

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

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 5 with BeanStat

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

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