spring

貢獻者:墨弦 類別:代码 時間:2021-08-24 23:15:09 收藏數:11 評分:0
返回上页 舉報此文章
请选择举报理由:




收藏到我的文章 改錯字
package org.springframework.core.codec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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 reactor.core.publisher.Flux;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.util.MimeType;
/**
* Benchmarks for {@link DataBufferUtils}.
*
* @author Rossen Stoyanchev
*/
@BenchmarkMode(Mode.Throughput)
public class StringDecoderBenchmark {
@Benchmark
public void parseSseLines(SseLinesState state, Blackhole blackhole) {
blackhole.consume(state.parseLines().blockLast());
}
@State(Scope.Benchmark)
@SuppressWarnings({"NotNullFieldNotInitialized", "ConstantConditions"})
public static class SseLinesState {
private static final Charset CHARSET = StandardCharsets.UTF_8;
private static final ResolvableType ELEMENT_TYPE = ResolvableType.forClass(String.class);
@Param("10240")
int totalSize;
@Param("2000")
int chunkSize;
List<DataBuffer> chunks;
StringDecoder decoder = StringDecoder.textPlainOnly(Arrays.asList("\r\n", "\n"), false);
MimeType mimeType = new MimeType("text", "plain", CHARSET);
@Setup(Level.Trial)
public void setup() {
String eventTemplate = "id:$1\n" +
"event:some-event\n" +
":some-comment-$1-aa\n" +
":some-comment-$1-bb\n" +
"data:abcdefg-$1-hijklmnop-$1-qrstuvw-$1-xyz-$1\n\n";
int eventLength = String.format(eventTemplate, String.format("%05d", 1)).length();
int eventCount = this.totalSize / eventLength;
DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
this.chunks = Flux.range(1, eventCount)
.map(index -> String.format(eventTemplate, String.format("%05d", index)))
.buffer(this.chunkSize > eventLength ? this.chunkSize / eventLength : 1)
.map(strings -> String.join("", strings))
.map(chunk -> {
byte[] bytes = chunk.getBytes(CHARSET);
DataBuffer buffer = bufferFactory.allocateBuffer(bytes.length);
buffer.write(bytes);
return buffer;
})
.collectList()
.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());
}
}
}
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章熱度:
文章難度:
文章質量:
說明:系統根據文章的熱度、難度、質量自動認證,已認證的文章將參與打字排名!

本文打字排名TOP20

登录后可见

用户更多文章推荐