Skip to content

Commit 5c05141

Browse files
authored
Transfer of execution data through socket should use buffered stream (#2089)
1 parent ab5efa9 commit 5c05141

10 files changed

Lines changed: 390 additions & 9 deletions

File tree

‎org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/output/TcpConnection.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*******************************************************************************/
1313
package org.jacoco.agent.rt.internal.output;
1414

15+
import java.io.BufferedOutputStream;
1516
import java.io.IOException;
1617
import java.net.Socket;
1718
import java.net.SocketException;
@@ -43,8 +44,12 @@ public TcpConnection(final Socket socket, final RuntimeData data) {
4344
}
4445

4546
public void init() throws IOException {
46-
this.writer = new RemoteControlWriter(socket.getOutputStream());
47-
this.reader = new RemoteControlReader(socket.getInputStream());
47+
this.writer = new RemoteControlWriter(
48+
new BufferedOutputStream(socket.getOutputStream()));
49+
this.reader = new RemoteControlReader(
50+
// BufferedInputStream will not improve performance here
51+
// while will add memory overhead because commands are short
52+
socket.getInputStream());
4853
this.reader.setRemoteCommandVisitor(this);
4954
this.initialized = true;
5055
}
@@ -107,6 +112,7 @@ public void visitDumpCommand(final boolean dump, final boolean reset)
107112
}
108113
}
109114
writer.sendCmdOk();
115+
writer.flush();
110116
}
111117

112118
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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.File;
18+
import java.io.FileOutputStream;
19+
import java.io.InputStream;
20+
import java.io.OutputStream;
21+
import java.net.InetAddress;
22+
import java.net.ServerSocket;
23+
import java.net.Socket;
24+
import java.util.concurrent.TimeUnit;
25+
26+
import org.openjdk.jmh.annotations.Benchmark;
27+
import org.openjdk.jmh.annotations.Mode;
28+
import org.openjdk.jmh.annotations.Param;
29+
import org.openjdk.jmh.annotations.Scope;
30+
import org.openjdk.jmh.annotations.Setup;
31+
import org.openjdk.jmh.annotations.State;
32+
import org.openjdk.jmh.annotations.TearDown;
33+
import org.openjdk.jmh.runner.Runner;
34+
import org.openjdk.jmh.runner.options.OptionsBuilder;
35+
import org.openjdk.jmh.runner.options.TimeValue;
36+
37+
/**
38+
* Benchmark for {@link CompactDataOutput} and {@link CompactDataInput}
39+
* connected through {@link Socket} to {@link FileOutputStream}.
40+
*
41+
* Demonstrates benefits of using {@link #bufferedFileWriter}
42+
* ({@link BufferedOutputStream} over raw {@link FileOutputStream}),
43+
* {@link #bufferedSocketReceiver} ({@link BufferedInputStream} over raw
44+
* {@link Socket#getInputStream()}) and {@link #bufferedSocketSender}
45+
* ({@link BufferedOutputStream} over raw {@link Socket#getOutputStream()})
46+
* together.
47+
*
48+
* @see CompactDataOutputBenchmark
49+
* @see CompactDataThroughSocketBenchmark
50+
*/
51+
@State(Scope.Thread)
52+
public class CompactDataThroughSocketToFileBenchmark {
53+
54+
@Param({ "32", "128", "1024" })
55+
private int dataSize;
56+
57+
private boolean[] booleanArray;
58+
59+
private Socket socket;
60+
61+
@Param({ "true", "false" })
62+
private boolean bufferedSocketSender;
63+
64+
@Param({ "true", "false" })
65+
private boolean bufferedSocketReceiver;
66+
67+
@Param({ "true", "false" })
68+
private boolean bufferedFileWriter;
69+
70+
private CompactDataInput input;
71+
private CompactDataOutput output;
72+
private FileOutputStream fileOutputStream;
73+
74+
@Setup
75+
public void setup() throws Exception {
76+
booleanArray = new boolean[dataSize];
77+
final ServerSocket serverSocket = new ServerSocket(0, 1);
78+
new Thread() {
79+
@Override
80+
public void run() {
81+
try {
82+
final Socket socket = serverSocket.accept();
83+
serverSocket.close();
84+
OutputStream outputStream = socket.getOutputStream();
85+
if (bufferedSocketSender) {
86+
outputStream = new BufferedOutputStream(outputStream);
87+
}
88+
final CompactDataOutput output = new CompactDataOutput(
89+
outputStream);
90+
while (true) {
91+
output.writeBooleanArray(booleanArray);
92+
}
93+
} catch (final Exception e) {
94+
// ignore
95+
}
96+
}
97+
}.start();
98+
socket = new Socket(InetAddress.getByName(null),
99+
serverSocket.getLocalPort());
100+
101+
InputStream inputStream = socket.getInputStream();
102+
if (bufferedSocketReceiver) {
103+
inputStream = new BufferedInputStream(inputStream);
104+
}
105+
input = new CompactDataInput(inputStream);
106+
107+
final File file = File.createTempFile(
108+
CompactDataThroughSocketToFileBenchmark.class.getName(), "out");
109+
fileOutputStream = new FileOutputStream(file);
110+
output = new CompactDataOutput(
111+
bufferedFileWriter ? new BufferedOutputStream(fileOutputStream)
112+
: fileOutputStream);
113+
}
114+
115+
@Benchmark
116+
public void benchmark() throws Exception {
117+
fileOutputStream.getChannel().position(0);
118+
output.writeBooleanArray(input.readBooleanArray());
119+
output.flush();
120+
}
121+
122+
@TearDown
123+
public void tearDown() throws Exception {
124+
socket.close();
125+
}
126+
127+
public static void main(String[] args) throws Exception {
128+
new Runner(new OptionsBuilder() //
129+
.include(
130+
CompactDataThroughSocketToFileBenchmark.class.getName()) //
131+
.mode(Mode.AverageTime) //
132+
.timeUnit(TimeUnit.MICROSECONDS) //
133+
.warmupIterations(5) //
134+
.warmupTime(TimeValue.seconds(1)) //
135+
.measurementIterations(10) //
136+
.measurementTime(TimeValue.seconds(1)) //
137+
.forks(1) //
138+
.build()).run();
139+
}
140+
}

‎org.jacoco.core/src/org/jacoco/core/runtime/RemoteControlReader.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ public class RemoteControlReader extends ExecutionDataReader {
2727
/**
2828
* Creates a new reader based on the given input stream.
2929
*
30+
* Depending on the nature of the underlying stream input should be buffered
31+
* as most data is read in single bytes.
32+
*
33+
* Should be buffered when used to read execution data from
34+
* {@link java.net.Socket#getInputStream()}.
35+
*
36+
* Should not be buffered when used only for commands because they are short
37+
* and this will merely add memory overhead.
38+
*
3039
* @param input
3140
* input stream to read commands from
3241
* @throws IOException

‎org.jacoco.core/src/org/jacoco/core/runtime/RemoteControlWriter.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ public class RemoteControlWriter extends ExecutionDataWriter
3232
/**
3333
* Creates a new writer based on the given output stream.
3434
*
35+
* Depending on the nature of the underlying stream output should be
36+
* buffered as most data is written in single bytes.
37+
*
38+
* Should be buffered when used to write execution data to
39+
* {@link java.net.Socket#getOutputStream()}.
40+
*
41+
* Should not be buffered when used only for commands because they are short
42+
* and this will merely add memory overhead.
43+
*
3544
* @param output
3645
* stream to write commands to
3746
* @throws IOException

0 commit comments

Comments
 (0)