|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2009, 2026 Mountainminds GmbH & Co. KG and Contributors |
| 3 | + * This program and the accompanying materials are made available under |
| 4 | + * the terms of the Eclipse Public License 2.0 which is available at |
| 5 | + * https://www.eclipse.org/legal/epl-2.0 |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Evgeny Mandrikov - initial API and implementation |
| 11 | + * |
| 12 | + *******************************************************************************/ |
| 13 | +package org.jacoco.core.internal.data; |
| 14 | + |
| 15 | +import java.io.BufferedInputStream; |
| 16 | +import java.io.BufferedOutputStream; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.OutputStream; |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.net.ServerSocket; |
| 21 | +import java.net.Socket; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import org.openjdk.jmh.annotations.Benchmark; |
| 25 | +import org.openjdk.jmh.annotations.Mode; |
| 26 | +import org.openjdk.jmh.annotations.Param; |
| 27 | +import org.openjdk.jmh.annotations.Scope; |
| 28 | +import org.openjdk.jmh.annotations.Setup; |
| 29 | +import org.openjdk.jmh.annotations.State; |
| 30 | +import org.openjdk.jmh.annotations.TearDown; |
| 31 | +import org.openjdk.jmh.runner.Runner; |
| 32 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 33 | +import org.openjdk.jmh.runner.options.TimeValue; |
| 34 | + |
| 35 | +/** |
| 36 | + * Benchmark for {@link CompactDataOutput} and {@link CompactDataInput} with |
| 37 | + * {@link Socket} connection. {@link BenchmarkSide#SENDER} and |
| 38 | + * {@link BenchmarkSide#RECEIVER} respectively. |
| 39 | + * |
| 40 | + * Demonstrates benefits of using {@link #bufferedSender} |
| 41 | + * ({@link BufferedInputStream} over raw {@link Socket#getInputStream()}) and |
| 42 | + * {@link #bufferedReceiver} ({@link BufferedOutputStream} over raw |
| 43 | + * {@link Socket#getOutputStream()}). |
| 44 | + * |
| 45 | + * Use of buffering only on sender side is less beneficial. |
| 46 | + * |
| 47 | + * Use of buffering only on receiver side is neither beneficial nor harmful. |
| 48 | + * |
| 49 | + * This asymmetry when only one side buffered is due to difference between |
| 50 | + * {@link Socket#getSendBufferSize()} and {@link Socket#getReceiveBufferSize()}. |
| 51 | + * |
| 52 | + * @see CompactDataOutputBenchmark |
| 53 | + */ |
| 54 | +@State(Scope.Thread) |
| 55 | +public class CompactDataThroughSocketBenchmark { |
| 56 | + |
| 57 | + public enum BenchmarkSide { |
| 58 | + SENDER, RECEIVER |
| 59 | + } |
| 60 | + |
| 61 | + @Param({ "SENDER", "RECEIVER" }) |
| 62 | + private BenchmarkSide benchmarkSide; |
| 63 | + |
| 64 | + @Param({ "true", "false" }) |
| 65 | + private boolean bufferedReceiver; |
| 66 | + |
| 67 | + @Param({ "true", "false" }) |
| 68 | + private boolean bufferedSender; |
| 69 | + |
| 70 | + public enum DataType { |
| 71 | + STRING, BOOLEAN_ARRAY |
| 72 | + } |
| 73 | + |
| 74 | + @Param({ "STRING", "BOOLEAN_ARRAY" }) |
| 75 | + public DataType dataType; |
| 76 | + |
| 77 | + @Param({ "32", "128", "1024" }) |
| 78 | + private int dataSize; |
| 79 | + |
| 80 | + private boolean[] booleanArray; |
| 81 | + private String string; |
| 82 | + |
| 83 | + interface Operation { |
| 84 | + void execute() throws Exception; |
| 85 | + } |
| 86 | + |
| 87 | + private Operation operation; |
| 88 | + private Socket socket; |
| 89 | + |
| 90 | + @Setup |
| 91 | + public void setup() throws Exception { |
| 92 | + booleanArray = new boolean[dataSize]; |
| 93 | + string = new String(new char[dataSize]); |
| 94 | + final ServerSocket serverSocket = new ServerSocket(0, 1); |
| 95 | + new Thread() { |
| 96 | + @Override |
| 97 | + public void run() { |
| 98 | + try { |
| 99 | + final Socket socket = serverSocket.accept(); |
| 100 | + serverSocket.close(); |
| 101 | + final Operation operation = setup(socket, |
| 102 | + benchmarkSide == BenchmarkSide.RECEIVER); |
| 103 | + while (true) { |
| 104 | + operation.execute(); |
| 105 | + } |
| 106 | + } catch (final Exception e) { |
| 107 | + // ignore |
| 108 | + } |
| 109 | + } |
| 110 | + }.start(); |
| 111 | + socket = new Socket(InetAddress.getByName(null), |
| 112 | + serverSocket.getLocalPort()); |
| 113 | + operation = setup(socket, benchmarkSide == BenchmarkSide.SENDER); |
| 114 | + } |
| 115 | + |
| 116 | + private Operation setup(final Socket socket, final boolean sender) |
| 117 | + throws Exception { |
| 118 | + if (sender) { |
| 119 | + OutputStream outputStream = socket.getOutputStream(); |
| 120 | + if (bufferedSender) { |
| 121 | + outputStream = new BufferedOutputStream(outputStream); |
| 122 | + } |
| 123 | + final CompactDataOutput output = new CompactDataOutput( |
| 124 | + outputStream); |
| 125 | + return new Operation() { |
| 126 | + public void execute() throws Exception { |
| 127 | + sendTo(output); |
| 128 | + } |
| 129 | + }; |
| 130 | + } else { |
| 131 | + InputStream inputStream = socket.getInputStream(); |
| 132 | + if (bufferedReceiver) { |
| 133 | + inputStream = new BufferedInputStream(inputStream); |
| 134 | + } |
| 135 | + final CompactDataInput input = new CompactDataInput(inputStream); |
| 136 | + return new Operation() { |
| 137 | + |
| 138 | + public void execute() throws Exception { |
| 139 | + receiveFrom(input); |
| 140 | + } |
| 141 | + |
| 142 | + }; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private void sendTo(final CompactDataOutput output) throws Exception { |
| 147 | + switch (dataType) { |
| 148 | + case STRING: |
| 149 | + output.writeUTF(string); |
| 150 | + break; |
| 151 | + case BOOLEAN_ARRAY: |
| 152 | + output.writeBooleanArray(booleanArray); |
| 153 | + break; |
| 154 | + default: |
| 155 | + throw new IllegalStateException(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private void receiveFrom(final CompactDataInput input) throws Exception { |
| 160 | + switch (dataType) { |
| 161 | + case STRING: |
| 162 | + input.readUTF(); |
| 163 | + break; |
| 164 | + case BOOLEAN_ARRAY: |
| 165 | + input.readBooleanArray(); |
| 166 | + break; |
| 167 | + default: |
| 168 | + throw new IllegalStateException(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Benchmark |
| 173 | + public void benchmark() throws Exception { |
| 174 | + operation.execute(); |
| 175 | + } |
| 176 | + |
| 177 | + @TearDown |
| 178 | + public void tearDown() throws Exception { |
| 179 | + socket.close(); |
| 180 | + } |
| 181 | + |
| 182 | + public static void main(String[] args) throws Exception { |
| 183 | + new Runner(new OptionsBuilder() // |
| 184 | + .include(CompactDataThroughSocketBenchmark.class.getName()) // |
| 185 | + .mode(Mode.AverageTime) // |
| 186 | + .timeUnit(TimeUnit.MICROSECONDS) // |
| 187 | + .warmupIterations(5) // |
| 188 | + .warmupTime(TimeValue.seconds(1)) // |
| 189 | + .measurementIterations(10) // |
| 190 | + .measurementTime(TimeValue.seconds(1)) // |
| 191 | + .forks(1) // |
| 192 | + .build()).run(); |
| 193 | + } |
| 194 | +} |
0 commit comments