Skip to content

Commit 5309912

Browse files
committed
Test large property source performance
Add a test to ensure that a large number of property sources that each contain many items can perform well. See gh-20625
1 parent 74327e1 commit 5309912

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourcesTests.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,9 +19,13 @@
1919
import java.util.Collections;
2020
import java.util.LinkedHashMap;
2121
import java.util.Map;
22+
import java.util.concurrent.TimeUnit;
2223

24+
import org.junit.jupiter.api.Disabled;
2325
import org.junit.jupiter.api.Test;
2426

27+
import org.springframework.boot.origin.Origin;
28+
import org.springframework.boot.origin.OriginLookup;
2529
import org.springframework.core.env.ConfigurableEnvironment;
2630
import org.springframework.core.env.Environment;
2731
import org.springframework.core.env.MapPropertySource;
@@ -121,4 +125,71 @@ public String getProperty(String key) {
121125
assertThat(configurationSources.iterator()).toIterable().hasSize(5);
122126
}
123127

128+
@Test // gh-20625
129+
void environmentPropertyAccessWhenImmutableShouldBePerformant() {
130+
testPropertySourcePerformance(true, 1000);
131+
}
132+
133+
@Test // gh-20625
134+
@Disabled("for manual testing")
135+
void environmentPropertyAccessWhenMutableShouldBeTolerable() {
136+
testPropertySourcePerformance(false, 5000);
137+
}
138+
139+
private void testPropertySourcePerformance(boolean immutable, int maxTime) {
140+
StandardEnvironment environment = createPerformanceTestEnvironment(immutable);
141+
testPropertySourcePerformance(environment, maxTime);
142+
}
143+
144+
private StandardEnvironment createPerformanceTestEnvironment(boolean immutable) {
145+
StandardEnvironment environment = new StandardEnvironment();
146+
MutablePropertySources propertySources = environment.getPropertySources();
147+
for (int i = 0; i < 100; i++) {
148+
propertySources.addLast(new TestPropertySource(i, immutable));
149+
}
150+
ConfigurationPropertySources.attach(environment);
151+
return environment;
152+
}
153+
154+
private void testPropertySourcePerformance(StandardEnvironment environment, int maxTime) {
155+
long start = System.nanoTime();
156+
for (int i = 0; i < 1000; i++) {
157+
environment.getProperty("missing" + i);
158+
}
159+
long total = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
160+
assertThat(environment.getProperty("test-10-property-80")).isEqualTo("test-10-property-80-value");
161+
assertThat(total).isLessThan(maxTime);
162+
}
163+
164+
static class TestPropertySource extends MapPropertySource implements OriginLookup<String> {
165+
166+
private final boolean immutable;
167+
168+
TestPropertySource(int index, boolean immutable) {
169+
super("test-" + index, createProperties(index));
170+
this.immutable = immutable;
171+
}
172+
173+
private static Map<String, Object> createProperties(int index) {
174+
Map<String, Object> map = new LinkedHashMap<String, Object>();
175+
for (int i = 0; i < 1000; i++) {
176+
String name = "test-" + index + "-property-" + i;
177+
String value = name + "-value";
178+
map.put(name, value);
179+
}
180+
return map;
181+
}
182+
183+
@Override
184+
public Origin getOrigin(String key) {
185+
return null;
186+
}
187+
188+
@Override
189+
public boolean isImmutable() {
190+
return this.immutable;
191+
}
192+
193+
}
194+
124195
}

0 commit comments

Comments
 (0)