|
| 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=paranoid"} |
| 26 | + ) |
| 27 | +@Warmup(iterations = 10) |
| 28 | +@Measurement(iterations = 10, time = 10) |
| 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(Blackhole blackhole) { |
| 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 | + blackhole.consume(payload); |
| 53 | + |
| 54 | + return Mono.empty(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Mono<Payload> requestResponse(Payload payload) { |
| 59 | + payload.release(); |
| 60 | + return PAYLOAD_MONO; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Flux<Payload> requestStream(Payload payload) { |
| 65 | + payload.release(); |
| 66 | + return PAYLOAD_FLUX; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Flux<Payload> requestChannel(Publisher<Payload> payloads) { |
| 71 | + return Flux.from(payloads); |
| 72 | + } |
| 73 | + })) |
| 74 | + .transport(LocalServerTransport.create("server")) |
| 75 | + .start() |
| 76 | + .block(); |
| 77 | + |
| 78 | + client = |
| 79 | + RSocketFactory.connect() |
| 80 | + .frameDecoder(PayloadDecoder.ZERO_COPY) |
| 81 | + .transport(LocalClientTransport.create("server")) |
| 82 | + .start() |
| 83 | + .block(); |
| 84 | + } |
| 85 | + |
| 86 | + @Benchmark |
| 87 | + @SuppressWarnings("unchecked") |
| 88 | + public PerfSubscriber fireAndForget(Blackhole blackhole) throws InterruptedException { |
| 89 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 90 | + client.fireAndForget(PAYLOAD).subscribe((CoreSubscriber) subscriber); |
| 91 | + subscriber.latch.await(); |
| 92 | + |
| 93 | + return subscriber; |
| 94 | + } |
| 95 | + |
| 96 | + @Benchmark |
| 97 | + public PerfSubscriber requestResponse(Blackhole blackhole) throws InterruptedException { |
| 98 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 99 | + client.requestResponse(PAYLOAD).subscribe(subscriber); |
| 100 | + subscriber.latch.await(); |
| 101 | + |
| 102 | + return subscriber; |
| 103 | + } |
| 104 | + |
| 105 | + @Benchmark |
| 106 | + public PerfSubscriber requestStreamWithRequestByOneStrategy(Blackhole blackhole) |
| 107 | + throws InterruptedException { |
| 108 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 109 | + client.requestStream(PAYLOAD).subscribe(subscriber); |
| 110 | + subscriber.latch.await(); |
| 111 | + |
| 112 | + return subscriber; |
| 113 | + } |
| 114 | + |
| 115 | + @Benchmark |
| 116 | + public MaxPerfSubscriber requestStreamWithRequestAllStrategy(Blackhole blackhole) |
| 117 | + throws InterruptedException { |
| 118 | + MaxPerfSubscriber subscriber = new MaxPerfSubscriber(blackhole); |
| 119 | + client.requestStream(PAYLOAD).subscribe(subscriber); |
| 120 | + subscriber.latch.await(); |
| 121 | + |
| 122 | + return subscriber; |
| 123 | + } |
| 124 | + |
| 125 | + @Benchmark |
| 126 | + public PerfSubscriber requestChannelWithRequestByOneStrategy(Blackhole blackhole) |
| 127 | + throws InterruptedException { |
| 128 | + PerfSubscriber subscriber = new PerfSubscriber(blackhole); |
| 129 | + client.requestChannel(PAYLOAD_FLUX).subscribe(subscriber); |
| 130 | + subscriber.latch.await(); |
| 131 | + |
| 132 | + return subscriber; |
| 133 | + } |
| 134 | + |
| 135 | + @Benchmark |
| 136 | + public MaxPerfSubscriber requestChannelWithRequestAllStrategy(Blackhole blackhole) |
| 137 | + throws InterruptedException { |
| 138 | + MaxPerfSubscriber subscriber = new MaxPerfSubscriber(blackhole); |
| 139 | + client.requestChannel(PAYLOAD_FLUX).subscribe(subscriber); |
| 140 | + subscriber.latch.await(); |
| 141 | + |
| 142 | + return subscriber; |
| 143 | + } |
| 144 | +} |
0 commit comments