|
3 | 3 | import io.rsocket.fragmentation.FrameFragmenter; |
4 | 4 | import io.rsocket.fragmentation.FrameReassembler; |
5 | 5 | import io.rsocket.util.PayloadImpl; |
| 6 | +import java.nio.ByteBuffer; |
| 7 | +import java.util.concurrent.ThreadLocalRandom; |
| 8 | +import java.util.stream.Collectors; |
6 | 9 | import org.openjdk.jmh.annotations.Benchmark; |
7 | 10 | import org.openjdk.jmh.annotations.BenchmarkMode; |
8 | 11 | import org.openjdk.jmh.annotations.Fork; |
|
14 | 17 | import org.openjdk.jmh.annotations.Warmup; |
15 | 18 | import org.openjdk.jmh.infra.Blackhole; |
16 | 19 |
|
17 | | -import java.nio.ByteBuffer; |
18 | | -import java.util.concurrent.ThreadLocalRandom; |
19 | | -import java.util.stream.Collectors; |
20 | | - |
21 | 20 | @BenchmarkMode(Mode.Throughput) |
22 | | -@Fork(value = 1, jvmArgsAppend = {"-XX:+UnlockCommercialFeatures", "-XX:+FlightRecorder"})//, "-Dio.netty.leakDetection.level=advanced"}) |
| 21 | +@Fork( |
| 22 | + value = 1, |
| 23 | + jvmArgsAppend = {"-XX:+UnlockCommercialFeatures", "-XX:+FlightRecorder"} |
| 24 | +) //, "-Dio.netty.leakDetection.level=advanced"}) |
23 | 25 | @Warmup(iterations = 10) |
24 | 26 | @Measurement(iterations = 10_000) |
25 | 27 | @State(Scope.Thread) |
26 | 28 | public class FragmentationPerf { |
27 | | - @State(Scope.Benchmark) |
28 | | - public static class Input { |
29 | | - Blackhole bh; |
30 | | - Frame smallFrame; |
31 | | - FrameFragmenter smallFrameFragmenter; |
32 | | - |
33 | | - Frame largeFrame; |
34 | | - FrameFragmenter largeFrameFragmenter; |
35 | | - |
36 | | - Iterable<Frame> smallFramesIterable; |
37 | | - |
38 | | - @Setup |
39 | | - public void setup(Blackhole bh) { |
40 | | - this.bh = bh; |
41 | | - |
42 | | - ByteBuffer data = createRandomBytes(1 << 18); |
43 | | - ByteBuffer metadata = createRandomBytes(1 << 18); |
44 | | - largeFrame = Frame.Request.from(1, FrameType.REQUEST_RESPONSE, new PayloadImpl(data, metadata), 1); |
45 | | - largeFrameFragmenter = new FrameFragmenter(1024); |
46 | | - |
47 | | - |
48 | | - data = createRandomBytes(16); |
49 | | - metadata = createRandomBytes(16); |
50 | | - smallFrame = Frame.Request.from(1, FrameType.REQUEST_RESPONSE, new PayloadImpl(data, metadata), 1); |
51 | | - smallFrameFragmenter = new FrameFragmenter(2); |
52 | | - smallFramesIterable = smallFrameFragmenter |
53 | | - .fragment(smallFrame) |
54 | | - .map(frame -> frame.copy()) |
55 | | - .toStream() |
56 | | - .collect(Collectors.toList()); |
57 | | - } |
58 | | - |
59 | | - } |
60 | | - |
61 | | - @Benchmark |
62 | | - public void smallFragmentationPerf(Input input) { |
63 | | - Frame frame = input.smallFrameFragmenter.fragment(input.smallFrame).doOnNext(Frame::release).blockLast(); |
64 | | - input.bh.consume(frame); |
65 | | - } |
66 | | - |
67 | | - |
68 | | - @Benchmark |
69 | | - public void largeFragmentationPerf(Input input) { |
70 | | - Frame frame = input.largeFrameFragmenter.fragment(input.largeFrame).doOnNext(Frame::release).blockLast(); |
71 | | - input.bh.consume(frame); |
72 | | - } |
73 | | - |
74 | | - @Benchmark |
75 | | - public void smallFragmentationFrameReassembler(Input input) { |
76 | | - FrameReassembler smallFragmentAssembler = new FrameReassembler(input.smallFrame); |
77 | | - |
78 | | - input |
79 | | - .smallFramesIterable |
80 | | - .forEach(smallFragmentAssembler::append); |
81 | | - |
82 | | - Frame frame = smallFragmentAssembler.reassemble(); |
83 | | - input.bh.consume(frame); |
84 | | - frame.release(); |
85 | | - //input.smallFragmentAssembler.clear(); |
86 | | - } |
87 | | - |
88 | | - private static ByteBuffer createRandomBytes(int size) { |
89 | | - byte[] bytes = new byte[size]; |
90 | | - ThreadLocalRandom.current().nextBytes(bytes); |
91 | | - return ByteBuffer.wrap(bytes); |
| 29 | + @State(Scope.Benchmark) |
| 30 | + public static class Input { |
| 31 | + Blackhole bh; |
| 32 | + Frame smallFrame; |
| 33 | + FrameFragmenter smallFrameFragmenter; |
| 34 | + |
| 35 | + Frame largeFrame; |
| 36 | + FrameFragmenter largeFrameFragmenter; |
| 37 | + |
| 38 | + Iterable<Frame> smallFramesIterable; |
| 39 | + |
| 40 | + @Setup |
| 41 | + public void setup(Blackhole bh) { |
| 42 | + this.bh = bh; |
| 43 | + |
| 44 | + ByteBuffer data = createRandomBytes(1 << 18); |
| 45 | + ByteBuffer metadata = createRandomBytes(1 << 18); |
| 46 | + largeFrame = |
| 47 | + Frame.Request.from(1, FrameType.REQUEST_RESPONSE, new PayloadImpl(data, metadata), 1); |
| 48 | + largeFrameFragmenter = new FrameFragmenter(1024); |
| 49 | + |
| 50 | + data = createRandomBytes(16); |
| 51 | + metadata = createRandomBytes(16); |
| 52 | + smallFrame = |
| 53 | + Frame.Request.from(1, FrameType.REQUEST_RESPONSE, new PayloadImpl(data, metadata), 1); |
| 54 | + smallFrameFragmenter = new FrameFragmenter(2); |
| 55 | + smallFramesIterable = |
| 56 | + smallFrameFragmenter |
| 57 | + .fragment(smallFrame) |
| 58 | + .map(frame -> frame.copy()) |
| 59 | + .toStream() |
| 60 | + .collect(Collectors.toList()); |
92 | 61 | } |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public void smallFragmentationPerf(Input input) { |
| 66 | + Frame frame = |
| 67 | + input.smallFrameFragmenter.fragment(input.smallFrame).doOnNext(Frame::release).blockLast(); |
| 68 | + input.bh.consume(frame); |
| 69 | + } |
| 70 | + |
| 71 | + @Benchmark |
| 72 | + public void largeFragmentationPerf(Input input) { |
| 73 | + Frame frame = |
| 74 | + input.largeFrameFragmenter.fragment(input.largeFrame).doOnNext(Frame::release).blockLast(); |
| 75 | + input.bh.consume(frame); |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + public void smallFragmentationFrameReassembler(Input input) { |
| 80 | + FrameReassembler smallFragmentAssembler = new FrameReassembler(input.smallFrame); |
| 81 | + |
| 82 | + input.smallFramesIterable.forEach(smallFragmentAssembler::append); |
| 83 | + |
| 84 | + Frame frame = smallFragmentAssembler.reassemble(); |
| 85 | + input.bh.consume(frame); |
| 86 | + frame.release(); |
| 87 | + //input.smallFragmentAssembler.clear(); |
| 88 | + } |
| 89 | + |
| 90 | + private static ByteBuffer createRandomBytes(int size) { |
| 91 | + byte[] bytes = new byte[size]; |
| 92 | + ThreadLocalRandom.current().nextBytes(bytes); |
| 93 | + return ByteBuffer.wrap(bytes); |
| 94 | + } |
93 | 95 | } |
0 commit comments