| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// |
| 5 |
// This program and the accompanying materials are made available under the |
| 6 |
// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 |
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 |
// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 |
// |
| 10 |
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 |
// ======================================================================== |
| 12 |
// |
| 13 |
|
| 14 |
package examples; |
| 15 |
|
| 16 |
import java.net.InetSocketAddress; |
| 17 |
import java.net.URI; |
| 18 |
import java.nio.charset.StandardCharsets; |
| 19 |
import java.time.Duration; |
| 20 |
import java.util.Arrays; |
| 21 |
import java.util.Random; |
| 22 |
import java.util.concurrent.TimeUnit; |
| 23 |
import java.util.concurrent.atomic.AtomicInteger; |
| 24 |
import java.util.concurrent.atomic.AtomicLong; |
| 25 |
|
| 26 |
import org.eclipse.jetty.websocket.api.Session; |
| 27 |
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; |
| 28 |
import org.eclipse.jetty.websocket.client.WebSocketClient; |
| 29 |
import org.eclipse.jetty.websocket.core.OpCode; |
| 30 |
|
| 31 |
/** |
| 32 |
* This is not a general purpose websocket client. |
| 33 |
* <p> |
| 34 |
* It's only for testing the websocket server and is hardwired to a specific draft version of the protocol. |
| 35 |
* </p> |
| 36 |
*/ |
| 37 |
public class ClientDemo |
| 38 |
{ |
| 39 |
public class TestSocket extends Session.Listener.Abstract |
| 40 |
{ |
| 41 |
@Override |
| 42 |
public void onWebSocketOpen(Session session) |
| 43 |
{ |
| 44 |
if (verbose) |
| 45 |
System.err.printf("%s#onWebSocketOpen %s %s\n", this.getClass().getSimpleName(), session, session.getClass().getSimpleName()); |
| 46 |
} |
| 47 |
|
| 48 |
public void send(byte op, byte[] data, int maxFragmentLength) |
| 49 |
{ |
| 50 |
int off = 0; |
| 51 |
int len = data.length; |
| 52 |
if ((maxFragmentLength > 0) && (len > maxFragmentLength)) |
| 53 |
{ |
| 54 |
len = maxFragmentLength; |
| 55 |
} |
| 56 |
messagesSent++; |
| 57 |
while (off < data.length) |
| 58 |
{ |
| 59 |
framesSent++; |
| 60 |
|
| 61 |
off += len; |
| 62 |
if ((data.length - off) > len) |
| 63 |
{ |
| 64 |
len = data.length - off; |
| 65 |
} |
| 66 |
if ((maxFragmentLength > 0) && (len > maxFragmentLength)) |
| 67 |
{ |
| 68 |
len = maxFragmentLength; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
private static boolean verbose = false; |
| 75 |
|
| 76 |
private static final Random RANDOM = new Random(); |
| 77 |
|
| 78 |
private final String host; |
| 79 |
private final int port; |
| 80 |
private final String protocol; |
| 81 |
private final int timeout; |
| 82 |
|
| 83 |
private static int framesSent; |
| 84 |
private static int messagesSent; |
| 85 |
private static AtomicInteger framesReceived = new AtomicInteger(); |
| 86 |
private static AtomicInteger messagesReceived = new AtomicInteger(); |
| 87 |
|
| 88 |
private static AtomicLong totalTime = new AtomicLong(); |
| 89 |
private static AtomicLong minDuration = new AtomicLong(Long.MAX_VALUE); |
| 90 |
private static AtomicLong maxDuration = new AtomicLong(Long.MIN_VALUE); |
| 91 |
private static long start; |
| 92 |
|
| 93 |
public static void main(String[] args) throws Exception |
| 94 |
{ |
| 95 |
String host = "localhost"; |
| 96 |
int port = 8080; |
| 97 |
String protocol = null; |
| 98 |
int count = 10; |
| 99 |
int size = 64; |
| 100 |
int fragment = 4000; |
| 101 |
boolean binary = false; |
| 102 |
int clients = 1; |
| 103 |
int delay = 1000; |
| 104 |
|
| 105 |
for (int i = 0; i < args.length; i++) |
| 106 |
{ |
| 107 |
String a = args[i]; |
| 108 |
if ("-p".equals(a) || "--port".equals(a)) |
| 109 |
{ |
| 110 |
port = Integer.parseInt(args[++i]); |
| 111 |
} |
| 112 |
else if ("-h".equals(a) || "--host".equals(a)) |
| 113 |
{ |
| 114 |
host = args[++i]; |
| 115 |
} |
| 116 |
else if ("-c".equals(a) || "--count".equals(a)) |
| 117 |
{ |
| 118 |
count = Integer.parseInt(args[++i]); |
| 119 |
} |
| 120 |
else if ("-s".equals(a) || "--size".equals(a)) |
| 121 |
{ |
| 122 |
size = Integer.parseInt(args[++i]); |
| 123 |
} |
| 124 |
else if ("-f".equals(a) || "--fragment".equals(a)) |
| 125 |
{ |
| 126 |
fragment = Integer.parseInt(args[++i]); |
| 127 |
} |
| 128 |
else if ("-P".equals(a) || "--protocol".equals(a)) |
| 129 |
{ |
| 130 |
protocol = args[++i]; |
| 131 |
} |
| 132 |
else if ("-v".equals(a) || "--verbose".equals(a)) |
| 133 |
{ |
| 134 |
verbose = true; |
| 135 |
} |
| 136 |
else if ("-b".equals(a) || "--binary".equals(a)) |
| 137 |
{ |
| 138 |
binary = true; |
| 139 |
} |
| 140 |
else if ("-C".equals(a) || "--clients".equals(a)) |
| 141 |
{ |
| 142 |
clients = Integer.parseInt(args[++i]); |
| 143 |
} |
| 144 |
else if ("-d".equals(a) || "--delay".equals(a)) |
| 145 |
{ |
| 146 |
delay = Integer.parseInt(args[++i]); |
| 147 |
} |
| 148 |
else if (a.startsWith("-")) |
| 149 |
{ |
| 150 |
usage(args); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
ClientDemo[] client = new ClientDemo[clients]; |
| 155 |
WebSocketClient wsclient = new WebSocketClient(); |
| 156 |
try |
| 157 |
{ |
| 158 |
wsclient.start(); |
| 159 |
start = System.currentTimeMillis(); |
| 160 |
protocol = protocol == null ? "echo" : protocol; |
| 161 |
|
| 162 |
for (int i = 0; i < clients; i++) |
| 163 |
{ |
| 164 |
client[i] = new ClientDemo(wsclient, host, port, protocol, 60000); |
| 165 |
client[i].open(); |
| 166 |
} |
| 167 |
|
| 168 |
System.out |
| 169 |
.println("Jetty WebSocket PING " + host + ":" + port + " (" + new InetSocketAddress(host, port) + ") " + clients + " clients " + protocol); |
| 170 |
|
| 171 |
for (int p = 0; p < count; p++) |
| 172 |
{ |
| 173 |
long next = System.currentTimeMillis() + delay; |
| 174 |
|
| 175 |
byte op = OpCode.TEXT; |
| 176 |
if (binary) |
| 177 |
{ |
| 178 |
op = OpCode.BINARY; |
| 179 |
} |
| 180 |
|
| 181 |
byte[] data = null; |
| 182 |
|
| 183 |
switch (op) |
| 184 |
{ |
| 185 |
case OpCode.TEXT: |
| 186 |
{ |
| 187 |
StringBuilder b = new StringBuilder(); |
| 188 |
while (b.length() < size) |
| 189 |
{ |
| 190 |
b.append('A' + RANDOM.nextInt(26)); |
| 191 |
} |
| 192 |
data = b.toString().getBytes(StandardCharsets.UTF_8); |
| 193 |
break; |
| 194 |
} |
| 195 |
case OpCode.BINARY: |
| 196 |
{ |
| 197 |
data = new byte[size]; |
| 198 |
RANDOM.nextBytes(data); |
| 199 |
break; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
for (int i = 0; i < clients; i++) |
| 204 |
{ |
| 205 |
client[i].send(op, data, fragment); |
| 206 |
} |
| 207 |
|
| 208 |
while (System.currentTimeMillis() < next) |
| 209 |
{ |
| 210 |
Thread.sleep(10); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
finally |
| 215 |
{ |
| 216 |
for (int i = 0; i < clients; i++) |
| 217 |
{ |
| 218 |
if (client[i] != null) |
| 219 |
{ |
| 220 |
client[i].disconnect(); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
long duration = System.currentTimeMillis() - start; |
| 225 |
System.out.println("--- " + host + " websocket ping statistics using " + clients + " connection" + (clients > 1 ? "s" : "") + " ---"); |
| 226 |
System.out.printf("%d/%d frames sent/recv, %d/%d mesg sent/recv, time %dms %dm/s %.2fbps%n", framesSent, framesReceived.get(), messagesSent, |
| 227 |
messagesReceived.get(), duration, ((1000L * messagesReceived.get()) / duration), |
| 228 |
(1000.0D * messagesReceived.get() * 8 * size) / duration / 1024 / 1024); |
| 229 |
System.out.printf("rtt min/ave/max = %.3f/%.3f/%.3f ms\n", minDuration.get() / 1000000.0, |
| 230 |
messagesReceived.get() == 0 ? 0.0 : (totalTime.get() / messagesReceived.get() / 1000000.0), maxDuration.get() / 1000000.0); |
| 231 |
|
| 232 |
wsclient.stop(); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
private static void usage(String[] args) |
| 237 |
{ |
| 238 |
System.err.println("ERROR: " + Arrays.asList(args)); |
| 239 |
System.err.println("USAGE: java -cp CLASSPATH " + ClientDemo.class + " [ OPTIONS ]"); |
| 240 |
System.err.println(" -h|--host HOST (default localhost)"); |
| 241 |
System.err.println(" -p|--port PORT (default 8080)"); |
| 242 |
System.err.println(" -b|--binary"); |
| 243 |
System.err.println(" -v|--verbose"); |
| 244 |
System.err.println(" -c|--count n (default 10)"); |
| 245 |
System.err.println(" -s|--size n (default 64)"); |
| 246 |
System.err.println(" -f|--fragment n (default 4000) "); |
| 247 |
System.err.println(" -P|--protocol echo|echo-assemble|echo-fragment|echo-broadcast"); |
| 248 |
System.err.println(" -C|--clients n (default 1) "); |
| 249 |
System.err.println(" -d|--delay n (default 1000ms) "); |
| 250 |
System.exit(1); |
| 251 |
} |
| 252 |
|
| 253 |
private WebSocketClient client; |
| 254 |
private TestSocket socket; |
| 255 |
|
| 256 |
public ClientDemo(WebSocketClient client, String host, int port, String protocol, int timeoutMS) throws Exception |
| 257 |
{ |
| 258 |
this.client = client; |
| 259 |
this.host = host; |
| 260 |
this.port = port; |
| 261 |
this.protocol = protocol; |
| 262 |
timeout = timeoutMS; |
| 263 |
} |
| 264 |
|
| 265 |
private void disconnect() |
| 266 |
{ |
| 267 |
// TODO Auto-generated method stub |
| 268 |
} |
| 269 |
|
| 270 |
private void open() throws Exception |
| 271 |
{ |
| 272 |
client.setIdleTimeout(Duration.ofMillis(timeout)); |
| 273 |
ClientUpgradeRequest request = new ClientUpgradeRequest(); |
| 274 |
request.setSubProtocols(protocol); |
| 275 |
socket = new TestSocket(); |
| 276 |
URI wsUri = new URI("ws://" + host + ":" + port + "/"); |
| 277 |
client.connect(socket, wsUri, request).get(10, TimeUnit.SECONDS); |
| 278 |
} |
| 279 |
|
| 280 |
private void send(byte op, byte[] data, int fragment) |
| 281 |
{ |
| 282 |
socket.send(op, data, fragment); |
| 283 |
} |
| 284 |
} |