Rewrite "performance" test to JMH benchmarks

This commit rewrites the remaining "fastEnough" performance tests into
proper JMH benchmarks.

See gh-24830
This commit is contained in:
Brian Clozel
2020-09-25 13:41:10 +02:00
parent e02d3f32b4
commit 61d893257e
22 changed files with 752 additions and 1047 deletions

View File

@@ -42,10 +42,7 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.testfixture.beans.INestedTestBean;
import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.NestedTestBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -53,10 +50,6 @@ import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.NestedRuntimeException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.testfixture.Assume;
import org.springframework.core.testfixture.EnabledForTestGroups;
import org.springframework.core.testfixture.TestGroup;
import org.springframework.lang.Nullable;
import org.springframework.util.StopWatch;
@@ -112,72 +105,6 @@ public class AspectJAutoProxyCreatorTests {
doTestAspectsAndAdvisorAreApplied(ac, shouldBeWeaved);
}
@Test
@EnabledForTestGroups(TestGroup.PERFORMANCE)
public void testAspectsAndAdvisorAppliedToPrototypeIsFastEnough() {
Assume.notLogging(factoryLog);
ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
StopWatch sw = new StopWatch();
sw.start("Prototype Creation");
for (int i = 0; i < 10000; i++) {
ITestBean shouldBeWeaved = (ITestBean) ac.getBean("adrian2");
if (i < 10) {
doTestAspectsAndAdvisorAreApplied(ac, shouldBeWeaved);
}
}
sw.stop();
// What's a reasonable expectation for _any_ server or developer machine load?
// 9 seconds?
assertStopWatchTimeLimit(sw, 9000);
}
@Test
@EnabledForTestGroups(TestGroup.PERFORMANCE)
public void testAspectsAndAdvisorNotAppliedToPrototypeIsFastEnough() {
Assume.notLogging(factoryLog);
ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
StopWatch sw = new StopWatch();
sw.start("Prototype Creation");
for (int i = 0; i < 100000; i++) {
INestedTestBean shouldNotBeWeaved = (INestedTestBean) ac.getBean("i21");
if (i < 10) {
assertThat(AopUtils.isAopProxy(shouldNotBeWeaved)).isFalse();
}
}
sw.stop();
// What's a reasonable expectation for _any_ server or developer machine load?
// 3 seconds?
assertStopWatchTimeLimit(sw, 6000);
}
@Test
@EnabledForTestGroups(TestGroup.PERFORMANCE)
public void testAspectsAndAdvisorNotAppliedToManySingletonsIsFastEnough() {
Assume.notLogging(factoryLog);
GenericApplicationContext ac = new GenericApplicationContext();
new XmlBeanDefinitionReader(ac).loadBeanDefinitions(new ClassPathResource(qName("aspectsPlusAdvisor.xml"),
getClass()));
for (int i = 0; i < 10000; i++) {
ac.registerBeanDefinition("singleton" + i, new RootBeanDefinition(NestedTestBean.class));
}
StopWatch sw = new StopWatch();
sw.start("Singleton Creation");
ac.refresh();
sw.stop();
// What's a reasonable expectation for _any_ server or developer machine load?
// 8 seconds?
assertStopWatchTimeLimit(sw, 8000);
}
@Test
public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() {
ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");

View File

@@ -69,12 +69,9 @@ import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.Person;
import org.springframework.beans.testfixture.beans.SerializablePerson;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.testfixture.EnabledForTestGroups;
import org.springframework.core.testfixture.TestGroup;
import org.springframework.core.testfixture.TimeStamped;
import org.springframework.core.testfixture.io.SerializationTestUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.StopWatch;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -157,36 +154,6 @@ public abstract class AbstractAopProxyTests {
assertThat(tb.getName()).isEqualTo(name);
}
/**
* This is primarily a test for the efficiency of our
* usage of CGLIB. If we create too many classes with
* CGLIB this will be slow or will run out of memory.
*/
@Test
@EnabledForTestGroups(TestGroup.PERFORMANCE)
public void testManyProxies() {
int howMany = 10000;
StopWatch sw = new StopWatch();
sw.start("Create " + howMany + " proxies");
testManyProxies(howMany);
sw.stop();
assertThat(sw.getTotalTimeMillis() < 5000).as("Proxy creation was too slow").isTrue();
}
private void testManyProxies(int howMany) {
int age1 = 33;
TestBean target1 = new TestBean();
target1.setAge(age1);
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvice(new NopInterceptor());
pf1.addAdvice(new NopInterceptor());
ITestBean[] proxies = new ITestBean[howMany];
for (int i = 0; i < howMany; i++) {
proxies[i] = (ITestBean) createAopProxy(pf1).getProxy();
assertThat(proxies[i].getAge()).isEqualTo(age1);
}
}
@Test
public void testSerializationAdviceAndTargetNotSerializable() throws Exception {
TestBean tb = new TestBean();

View File

@@ -1,161 +0,0 @@
/*
* Copyright 2002-2019 the original author or 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
*
* https://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 org.springframework.context.annotation;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.testfixture.Assume;
import org.springframework.core.testfixture.EnabledForTestGroups;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.core.testfixture.TestGroup.PERFORMANCE;
/**
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 2.5
*/
@EnabledForTestGroups(PERFORMANCE)
public class AnnotationProcessorPerformanceTests {
private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
@BeforeAll
public static void commonAssumptions() {
Assume.notLogging(factoryLog);
}
@Test
public void prototypeCreationWithResourcePropertiesIsFastEnough() {
GenericApplicationContext ctx = createContext();
RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
ctx.registerBeanDefinition("test", rbd);
ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
assertFastEnough(ctx);
}
@Test
public void prototypeCreationWithOverriddenResourcePropertiesIsFastEnough() {
GenericApplicationContext ctx = createContext();
RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
ctx.registerBeanDefinition("test", rbd);
ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
assertFastEnough(ctx);
}
@Test
public void prototypeCreationWithAutowiredPropertiesIsFastEnough() {
GenericApplicationContext ctx = createContext();
RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
ctx.registerBeanDefinition("test", rbd);
ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
assertFastEnough(ctx);
}
@Test
public void prototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough() {
GenericApplicationContext ctx = createContext();
RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
ctx.registerBeanDefinition("test", rbd);
ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
assertFastEnough(ctx);
}
private GenericApplicationContext createContext() {
GenericApplicationContext ctx = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
ctx.refresh();
return ctx;
}
private void assertFastEnough(GenericApplicationContext ctx) {
AtomicBoolean done = new AtomicBoolean();
TestBean spouse = ctx.getBean("spouse", TestBean.class);
Executors.newSingleThreadExecutor().submit(() -> {
for (int i = 0; i < 100_000; i++) {
TestBean tb = ctx.getBean("test", TestBean.class);
assertThat(tb.getSpouse()).isSameAs(spouse);
}
done.set(true);
});
// "fast enough" is of course relative, but we're using 6 seconds with the hope
// that these tests typically pass on the CI server.
Awaitility.await()
.atMost(6, TimeUnit.SECONDS)
.pollInterval(100, TimeUnit.MILLISECONDS)
.untilTrue(done);
}
private static class ResourceAnnotatedTestBean extends TestBean {
@Override
@Resource
@SuppressWarnings("deprecation")
@org.springframework.beans.factory.annotation.Required
public void setSpouse(ITestBean spouse) {
super.setSpouse(spouse);
}
}
private static class AutowiredAnnotatedTestBean extends TestBean {
@Override
@Autowired
@SuppressWarnings("deprecation")
@org.springframework.beans.factory.annotation.Required
public void setSpouse(ITestBean spouse) {
super.setSpouse(spouse);
}
}
}

View File

@@ -52,14 +52,10 @@ import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.testfixture.Assume;
import org.springframework.core.testfixture.EnabledForTestGroups;
import org.springframework.core.testfixture.io.SerializationTestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StopWatch;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.core.testfixture.TestGroup.PERFORMANCE;
/**
* @author Juergen Hoeller
@@ -247,37 +243,6 @@ class ApplicationContextExpressionTests {
}
}
@Test
@EnabledForTestGroups(PERFORMANCE)
void prototypeCreationIsFastEnough() {
Assume.notLogging(factoryLog);
GenericApplicationContext ac = new GenericApplicationContext();
RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
rbd.getConstructorArgumentValues().addGenericArgumentValue("#{systemProperties.name}");
rbd.getPropertyValues().add("country", "#{systemProperties.country}");
ac.registerBeanDefinition("test", rbd);
ac.refresh();
StopWatch sw = new StopWatch();
sw.start("prototype");
System.getProperties().put("name", "juergen");
System.getProperties().put("country", "UK");
try {
for (int i = 0; i < 100000; i++) {
TestBean tb = (TestBean) ac.getBean("test");
assertThat(tb.getName()).isEqualTo("juergen");
assertThat(tb.getCountry()).isEqualTo("UK");
}
sw.stop();
}
finally {
System.getProperties().remove("country");
System.getProperties().remove("name");
}
assertThat(sw.getTotalTimeMillis() < 6000).as("Prototype creation took too long: " + sw.getTotalTimeMillis()).isTrue();
ac.close();
}
@Test
void systemPropertiesSecurityManager() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();