Add GenericConversionService JMH benchmark
This commit removes some of the deprecated "PERFORMANCE" tests and turns them into JMH benchmarks. See gh-24830
This commit is contained in:
parent
703d54677e
commit
212bb7fef6
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* 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.core.convert.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Param;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
||||
/**
|
||||
* Benchmarks for {@link GenericConversionService}.
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
public class GenericConversionServiceBenchmark {
|
||||
|
||||
@Benchmark
|
||||
public void convertListOfStringToListOfIntegerWithConversionService(ListBenchmarkState state, Blackhole bh) {
|
||||
TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(state.source);
|
||||
bh.consume(state.conversionService.convert(state.source, sourceTypeDesc, state.targetTypeDesc));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void convertListOfStringToListOfIntegerBaseline(ListBenchmarkState state, Blackhole bh) {
|
||||
List<Integer> target = new ArrayList<>(state.source.size());
|
||||
for (String element : state.source) {
|
||||
target.add(Integer.valueOf(element));
|
||||
}
|
||||
bh.consume(target);
|
||||
}
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class ListBenchmarkState extends BenchmarkState {
|
||||
|
||||
List<String> source;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setup() throws Exception {
|
||||
this.source = IntStream.rangeClosed(1, collectionSize).mapToObj(String::valueOf).collect(Collectors.toList());
|
||||
List<Integer> target = new ArrayList<>();
|
||||
this.targetTypeDesc = TypeDescriptor.forObject(target);
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void convertMapOfStringToListOfIntegerWithConversionService(MapBenchmarkState state, Blackhole bh) {
|
||||
TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(state.source);
|
||||
bh.consume(state.conversionService.convert(state.source, sourceTypeDesc, state.targetTypeDesc));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void convertMapOfStringToListOfIntegerBaseline(MapBenchmarkState state, Blackhole bh) {
|
||||
Map<String, Integer> target = new HashMap<>(state.source.size());
|
||||
state.source.forEach((k, v) -> target.put(k, Integer.valueOf(v)));
|
||||
bh.consume(target);
|
||||
}
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class MapBenchmarkState extends BenchmarkState {
|
||||
|
||||
Map<String, String> source;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setup() throws Exception {
|
||||
this.source = new HashMap<>(this.collectionSize);
|
||||
Map<String, Integer> target = new HashMap<>();
|
||||
this.targetTypeDesc = TypeDescriptor.forObject(target);
|
||||
this.source = IntStream.rangeClosed(1, collectionSize).mapToObj(String::valueOf)
|
||||
.collect(Collectors.toMap(String::valueOf, String::valueOf));
|
||||
}
|
||||
}
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public static class BenchmarkState {
|
||||
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
|
||||
@Param({"10"})
|
||||
int collectionSize;
|
||||
|
||||
TypeDescriptor targetTypeDesc;
|
||||
|
||||
}
|
||||
}
|
|
@ -27,7 +27,6 @@ import java.util.Collections;
|
|||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
@ -43,9 +42,7 @@ import org.springframework.core.convert.converter.ConverterFactory;
|
|||
import org.springframework.core.convert.converter.GenericConverter;
|
||||
import org.springframework.core.io.DescriptiveResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.testfixture.EnabledForTestGroups;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static java.util.Comparator.naturalOrder;
|
||||
|
@ -54,7 +51,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.springframework.core.testfixture.TestGroup.PERFORMANCE;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link GenericConversionService}.
|
||||
|
@ -342,54 +338,6 @@ class GenericConversionServiceTests {
|
|||
assertThat(result).isSameAs(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForTestGroups(PERFORMANCE)
|
||||
void testPerformance2() throws Exception {
|
||||
StopWatch watch = new StopWatch("list<string> -> list<integer> conversionPerformance");
|
||||
watch.start("convert 4,000,000 with conversion service");
|
||||
List<String> source = new LinkedList<>();
|
||||
source.add("1");
|
||||
source.add("2");
|
||||
source.add("3");
|
||||
TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
conversionService.convert(source, TypeDescriptor.forObject(source), td);
|
||||
}
|
||||
watch.stop();
|
||||
watch.start("convert 4,000,000 manually");
|
||||
for (int i = 0; i < 4000000; i++) {
|
||||
List<Integer> target = new ArrayList<>(source.size());
|
||||
for (String element : source) {
|
||||
target.add(Integer.valueOf(element));
|
||||
}
|
||||
}
|
||||
watch.stop();
|
||||
// System.out.println(watch.prettyPrint());
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForTestGroups(PERFORMANCE)
|
||||
void testPerformance3() throws Exception {
|
||||
StopWatch watch = new StopWatch("map<string, string> -> map<string, integer> conversionPerformance");
|
||||
watch.start("convert 4,000,000 with conversion service");
|
||||
Map<String, String> source = new HashMap<>();
|
||||
source.put("1", "1");
|
||||
source.put("2", "2");
|
||||
source.put("3", "3");
|
||||
TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
conversionService.convert(source, TypeDescriptor.forObject(source), td);
|
||||
}
|
||||
watch.stop();
|
||||
watch.start("convert 4,000,000 manually");
|
||||
for (int i = 0; i < 4000000; i++) {
|
||||
Map<String, Integer> target = new HashMap<>(source.size());
|
||||
source.forEach((k, v) -> target.put(k, Integer.valueOf(v)));
|
||||
}
|
||||
watch.stop();
|
||||
// System.out.println(watch.prettyPrint());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyListToArray() {
|
||||
conversionService.addConverter(new CollectionToArrayConverter(conversionService));
|
||||
|
@ -638,10 +586,6 @@ class GenericConversionServiceTests {
|
|||
@ExampleAnnotation(active = false)
|
||||
public Color inactiveColor;
|
||||
|
||||
public List<Integer> list;
|
||||
|
||||
public Map<String, Integer> map;
|
||||
|
||||
public Map<String, ?> wildcardMap;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
|
|
Loading…
Reference in New Issue