From 212bb7fef65a21fc367f5ee341d5f9f52c112d46 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 15 May 2020 22:46:05 +0200 Subject: [PATCH] Add GenericConversionService JMH benchmark This commit removes some of the deprecated "PERFORMANCE" tests and turns them into JMH benchmarks. See gh-24830 --- .../GenericConversionServiceBenchmark.java | 112 ++++++++++++++++++ .../GenericConversionServiceTests.java | 56 --------- 2 files changed, 112 insertions(+), 56 deletions(-) create mode 100644 spring-core/src/jmh/java/org/springframework/core/convert/support/GenericConversionServiceBenchmark.java diff --git a/spring-core/src/jmh/java/org/springframework/core/convert/support/GenericConversionServiceBenchmark.java b/spring-core/src/jmh/java/org/springframework/core/convert/support/GenericConversionServiceBenchmark.java new file mode 100644 index 00000000000..8cd4ba48fcc --- /dev/null +++ b/spring-core/src/jmh/java/org/springframework/core/convert/support/GenericConversionServiceBenchmark.java @@ -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 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 source; + + @Setup(Level.Trial) + public void setup() throws Exception { + this.source = IntStream.rangeClosed(1, collectionSize).mapToObj(String::valueOf).collect(Collectors.toList()); + List 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 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 source; + + @Setup(Level.Trial) + public void setup() throws Exception { + this.source = new HashMap<>(this.collectionSize); + Map 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; + + } +} diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 9e0c6464827..01c40274fd7 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -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 -> list conversionPerformance"); - watch.start("convert 4,000,000 with conversion service"); - List 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 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 -> map conversionPerformance"); - watch.start("convert 4,000,000 with conversion service"); - Map 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 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 list; - - public Map map; - public Map wildcardMap; @SuppressWarnings("rawtypes")