author | egahlin |
Wed, 03 Jul 2019 22:51:44 +0200 | |
branch | JEP-349-branch |
changeset 57449 | 099789ceff7d |
parent 52595 | 16609197022c |
permissions | -rw-r--r-- |
52595
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
1 |
/* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
2 |
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
4 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
8 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
13 |
* accompanied this code). |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
14 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
18 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
21 |
* questions. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
22 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
23 |
package org.openjdk.bench.java.net; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
24 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
25 |
import org.openjdk.jmh.annotations.Benchmark; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
26 |
import org.openjdk.jmh.annotations.BenchmarkMode; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
27 |
import org.openjdk.jmh.annotations.Mode; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
28 |
import org.openjdk.jmh.annotations.OutputTimeUnit; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
29 |
import org.openjdk.jmh.annotations.Scope; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
30 |
import org.openjdk.jmh.annotations.Setup; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
31 |
import org.openjdk.jmh.annotations.State; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
32 |
import org.openjdk.jmh.annotations.TearDown; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
33 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
34 |
import java.io.IOException; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
35 |
import java.io.InputStream; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
36 |
import java.io.OutputStream; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
37 |
import java.net.InetAddress; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
38 |
import java.net.ServerSocket; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
39 |
import java.net.Socket; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
40 |
import java.util.concurrent.TimeUnit; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
41 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
42 |
/** |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
43 |
* Micro benchmark for streaming data over a Socket. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
44 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
45 |
@BenchmarkMode(Mode.AverageTime) |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
46 |
@OutputTimeUnit(TimeUnit.MILLISECONDS) |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
47 |
@State(Scope.Thread) |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
48 |
public class SocketStreaming { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
49 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
50 |
/** The bytes to write/read. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
51 |
public static final int dataLength = 16383; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
52 |
/** setTcpNoDelay(noNagle) */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
53 |
public static final boolean noNagle = false; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
54 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
55 |
private WriterThread writerThread; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
56 |
private Socket readSocket; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
57 |
private byte[] bytes; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
58 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
59 |
@Setup |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
60 |
public void prepare() throws Exception { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
61 |
bytes = new byte[dataLength]; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
62 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
63 |
// Setup the writer thread |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
64 |
writerThread = new WriterThread(dataLength, noNagle); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
65 |
writerThread.start(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
66 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
67 |
// Wait for a read socket |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
68 |
readSocket = writerThread.waitForReadSocket(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
69 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
70 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
71 |
@TearDown |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
72 |
public void cleanup() throws IOException { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
73 |
// Take down the writer thread and the reader socket |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
74 |
writerThread.finish(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
75 |
while (!readSocket.isClosed()) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
76 |
readSocket.close(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
77 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
78 |
readSocket = null; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
79 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
80 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
81 |
@Benchmark |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
82 |
public void testSocketInputStreamRead() throws InterruptedException, IOException { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
83 |
InputStream in = readSocket.getInputStream(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
84 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
85 |
// Notify the writer thread to add elements to stream |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
86 |
writerThread.requestSendBytes(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
87 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
88 |
// Read these from the stream |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
89 |
int bytesRead = 0; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
90 |
while (bytesRead < dataLength) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
91 |
int lastRead = in.read(bytes); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
92 |
if (lastRead < 0) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
93 |
throw new InternalError("Unexpectedly got " + lastRead + " bytes from the socket"); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
94 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
95 |
bytesRead += lastRead; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
96 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
97 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
98 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
99 |
/** |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
100 |
* Thread used to write bytes to a socket. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
101 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
102 |
private class WriterThread extends Thread { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
103 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
104 |
/** The number of bytes to write. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
105 |
private int dataLength; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
106 |
/** setTcpNoDelay(noNagle) */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
107 |
private boolean noNagle; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
108 |
/** Lock needed to send sendBytes requests. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
109 |
private final Object sendBytesLock = new Object(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
110 |
/** Indicates that a sendBytes has been requested. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
111 |
private boolean sendBytesRequested; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
112 |
/** Indicates that no more sendBytes will be requested. Time to shutdown. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
113 |
private boolean sendBytesDone; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
114 |
/** Lock needed to protect the connectPort variable. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
115 |
private final Object connectLock = new Object(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
116 |
/** The port the read socket should connect to. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
117 |
private int connectPort = -1; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
118 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
119 |
/** |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
120 |
* Constructor. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
121 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
122 |
* @param dataLength The number of bytes to write |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
123 |
* @param noNagle setTcpNoDelay(noNagle) |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
124 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
125 |
public WriterThread(int dataLength, boolean noNagle) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
126 |
super("Load producer"); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
127 |
this.dataLength = dataLength; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
128 |
this.noNagle = noNagle; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
129 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
130 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
131 |
/** Entry point for data sending helper thread. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
132 |
@Override |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
133 |
public void run() { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
134 |
try { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
135 |
Socket writeSocket; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
136 |
ServerSocket serverSocket = new ServerSocket(0); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
137 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
138 |
/* Tell the other thread that we now know the port number. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
139 |
* The other thread will now start to connect until the following accept() call succeeds. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
140 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
141 |
synchronized (connectLock) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
142 |
connectPort = serverSocket.getLocalPort(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
143 |
connectLock.notify(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
144 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
145 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
146 |
// Wait for the other thread to connect |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
147 |
writeSocket = serverSocket.accept(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
148 |
writeSocket.setTcpNoDelay(noNagle); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
149 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
150 |
// No more connects so this can be closed |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
151 |
serverSocket.close(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
152 |
serverSocket = null; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
153 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
154 |
OutputStream out = writeSocket.getOutputStream(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
155 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
156 |
// Iterate as long as sendBytes are issued |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
157 |
while (waitForSendBytesRequest()) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
158 |
sendBytes(out); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
159 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
160 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
161 |
// Time to shutdown |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
162 |
while (!writeSocket.isClosed()) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
163 |
writeSocket.close(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
164 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
165 |
writeSocket = null; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
166 |
} catch (Exception e) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
167 |
System.exit(1); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
168 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
169 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
170 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
171 |
/** |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
172 |
* Sends bytes to the output stream |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
173 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
174 |
* @param out The output stream |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
175 |
* @throws IOException |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
176 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
177 |
private void sendBytes(OutputStream out) throws IOException { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
178 |
byte outBytes[] = new byte[dataLength]; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
179 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
180 |
int bytesToSend = dataLength; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
181 |
int bytesSent = 0; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
182 |
while (bytesSent < bytesToSend) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
183 |
out.write(outBytes); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
184 |
bytesSent += outBytes.length; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
185 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
186 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
187 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
188 |
/** |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
189 |
* Waits for the readSocket and returns it when it is ready. |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
190 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
191 |
* @return The socket to read from |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
192 |
* @throws InterruptedException |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
193 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
194 |
@SuppressWarnings("SleepWhileHoldingLock") |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
195 |
public Socket waitForReadSocket() throws InterruptedException { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
196 |
int theConnectPort = waitForConnectPort(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
197 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
198 |
while (true) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
199 |
try { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
200 |
return new Socket(InetAddress.getByName(null), theConnectPort); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
201 |
} catch (IOException e) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
202 |
// Wait some more for the server thread to get going |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
203 |
Thread.sleep(1000); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
204 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
205 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
206 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
207 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
208 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
209 |
/** |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
210 |
* Waits for next sendBytes request |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
211 |
* |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
212 |
* @return <code>true</code> if it is time to sendBytes, <code>false</code> if it is time to shutdown |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
213 |
* @throws InterruptedException |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
214 |
*/ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
215 |
public boolean waitForSendBytesRequest() throws InterruptedException { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
216 |
synchronized (sendBytesLock) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
217 |
while (!sendBytesRequested && !sendBytesDone) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
218 |
sendBytesLock.wait(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
219 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
220 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
221 |
// Clear the flag |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
222 |
sendBytesRequested = false; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
223 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
224 |
return !sendBytesDone; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
225 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
226 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
227 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
228 |
/** Requests a sendBytes. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
229 |
public void requestSendBytes() { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
230 |
synchronized (sendBytesLock) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
231 |
sendBytesRequested = true; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
232 |
sendBytesLock.notify(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
233 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
234 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
235 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
236 |
/** Tells the writerThread that it is time to shutdown. */ |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
237 |
public void finish() { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
238 |
synchronized (sendBytesLock) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
239 |
sendBytesDone = true; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
240 |
sendBytesLock.notify(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
241 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
242 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
243 |
|
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
244 |
private int waitForConnectPort() throws InterruptedException { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
245 |
synchronized (connectLock) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
246 |
while (connectPort == -1) { |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
247 |
connectLock.wait(); |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
248 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
249 |
return connectPort; |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
250 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
251 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
252 |
} |
16609197022c
8061281: Microbenchmark suite build support, directory layout and sample benchmarks
redestad
parents:
diff
changeset
|
253 |
} |