From df67816e5553a27901fde78ca7c3b9696ed458cb Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 9 Jul 2020 12:31:41 +0100 Subject: [PATCH] Try to make ConfigurationPropertySourcesTests perf tests more robust Previously, the tests used absolute values to verify that the work had completed sufficiently quickly. This led to flaky tests in environments where the performance can be variable such as CI. This commit tries to make the tests more robust by comparing the performance to a baseline and requiring it to be twice as fast. Closes gh-22137 --- .../ConfigurationPropertySourcesTests.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java index 158cc428ab6..8ec3d45835b 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java @@ -127,20 +127,24 @@ class ConfigurationPropertySourcesTests { @Test // gh-20625 void environmentPropertyAccessWhenImmutableShouldBePerformant() { - testPropertySourcePerformance(true, 1000); + long baseline = testPropertySourcePerformance(false); + long immutable = testPropertySourcePerformance(true); + assertThat(immutable).isLessThan(baseline / 2); } @Test // gh-20625 void environmentPropertyAccessWhenMutableWithCacheShouldBePerformant() { StandardEnvironment environment = createPerformanceTestEnvironment(false); + long uncached = testPropertySourcePerformance(environment); ConfigurationPropertyCaching.get(environment).enable(); - testPropertySourcePerformance(environment, 1000); + long cached = testPropertySourcePerformance(environment); + assertThat(cached).isLessThan(uncached / 2); } @Test // gh-20625 @Disabled("for manual testing") void environmentPropertyAccessWhenMutableShouldBeTolerable() { - testPropertySourcePerformance(false, 5000); + assertThat(testPropertySourcePerformance(false)).isLessThan(5000); } @Test // gh-21416 @@ -158,9 +162,9 @@ class ConfigurationPropertySourcesTests { assertThat(total).isLessThan(1000); } - private void testPropertySourcePerformance(boolean immutable, int maxTime) { + private long testPropertySourcePerformance(boolean immutable) { StandardEnvironment environment = createPerformanceTestEnvironment(immutable); - testPropertySourcePerformance(environment, maxTime); + return testPropertySourcePerformance(environment); } private StandardEnvironment createPerformanceTestEnvironment(boolean immutable) { @@ -173,14 +177,14 @@ class ConfigurationPropertySourcesTests { return environment; } - private void testPropertySourcePerformance(StandardEnvironment environment, int maxTime) { + private long testPropertySourcePerformance(StandardEnvironment environment) { long start = System.nanoTime(); for (int i = 0; i < 1000; i++) { environment.getProperty("missing" + i); } long total = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); assertThat(environment.getProperty("test-10-property-80")).isEqualTo("test-10-property-80-value"); - assertThat(total).isLessThan(maxTime); + return total; } static class TestPropertySource extends MapPropertySource implements OriginLookup {