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,
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
40 #include "../lib/libwebsockets.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 libwebsocket *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 {
79 unsigned long ping_index;
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 libwebsocket_context * this,
104 struct libwebsocket *wsi,
105 enum libwebsocket_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 libwebsocket_callback_on_writable(this, 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 |= (*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, l);
193 fprintf(stderr, "\b \b");
198 if (psd->ringbuffer[n].seen > 1)
199 fprintf(stderr, "DUP! ");
201 if ((iv - psd->ringbuffer[n].issue_timestamp) < rtt_min)
202 rtt_min = iv - psd->ringbuffer[n].issue_timestamp;
204 if ((iv - psd->ringbuffer[n].issue_timestamp) > rtt_max)
205 rtt_max = iv - psd->ringbuffer[n].issue_timestamp;
207 rtt_avg += iv - psd->ringbuffer[n].issue_timestamp;
211 fprintf(stderr, "%d bytes from %s: req=%ld "
212 "time=%lu.%lums\n", (int)len, address, l,
213 (iv - psd->ringbuffer[n].issue_timestamp) / 1000,
214 ((iv - psd->ringbuffer[n].issue_timestamp) / 100) % 10);
216 fprintf(stderr, "\b \b");
219 case LWS_CALLBACK_CLIENT_WRITEABLE:
222 p = &pingbuf[LWS_SEND_BUFFER_PRE_PADDING];
224 /* 64-bit ping index in network byte order */
227 *p++ = psd->ping_index >> shift;
231 while (p - &pingbuf[LWS_SEND_BUFFER_PRE_PADDING] < size)
234 gettimeofday(&tv, NULL);
236 psd->ringbuffer[psd->ringbuffer_head].issue_timestamp =
237 (tv.tv_sec * 1000000) + tv.tv_usec;
238 psd->ringbuffer[psd->ringbuffer_head].index = psd->ping_index++;
239 psd->ringbuffer[psd->ringbuffer_head].seen = 0;
241 if (psd->ringbuffer_head == PING_RINGBUFFER_SIZE - 1)
242 psd->ringbuffer_head = 0;
244 psd->ringbuffer_head++;
246 /* snip any re-used tail so we keep to the ring length */
248 if (psd->ringbuffer_tail == psd->ringbuffer_head) {
249 if (psd->ringbuffer_tail == PING_RINGBUFFER_SIZE - 1)
250 psd->ringbuffer_tail = 0;
252 psd->ringbuffer_tail++;
258 libwebsocket_write(wsi,
259 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
260 size, write_options | LWS_WRITE_BINARY);
262 libwebsocket_write(wsi,
263 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
264 size, write_options | LWS_WRITE_PING);
267 (psd->ping_index - psd->rx_count) < (screen_width - 1))
268 fprintf(stderr, ".");
279 /* list of supported protocols and callbacks */
281 static struct libwebsocket_protocols protocols[] = {
284 "lws-mirror-protocol",
286 sizeof (struct per_session_data__ping),
289 NULL, NULL, 0/* end of list */
293 static struct option options[] = {
294 { "help", no_argument, NULL, 'h' },
295 { "debug", required_argument, NULL, 'd' },
296 { "port", required_argument, NULL, 'p' },
297 { "ssl", no_argument, NULL, 't' },
298 { "interval", required_argument, NULL, 'i' },
299 { "size", required_argument, NULL, 's' },
300 { "protocol", required_argument, NULL, 'n' },
301 { "flood", no_argument, NULL, 'f' },
302 { "mirror", no_argument, NULL, 'm' },
303 { "replicate", required_argument, NULL, 'r' },
304 { "killmask", no_argument, NULL, 'k' },
305 { "version", required_argument, NULL, 'v' },
311 signal_handler(int sig, siginfo_t *si, void *v)
315 gettimeofday(&tv, NULL);
316 interrupted_time = (tv.tv_sec * 1000000) + tv.tv_usec;
320 int main(int argc, char **argv)
325 struct libwebsocket_context *context;
326 char protocol_name[256];
333 unsigned long oldus = 0;
335 int ietf_version = -1;
336 struct lws_context_creation_info info;
338 memset(&info, 0, sizeof info);
347 n = getopt_long(argc, argv, "v:kr:hmfts:n:i:p:d:", options, NULL);
352 lws_set_log_level(atoi(optarg), NULL);
358 use_ssl = 2; /* 2 = allow selfsigned */
364 strncpy(protocol_name, optarg, sizeof protocol_name);
365 protocol_name[(sizeof protocol_name) - 1] = '\0';
366 protocols[PROTOCOL_LWS_MIRROR].name = protocol_name;
369 interval_us = 1000000.0 * atof(optarg);
378 clients = atoi(optarg);
379 if (clients > MAX_PING_CLIENTS || clients < 1) {
380 fprintf(stderr, "Max clients supportd = %d\n",
386 write_options = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
389 ietf_version = atoi(optarg);
398 if (size > MAX_PING_PAYLOAD) {
399 fprintf(stderr, "Max ping opcode payload size %d\n",
404 if (size > MAX_MIRROR_PAYLOAD) {
405 fprintf(stderr, "Max mirror payload size %d\n",
412 if (isatty(STDOUT_FILENO))
413 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
415 screen_width = w.ws_col;
418 info.port = CONTEXT_PORT_NO_LISTEN;
419 info.protocols = protocols;
420 #ifndef LWS_NO_EXTENSIONS
421 info.extensions = libwebsocket_internal_extensions;
426 context = libwebsocket_create_context(&info);
427 if (context == NULL) {
428 fprintf(stderr, "Creating libwebsocket context failed\n");
432 /* create client websockets using dumb increment protocol */
434 for (n = 0; n < clients; n++) {
435 ping_wsi[n] = libwebsocket_client_connect(context, address,
436 port, use_ssl, "/", address,
437 "origin", protocols[PROTOCOL_LWS_MIRROR].name,
439 if (ping_wsi[n] == NULL) {
440 fprintf(stderr, "client connection %d failed to "
446 libwebsockets_get_peer_addresses(context, ping_wsi[0],
447 libwebsocket_get_socket_fd(ping_wsi[0]),
448 peer_name, sizeof peer_name, ip, sizeof ip);
450 fprintf(stderr, "Websocket PING %s (%s) %d bytes of data.\n",
451 peer_name, ip, size);
454 /* set the ^C handler */
455 sa.sa_sigaction = signal_handler;
456 sa.sa_flags = SA_SIGINFO;
457 sigemptyset(&sa.sa_mask);
458 sigaction(SIGINT, &sa, NULL);
461 gettimeofday(&tv, NULL);
462 started = (tv.tv_sec * 1000000) + tv.tv_usec;
469 gettimeofday(&tv, NULL);
470 l = (tv.tv_sec * 1000000) + tv.tv_usec;
472 /* servers can hang up on us */
479 if (!interrupted_time) {
480 if ((l - oldus) > interval_us) {
481 for (n = 0; n < clients; n++)
482 libwebsocket_callback_on_writable(
483 context, ping_wsi[n]);
488 /* allow time for in-flight pongs to come */
490 if ((l - interrupted_time) > 250000) {
496 n = libwebsocket_service(context, 0);
498 n = libwebsocket_service(context, 1);
503 fprintf(stderr, "\n--- %s websocket ping statistics "
504 "using %d connections ---\n"
505 "%lu packets transmitted, %lu received, "
506 "%lu%% packet loss, time %ldms\n"
507 "rtt min/avg/max = %0.3f/%0.3f/%0.3f ms\n"
508 "payload bandwidth average %0.3f KiBytes/sec\n",
509 peer_name, clients, global_tx_count, global_rx_count,
510 ((global_tx_count - global_rx_count) * 100) / global_tx_count,
511 (l - started) / 1000,
512 ((double)rtt_min) / 1000.0,
513 ((double)rtt_avg / global_rx_count) / 1000.0,
514 ((double)rtt_max) / 1000.0,
515 ((double)global_rx_count * (double)size) /
516 ((double)(l - started) / 1000000.0) / 1024.0);
518 libwebsocket_context_destroy(context);
523 fprintf(stderr, "Usage: libwebsockets-test-ping "
524 "<server address> [--port=<p>] "
525 "[--ssl] [--interval=<float sec>] "
527 "[--protocol=<protocolname>] "
529 "[--replicate=clients>] "
530 "[--version <version>] "
531 "[-d <log bitfield> ]"