|
| 1 | +package io.rsocket; |
| 2 | + |
| 3 | +import io.rsocket.frame.decoder.PayloadDecoder; |
| 4 | +import io.rsocket.transport.local.LocalClientTransport; |
| 5 | +import io.rsocket.transport.local.LocalServerTransport; |
| 6 | +import io.rsocket.util.EmptyPayload; |
| 7 | +import java.util.stream.IntStream; |
| 8 | +import org.openjdk.jmh.annotations.Benchmark; |
| 9 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 10 | +import org.openjdk.jmh.annotations.Fork; |
| 11 | +import org.openjdk.jmh.annotations.Measurement; |
| 12 | +import org.openjdk.jmh.annotations.Mode; |
| 13 | +import org.openjdk.jmh.annotations.Scope; |
| 14 | +import org.openjdk.jmh.annotations.Setup; |
| 15 | +import org.openjdk.jmh.annotations.State; |
| 16 | +import org.openjdk.jmh.annotations.Warmup; |
| 17 | +import org.openjdk.jmh.infra.Blackhole; |
| 18 | +import org.reactivestreams.Publisher; |
| 19 | +import reactor.core.CoreSubscriber; |
| 20 | +import reactor.core.publisher.Flux; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | + |
| 23 | +@BenchmarkMode(Mode.Throughput) |
| 24 | +@Fork( |
| 25 | + value = 1 // , jvmArgsAppend = {"-Dio.netty.leakDetection.level=advanced"} |
| 26 | + ) |
| 27 | +@Warmup(iterations = 10) |
| 28 | +@Measurement(iterations = 10, time = 20) |
| 29 | +@State(Scope.Benchmark) |
| 30 | +public class RSocketPerf { |
| 31 | + |
| 32 | + static final Payload PAYLOAD = EmptyPayload.INSTANCE; |
| 33 | + static final Mono<Payload> PAYLOAD_MONO = Mono.just(PAYLOAD); |
| 34 | + static final Flux<Payload> PAYLOAD_FLUX = |
| 35 | + Flux.fromArray(IntStream.range(0, 100000).mapToObj(__ -> PAYLOAD).toArray(Payload[]::new)); |
| 36 | + |
| 37 | + RSocket client; |
| 38 | + Closeable server; |
| 39 | + |
| 40 | + @Setup |
| 41 | + public void setUp() { |
| 42 | + server = |
| 43 | + RSocketFactory.receive() |
| 44 | + .acceptor( |
| 45 | + (setup, sendingSocket) -> |
| 46 | + Mono.just( |
| 47 | + new AbstractRSocket() { |
| 48 | + |
| 49 | + @Override |
| 50 | + public Mono<Void> fireAndForget(Payload payload) { |
| 51 | + payload.release(); |
| 52 | + return Mono.empty(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Mono<Payload> requestResponse(Payload payload) { |
| 57 | + payload.release(); |
| 58 | + return PAYLOAD_MONO; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Flux<Payload> requestStream(Payload payload) { |
| 63 | + payload.release(); |
| 64 | + return PAYLOAD_FLUX; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Flux<Payload> requestChannel(Publisher<Payload> payloads) { |
| 69 | + return Flux.from(payloads); |
| 70 | + } |
| 71 | + })) |
| 72 | + .transport(LocalServerTransport.create("server")) |
| 73 | + .start() |
| 74 | + .block(); |
| 75 | + |
| 76 | + client = |
| 77 | + RSocketFactory.connect() |
| 78 | + .frameDecoder(PayloadDecoder.ZERO_COPY) |
| 79 | + .transport(LocalClientTransport.create("server")) |
| 80 | + .start() |
| 81 | + .block(); |
| 82 | + } |
| 83 | + |
| 84 | + @Benchmark |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + public PerfSubscriber fireAndForget(Blackhole blackhole) throws InterruptedException { |
| 87 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 88 | + client.fireAndForget(PAYLOAD).subscribe((CoreSubscriber) subscriber); |
| 89 | + subscriber.latch.await(); |
| 90 | + |
| 91 | + return subscriber; |
| 92 | + } |
| 93 | + |
| 94 | + @Benchmark |
| 95 | + public PerfSubscriber requestResponse(Blackhole blackhole) throws InterruptedException { |
| 96 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 97 | + client.requestResponse(PAYLOAD).subscribe(subscriber); |
| 98 | + subscriber.latch.await(); |
| 99 | + |
| 100 | + return subscriber; |
| 101 | + } |
| 102 | + |
| 103 | + @Benchmark |
| 104 | + public PerfSubscriber requestStreamWithRequestByOneStrategy(Blackhole blackhole) |
| 105 | + throws InterruptedException { |
| 106 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 107 | + client.requestStream(PAYLOAD).subscribe(subscriber); |
| 108 | + subscriber.latch.await(); |
| 109 | + |
| 110 | + return subscriber; |
| 111 | + } |
| 112 | + |
| 113 | + @Benchmark |
| 114 | + public MaxPerfSubscriber requestStreamWithRequestAllStrategy(Blackhole blackhole) |
| 115 | + throws InterruptedException { |
| 116 | + MaxPerfSubscriber subscriber = new MaxPerfSubscriber(blackhole); |
| 117 | + client.requestStream(PAYLOAD).subscribe(subscriber); |
| 118 | + subscriber.latch.await(); |
| 119 | + |
| 120 | + return subscriber; |
| 121 | + } |
| 122 | + |
| 123 | + @Benchmark |
| 124 | + public PerfSubscriber requestChannelWithRequestByOneStrategy(Blackhole blackhole) |
| 125 | + throws InterruptedException { |
| 126 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 127 | + client.requestChannel(PAYLOAD_FLUX).subscribe(subscriber); |
| 128 | + subscriber.latch.await(); |
| 129 | + |
| 130 | + return subscriber; |
| 131 | + } |
| 132 | + |
| 133 | + @Benchmark |
| 134 | + public MaxPerfSubscriber requestChannelWithRequestAllStrategy(Blackhole blackhole) |
| 135 | + throws InterruptedException { |
| 136 | + MaxPerfSubscriber subscriber = new MaxPerfSubscriber(blackhole); |
| 137 | + client.requestChannel(PAYLOAD_FLUX).subscribe(subscriber); |
| 138 | + subscriber.latch.await(); |
| 139 | + |
| 140 | + return subscriber; |
| 141 | + } |
| 142 | +} |
0 commit comments