2 * libwebsockets-test-ping - libwebsockets floodping
4 * Copyright (C) 2011 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 #include <sys/types.h>
29 #include "../lib/libwebsockets.h"
33 #include <sys/socket.h>
35 #include <sys/ioctl.h>
39 #include "gettimeofday.h"
43 * this is specified in the 04 standard, control frames can only have small
44 * payload length styles
46 #define MAX_PING_PAYLOAD 125
47 #define MAX_MIRROR_PAYLOAD 4096
48 #define MAX_PING_CLIENTS 256
49 #define PING_RINGBUFFER_SIZE 256
51 static struct lws *ping_wsi[MAX_PING_CLIENTS];
52 static unsigned int interval_us = 1000000;
53 static unsigned int size = 64;
55 static const char *address;
56 static unsigned char pingbuf[LWS_SEND_BUFFER_PRE_PADDING + MAX_MIRROR_PAYLOAD +
57 LWS_SEND_BUFFER_POST_PADDING];
58 static char peer_name[128];
59 static unsigned long started;
60 static int screen_width = 80;
61 static int use_mirror;
62 static unsigned int write_options;
64 static unsigned long rtt_min = 100000000;
65 static unsigned long rtt_max;
66 static unsigned long rtt_avg;
67 static unsigned long global_rx_count;
68 static unsigned long global_tx_count;
69 static int clients = 1;
70 static unsigned long interrupted_time;
73 unsigned long issue_timestamp;
78 struct per_session_data__ping {
81 struct ping ringbuffer[PING_RINGBUFFER_SIZE];
85 unsigned long rx_count;
89 * uses the ping pong protocol features to provide an equivalent for the
90 * ping utility for 04+ websockets
103 callback_lws_mirror(struct lws_context * this,
105 enum lws_callback_reasons reason,
106 void *user, void *in, size_t len)
115 struct per_session_data__ping *psd = user;
118 case LWS_CALLBACK_CLOSED:
120 fprintf(stderr, "LWS_CALLBACK_CLOSED on %p\n", (void *)wsi);
122 /* remove closed guy */
124 for (n = 0; n < clients; n++)
125 if (ping_wsi[n] == wsi) {
127 while (n < clients) {
128 ping_wsi[n] = ping_wsi[n + 1];
135 case LWS_CALLBACK_CLIENT_ESTABLISHED:
139 psd->ringbuffer_head = 0;
140 psd->ringbuffer_tail = 0;
143 * start the ball rolling,
144 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
147 lws_callback_on_writable(wsi);
150 case LWS_CALLBACK_CLIENT_RECEIVE:
151 case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
152 gettimeofday(&tv, NULL);
153 iv = (tv.tv_sec * 1000000) + tv.tv_usec;
162 l |= ((uint64_t)*p++) << shift;
166 /* find it in the ringbuffer, look backwards from head */
167 n = psd->ringbuffer_head;
170 if (psd->ringbuffer[n].index == l) {
171 psd->ringbuffer[n].seen++;
176 if (n == psd->ringbuffer_tail) {
182 n = PING_RINGBUFFER_SIZE - 1;
190 fprintf(stderr, "%d bytes from %s: req=%ld "
191 "time=(unknown)\n", (int)len, address,
194 fprintf(stderr, "\b \b");
199 if (psd->ringbuffer[n].seen > 1)
200 fprintf(stderr, "DUP! ");
202 if ((iv - psd->ringbuffer[n].issue_timestamp) < rtt_min)
203 rtt_min = iv - psd->ringbuffer[n].issue_timestamp;
205 if ((iv - psd->ringbuffer[n].issue_timestamp) > rtt_max)
206 rtt_max = iv - psd->ringbuffer[n].issue_timestamp;
208 rtt_avg += iv - psd->ringbuffer[n].issue_timestamp;
212 fprintf(stderr, "%d bytes from %s: req=%ld "
213 "time=%lu.%lums\n", (int)len, address, (long)l,
214 (iv - psd->ringbuffer[n].issue_timestamp) / 1000,
215 ((iv - psd->ringbuffer[n].issue_timestamp) / 100) % 10);
217 fprintf(stderr, "\b \b");
220 case LWS_CALLBACK_CLIENT_WRITEABLE:
223 p = &pingbuf[LWS_SEND_BUFFER_PRE_PADDING];
225 /* 64-bit ping index in network byte order */
228 *p++ = psd->ping_index >> shift;
232 while (p - &pingbuf[LWS_SEND_BUFFER_PRE_PADDING] < size)
235 gettimeofday(&tv, NULL);
237 psd->ringbuffer[psd->ringbuffer_head].issue_timestamp =
238 (tv.tv_sec * 1000000) + tv.tv_usec;
239 psd->ringbuffer[psd->ringbuffer_head].index = psd->ping_index++;
240 psd->ringbuffer[psd->ringbuffer_head].seen = 0;
242 if (psd->ringbuffer_head == PING_RINGBUFFER_SIZE - 1)
243 psd->ringbuffer_head = 0;
245 psd->ringbuffer_head++;
247 /* snip any re-used tail so we keep to the ring length */
249 if (psd->ringbuffer_tail == psd->ringbuffer_head) {
250 if (psd->ringbuffer_tail == PING_RINGBUFFER_SIZE - 1)
251 psd->ringbuffer_tail = 0;
253 psd->ringbuffer_tail++;
260 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
261 size, write_options | LWS_WRITE_BINARY);
264 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
265 size, write_options | LWS_WRITE_PING);
270 lwsl_err("Partial write\n");
275 (psd->ping_index - psd->rx_count) < (screen_width - 1))
276 fprintf(stderr, ".");
287 /* list of supported protocols and callbacks */
289 static struct lws_protocols protocols[] = {
292 "lws-mirror-protocol",
294 sizeof (struct per_session_data__ping),
297 NULL, NULL, 0/* end of list */
301 static struct option options[] = {
302 { "help", no_argument, NULL, 'h' },
303 { "debug", required_argument, NULL, 'd' },
304 { "port", required_argument, NULL, 'p' },
305 { "ssl", no_argument, NULL, 't' },
306 { "interval", required_argument, NULL, 'i' },
307 { "size", required_argument, NULL, 's' },
308 { "protocol", required_argument, NULL, 'n' },
309 { "flood", no_argument, NULL, 'f' },
310 { "mirror", no_argument, NULL, 'm' },
311 { "replicate", required_argument, NULL, 'r' },
312 { "killmask", no_argument, NULL, 'k' },
313 { "version", required_argument, NULL, 'v' },
319 signal_handler(int sig, siginfo_t *si, void *v)
323 gettimeofday(&tv, NULL);
324 interrupted_time = (tv.tv_sec * 1000000) + tv.tv_usec;
328 int main(int argc, char **argv)
333 struct lws_context *context;
334 char protocol_name[256];
341 unsigned long oldus = 0;
343 int ietf_version = -1;
344 struct lws_context_creation_info info;
346 memset(&info, 0, sizeof info);
355 n = getopt_long(argc, argv, "v:kr:hmfts:n:i:p:d:", options, NULL);
360 lws_set_log_level(atoi(optarg), NULL);
366 use_ssl = 2; /* 2 = allow selfsigned */
372 strncpy(protocol_name, optarg, sizeof protocol_name);
373 protocol_name[(sizeof protocol_name) - 1] = '\0';
374 protocols[PROTOCOL_LWS_MIRROR].name = protocol_name;
377 interval_us = 1000000.0 * atof(optarg);
386 clients = atoi(optarg);
387 if (clients > MAX_PING_CLIENTS || clients < 1) {
388 fprintf(stderr, "Max clients supportd = %d\n",
394 write_options = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
397 ietf_version = atoi(optarg);
406 if (size > MAX_PING_PAYLOAD) {
407 fprintf(stderr, "Max ping opcode payload size %d\n",
412 if (size > MAX_MIRROR_PAYLOAD) {
413 fprintf(stderr, "Max mirror payload size %d\n",
420 if (isatty(STDOUT_FILENO))
421 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
423 screen_width = w.ws_col;
426 info.port = CONTEXT_PORT_NO_LISTEN;
427 info.protocols = protocols;
428 #ifndef LWS_NO_EXTENSIONS
429 info.extensions = lws_get_internal_extensions();
434 context = lws_create_context(&info);
435 if (context == NULL) {
436 fprintf(stderr, "Creating libwebsocket context failed\n");
440 /* create client websockets using dumb increment protocol */
442 for (n = 0; n < clients; n++) {
443 ping_wsi[n] = lws_client_connect(context, address,
444 port, use_ssl, "/", address,
445 "origin", protocols[PROTOCOL_LWS_MIRROR].name,
447 if (ping_wsi[n] == NULL) {
448 fprintf(stderr, "client connection %d failed to "
454 lws_get_peer_addresses(ping_wsi[0], lws_get_socket_fd(ping_wsi[0]),
455 peer_name, sizeof peer_name, ip, sizeof ip);
457 fprintf(stderr, "Websocket PING %s (%s) %d bytes of data.\n",
458 peer_name, ip, size);
461 /* set the ^C handler */
462 sa.sa_sigaction = signal_handler;
463 sa.sa_flags = SA_SIGINFO;
464 sigemptyset(&sa.sa_mask);
465 sigaction(SIGINT, &sa, NULL);
468 gettimeofday(&tv, NULL);
469 started = (tv.tv_sec * 1000000) + tv.tv_usec;
476 gettimeofday(&tv, NULL);
477 l = (tv.tv_sec * 1000000) + tv.tv_usec;
479 /* servers can hang up on us */
486 if (!interrupted_time) {
487 if ((l - oldus) > interval_us) {
488 for (n = 0; n < clients; n++)
489 lws_callback_on_writable(ping_wsi[n]);
494 /* allow time for in-flight pongs to come */
496 if ((l - interrupted_time) > 250000) {
502 n = lws_service(context, 0);
504 n = lws_service(context, 1);
509 fprintf(stderr, "\n--- %s websocket ping statistics "
510 "using %d connections ---\n"
511 "%lu packets transmitted, %lu received, "
512 "%lu%% packet loss, time %ldms\n"
513 "rtt min/avg/max = %0.3f/%0.3f/%0.3f ms\n"
514 "payload bandwidth average %0.3f KiBytes/sec\n",
515 peer_name, clients, global_tx_count, global_rx_count,
516 ((global_tx_count - global_rx_count) * 100) / global_tx_count,
517 (l - started) / 1000,
518 ((double)rtt_min) / 1000.0,
519 ((double)rtt_avg / global_rx_count) / 1000.0,
520 ((double)rtt_max) / 1000.0,
521 ((double)global_rx_count * (double)size) /
522 ((double)(l - started) / 1000000.0) / 1024.0);
524 lws_context_destroy(context);
529 fprintf(stderr, "Usage: libwebsockets-test-ping "
530 "<server address> [--port=<p>] "
531 "[--ssl] [--interval=<float sec>] "
533 "[--protocol=<protocolname>] "
535 "[--replicate=clients>] "
536 "[--version <version>] "
537 "[-d <log bitfield> ]"