Update StringDecoderBenchmark

See gh-25915
This commit is contained in:
Rossen Stoyanchev 2020-10-22 21:50:27 +01:00
parent eec6ec8f44
commit 9a1323aad5
1 changed files with 21 additions and 18 deletions

View File

@ -17,6 +17,7 @@ package org.springframework.core.codec;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -24,6 +25,7 @@ import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level; import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.State;
@ -45,46 +47,42 @@ import org.springframework.util.MimeType;
@BenchmarkMode(Mode.Throughput) @BenchmarkMode(Mode.Throughput)
public class StringDecoderBenchmark { public class StringDecoderBenchmark {
private static final ResolvableType ELEMENT_TYPE = ResolvableType.forClass(String.class);
@Benchmark @Benchmark
public void parseLines(DecodeState state, Blackhole blackhole) { public void parseSseLines(SseLinesState state, Blackhole blackhole) {
Flux<DataBuffer> input = Flux.fromIterable(state.chunks); blackhole.consume(state.parseLines().blockLast());
MimeType mimeType = state.mimeType;
List<String> lines = state.decoder.decode(input, ELEMENT_TYPE, mimeType, Collections.emptyMap())
.collectList()
.block();
blackhole.consume(lines);
} }
@State(Scope.Benchmark) @State(Scope.Benchmark)
@SuppressWarnings({"NotNullFieldNotInitialized", "ConstantConditions"}) @SuppressWarnings({"NotNullFieldNotInitialized", "ConstantConditions"})
public static class DecodeState { public static class SseLinesState {
private static final Charset CHARSET = StandardCharsets.UTF_8; private static final Charset CHARSET = StandardCharsets.UTF_8;
byte[][] delimiterBytes; private static final ResolvableType ELEMENT_TYPE = ResolvableType.forClass(String.class);
@Param("10240")
int totalSize;
@Param("2000")
int chunkSize;
List<DataBuffer> chunks; List<DataBuffer> chunks;
StringDecoder decoder = StringDecoder.textPlainOnly(); StringDecoder decoder = StringDecoder.textPlainOnly(Arrays.asList("\r\n", "\n"), false);
MimeType mimeType = new MimeType("text", "plain", CHARSET); MimeType mimeType = new MimeType("text", "plain", CHARSET);
@Setup(Level.Trial) @Setup(Level.Trial)
public void setup() { public void setup() {
this.delimiterBytes = new byte[][] {"\r\n".getBytes(CHARSET), "\n".getBytes(CHARSET)};
String eventTemplate = "id:$1\n" + String eventTemplate = "id:$1\n" +
"event:some-event\n" + "event:some-event\n" +
":some-comment-$1-aa\n" + ":some-comment-$1-aa\n" +
":some-comment-$1-bb\n" + ":some-comment-$1-bb\n" +
"data:abcdefg-$1-hijklmnop-$1-qrstuvw-$1-xyz-$1\n\n"; "data:abcdefg-$1-hijklmnop-$1-qrstuvw-$1-xyz-$1\n\n";
int totalSize = 10 * 1024;
int chunkSize = 2000;
int eventLength = String.format(eventTemplate, String.format("%05d", 1)).length(); int eventLength = String.format(eventTemplate, String.format("%05d", 1)).length();
int eventCount = totalSize / eventLength; int eventCount = totalSize / eventLength;
DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
@ -102,6 +100,11 @@ public class StringDecoderBenchmark {
.collectList() .collectList()
.block(); .block();
} }
public Flux<String> parseLines() {
Flux<DataBuffer> input = Flux.fromIterable(this.chunks).doOnNext(DataBufferUtils::retain);
return this.decoder.decode(input, ELEMENT_TYPE, this.mimeType, Collections.emptyMap());
}
} }
} }