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

@@ -0,0 +1,74 @@
/*
* Copyright 2002-2020 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.web.bind;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
/**
* Benchmarks for extracting parameters from {@libnk ServletRequest}.
* @author Brian Clozel
*/
@BenchmarkMode(Mode.Throughput)
public class ServletRequestUtilsBenchmark {
@State(Scope.Benchmark)
public static class BenchmarkData {
public MockHttpServletRequest request = new MockHttpServletRequest();
public String parameterName = "nonExistingParam";
}
@Benchmark
public int intParameterWithDefaultValue(BenchmarkData data) {
return ServletRequestUtils.getIntParameter(data.request, data.parameterName, 0);
}
@Benchmark
public long longParameterWithDefaultValue(BenchmarkData data) {
return ServletRequestUtils.getLongParameter(data.request, data.parameterName, 0);
}
@Benchmark
public float floatParameterWithDefaultValue(BenchmarkData data) {
return ServletRequestUtils.getFloatParameter(data.request, data.parameterName, 0f);
}
@Benchmark
public double doubleParameterWithDefaultValue(BenchmarkData data) {
return ServletRequestUtils.getDoubleParameter(data.request, data.parameterName, 0d);
}
@Benchmark
public boolean booleanParameterWithDefaultValue(BenchmarkData data) {
return ServletRequestUtils.getBooleanParameter(data.request, data.parameterName, false);
}
@Benchmark
public String stringParameterWithDefaultValue(BenchmarkData data) {
return ServletRequestUtils.getStringParameter(data.request, data.parameterName, "defaultValue");
}
}

View File

@@ -18,13 +18,10 @@ package org.springframework.web.bind;
import org.junit.jupiter.api.Test;
import org.springframework.core.testfixture.EnabledForTestGroups;
import org.springframework.util.StopWatch;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.core.testfixture.TestGroup.PERFORMANCE;
/**
* @author Juergen Hoeller
@@ -270,82 +267,4 @@ class ServletRequestUtilsTests {
assertThat(ServletRequestUtils.getRequiredStringParameter(request, "paramEmpty")).isEmpty();
}
@Test
@EnabledForTestGroups(PERFORMANCE)
void testGetIntParameterWithDefaultValueHandlingIsFastEnough() {
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000; i++) {
ServletRequestUtils.getIntParameter(request, "nonExistingParam", 0);
}
sw.stop();
System.out.println(sw.getTotalTimeMillis());
assertThat(sw.getTotalTimeMillis() < 250).as("getStringParameter took too long: " + sw.getTotalTimeMillis()).isTrue();
}
@Test
@EnabledForTestGroups(PERFORMANCE)
void testGetLongParameterWithDefaultValueHandlingIsFastEnough() {
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000; i++) {
ServletRequestUtils.getLongParameter(request, "nonExistingParam", 0);
}
sw.stop();
System.out.println(sw.getTotalTimeMillis());
assertThat(sw.getTotalTimeMillis() < 250).as("getStringParameter took too long: " + sw.getTotalTimeMillis()).isTrue();
}
@Test
@EnabledForTestGroups(PERFORMANCE)
void testGetFloatParameterWithDefaultValueHandlingIsFastEnough() {
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000; i++) {
ServletRequestUtils.getFloatParameter(request, "nonExistingParam", 0f);
}
sw.stop();
System.out.println(sw.getTotalTimeMillis());
assertThat(sw.getTotalTimeMillis() < 250).as("getStringParameter took too long: " + sw.getTotalTimeMillis()).isTrue();
}
@Test
@EnabledForTestGroups(PERFORMANCE)
void testGetDoubleParameterWithDefaultValueHandlingIsFastEnough() {
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000; i++) {
ServletRequestUtils.getDoubleParameter(request, "nonExistingParam", 0d);
}
sw.stop();
System.out.println(sw.getTotalTimeMillis());
assertThat(sw.getTotalTimeMillis() < 250).as("getStringParameter took too long: " + sw.getTotalTimeMillis()).isTrue();
}
@Test
@EnabledForTestGroups(PERFORMANCE)
void testGetBooleanParameterWithDefaultValueHandlingIsFastEnough() {
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000; i++) {
ServletRequestUtils.getBooleanParameter(request, "nonExistingParam", false);
}
sw.stop();
System.out.println(sw.getTotalTimeMillis());
assertThat(sw.getTotalTimeMillis() < 250).as("getStringParameter took too long: " + sw.getTotalTimeMillis()).isTrue();
}
@Test
@EnabledForTestGroups(PERFORMANCE)
void testGetStringParameterWithDefaultValueHandlingIsFastEnough() {
StopWatch sw = new StopWatch();
sw.start();
for (int i = 0; i < 1000000; i++) {
ServletRequestUtils.getStringParameter(request, "nonExistingParam", "defaultValue");
}
sw.stop();
System.out.println(sw.getTotalTimeMillis());
assertThat(sw.getTotalTimeMillis() < 250).as("getStringParameter took too long: " + sw.getTotalTimeMillis()).isTrue();
}
}