|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.util.Collections;
|
20 | 20 | import java.util.LinkedHashMap;
|
21 | 21 | import java.util.Map;
|
| 22 | +import java.util.concurrent.TimeUnit; |
22 | 23 |
|
| 24 | +import org.junit.jupiter.api.Disabled; |
23 | 25 | import org.junit.jupiter.api.Test;
|
24 | 26 |
|
| 27 | +import org.springframework.boot.origin.Origin; |
| 28 | +import org.springframework.boot.origin.OriginLookup; |
25 | 29 | import org.springframework.core.env.ConfigurableEnvironment;
|
26 | 30 | import org.springframework.core.env.Environment;
|
27 | 31 | import org.springframework.core.env.MapPropertySource;
|
@@ -121,4 +125,71 @@ public String getProperty(String key) {
|
121 | 125 | assertThat(configurationSources.iterator()).toIterable().hasSize(5);
|
122 | 126 | }
|
123 | 127 |
|
| 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 | + |
124 | 195 | }
|
0 commit comments