From 5dd7780a381f0e9553b6599cb6a0b0e0fd5a8c37 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 29 Jul 2020 10:56:30 +0100 Subject: [PATCH] Try to stabilise descendantOfPropertyAccess performance test Closes gh-22633 --- .../ConfigurationPropertySourcesTests.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 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 8ec3d45835..98be1e214f 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 @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -149,17 +150,22 @@ class ConfigurationPropertySourcesTests { @Test // gh-21416 void descendantOfPropertyAccessWhenMutableWithCacheShouldBePerformant() { - StandardEnvironment environment = createPerformanceTestEnvironment(true); - Iterable sources = ConfigurationPropertySources.get(environment); - ConfigurationPropertyName missing = ConfigurationPropertyName.of("missing"); - long start = System.nanoTime(); - for (int i = 0; i < 1000; i++) { - for (ConfigurationPropertySource source : sources) { - assertThat(source.containsDescendantOf(missing)).isEqualTo(ConfigurationPropertyState.ABSENT); + Function descendantOfPerformance = (environment) -> { + Iterable sources = ConfigurationPropertySources.get(environment); + ConfigurationPropertyName missing = ConfigurationPropertyName.of("missing"); + long start = System.nanoTime(); + for (int i = 0; i < 1000; i++) { + for (ConfigurationPropertySource source : sources) { + assertThat(source.containsDescendantOf(missing)).isEqualTo(ConfigurationPropertyState.ABSENT); + } } - } - long total = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); - assertThat(total).isLessThan(1000); + return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); + }; + StandardEnvironment environment = createPerformanceTestEnvironment(false); + long baseline = descendantOfPerformance.apply(environment); + ConfigurationPropertyCaching.get(environment).enable(); + long cached = descendantOfPerformance.apply(environment); + assertThat(cached).isLessThan(baseline / 2); } private long testPropertySourcePerformance(boolean immutable) {