2 * libwebsockets-test-ping - libwebsockets test floodping
4 * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * The person who associated a work with this deed has dedicated
10 * the work to the public domain by waiving all of his or her rights
11 * to the work worldwide under copyright law, including all related
12 * and neighboring rights, to the extent allowed by law. You can copy,
13 * modify, distribute and perform the work, even for commercial purposes,
14 * all without asking permission.
16 * The test apps are intended to be adapted for use in your code, which
17 * may be proprietary. So unlike the library itself, they are licensed
26 #include <sys/types.h>
28 #include "../lib/libwebsockets.h"
32 #include <sys/socket.h>
34 #include <sys/ioctl.h>
38 #include "gettimeofday.h"
46 * this is specified in the 04 standard, control frames can only have small
47 * payload length styles
49 #define MAX_PING_PAYLOAD 125
50 #define MAX_MIRROR_PAYLOAD 4096
51 #define MAX_PING_CLIENTS 256
52 #define PING_RINGBUFFER_SIZE 256
54 static struct lws *ping_wsi[MAX_PING_CLIENTS];
55 static unsigned int interval_us = 1000000;
56 static unsigned int size = 64;
58 static const char *address;
59 static unsigned char pingbuf[LWS_PRE + MAX_MIRROR_PAYLOAD];
60 static char peer_name[128];
61 static unsigned long started;
62 static int screen_width = 80;
63 static int use_mirror;
64 static unsigned int write_options;
66 static unsigned long rtt_min = 100000000;
67 static unsigned long rtt_max;
68 static unsigned long rtt_avg;
69 static unsigned long global_rx_count;
70 static unsigned long global_tx_count;
71 static int clients = 1;
72 static unsigned long interrupted_time;
75 unsigned long issue_timestamp;
80 struct per_session_data__ping {
83 struct ping ringbuffer[PING_RINGBUFFER_SIZE];
87 unsigned long rx_count;
91 * uses the ping pong protocol features to provide an equivalent for the
92 * ping utility for 04+ websockets
105 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
106 void *user, void *in, size_t len)
108 struct per_session_data__ping *psd = user;
118 case LWS_CALLBACK_CLOSED:
119 fprintf(stderr, "LWS_CALLBACK_CLOSED on %p\n", (void *)wsi);
121 /* remove closed guy */
123 for (n = 0; n < clients; n++)
124 if (ping_wsi[n] == wsi) {
126 while (n < clients) {
127 ping_wsi[n] = ping_wsi[n + 1];
134 case LWS_CALLBACK_CLIENT_ESTABLISHED:
138 psd->ringbuffer_head = 0;
139 psd->ringbuffer_tail = 0;
142 * start the ball rolling,
143 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
146 lws_callback_on_writable(wsi);
149 case LWS_CALLBACK_CLIENT_RECEIVE:
150 case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
151 gettimeofday(&tv, NULL);
152 iv = (tv.tv_sec * 1000000) + tv.tv_usec;
161 l |= ((uint64_t)*p++) << shift;
165 /* find it in the ringbuffer, look backwards from head */
166 n = psd->ringbuffer_head;
169 if (psd->ringbuffer[n].index == l) {
170 psd->ringbuffer[n].seen++;
175 if (n == psd->ringbuffer_tail) {
181 n = PING_RINGBUFFER_SIZE - 1;
189 fprintf(stderr, "%d bytes from %s: req=%ld "
190 "time=(unknown)\n", (int)len, address,
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, (long)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_PRE];
224 /* 64-bit ping index in network byte order */
227 *p++ = (unsigned char)(psd->ping_index >> shift);
231 while ((unsigned int)(p - &pingbuf[LWS_PRE]) < 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 = (unsigned long)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++;
260 size, write_options | LWS_WRITE_BINARY);
264 size, write_options | LWS_WRITE_PING);
269 lwsl_err("Partial write\n");
274 (psd->ping_index - psd->rx_count) < (screen_width - 1))
275 fprintf(stderr, ".");
286 /* list of supported protocols and callbacks */
288 static struct lws_protocols protocols[] = {
291 "lws-mirror-protocol",
293 sizeof (struct per_session_data__ping),
296 NULL, NULL, 0/* end of list */
300 static const struct lws_extension exts[] = {
302 "permessage-deflate",
303 lws_extension_callback_pm_deflate,
304 "permessage-deflate; client_no_context_takeover; client_max_window_bits"
308 lws_extension_callback_pm_deflate,
311 { NULL, NULL, NULL /* terminator */ }
314 static struct option options[] = {
315 { "help", no_argument, NULL, 'h' },
316 { "debug", required_argument, NULL, 'd' },
317 { "port", required_argument, NULL, 'p' },
318 { "ssl", no_argument, NULL, 't' },
319 { "interval", required_argument, NULL, 'i' },
320 { "size", required_argument, NULL, 's' },
321 { "protocol", required_argument, NULL, 'n' },
322 { "flood", no_argument, NULL, 'f' },
323 { "mirror", no_argument, NULL, 'm' },
324 { "replicate", required_argument, NULL, 'r' },
325 { "killmask", no_argument, NULL, 'k' },
326 { "version", required_argument, NULL, 'v' },
332 signal_handler(int sig, siginfo_t *si, void *v)
336 gettimeofday(&tv, NULL);
337 interrupted_time = (tv.tv_sec * 1000000) + tv.tv_usec;
341 int main(int argc, char **argv)
346 struct lws_context *context;
347 char protocol_name[256], ads_port[300];
354 unsigned long oldus = 0;
356 int ietf_version = -1;
357 struct lws_context_creation_info info;
358 struct lws_client_connect_info i;
360 memset(&info, 0, sizeof info);
366 n = getopt_long(argc, argv, "v:kr:hmfts:n:i:p:d:", options, NULL);
371 lws_set_log_level(atoi(optarg), NULL);
377 use_ssl = 2; /* 2 = allow selfsigned */
383 strncpy(protocol_name, optarg, sizeof protocol_name);
384 protocol_name[(sizeof protocol_name) - 1] = '\0';
385 protocols[PROTOCOL_LWS_MIRROR].name = protocol_name;
388 interval_us = (unsigned int)(1000000.0 * atof(optarg));
397 clients = atoi(optarg);
398 if (clients > MAX_PING_CLIENTS || clients < 1) {
399 fprintf(stderr, "Max clients supportd = %d\n",
405 write_options = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
408 ietf_version = atoi(optarg);
417 if (size > MAX_PING_PAYLOAD) {
418 fprintf(stderr, "Max ping opcode payload size %d\n",
423 if (size > MAX_MIRROR_PAYLOAD) {
424 fprintf(stderr, "Max mirror payload size %d\n",
431 if (isatty(STDOUT_FILENO))
432 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
434 screen_width = w.ws_col;
437 info.port = CONTEXT_PORT_NO_LISTEN;
438 info.protocols = protocols;
439 info.extensions = exts;
445 info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
447 context = lws_create_context(&info);
448 if (context == NULL) {
449 fprintf(stderr, "Creating libwebsocket context failed\n");
453 /* create client websockets using dumb increment protocol */
455 address = argv[optind];
456 snprintf(ads_port, sizeof(ads_port), "%s:%u",
457 address, port & 65535);
458 lwsl_notice("Connecting to %s...\n", ads_port);
459 memset(&i, 0, sizeof(i));
463 i.ssl_connection = use_ssl;
467 i.protocol = protocols[PROTOCOL_LWS_MIRROR].name;
468 i.client_exts = exts;
469 i.ietf_version_or_minus_one = ietf_version;
471 for (n = 0; n < clients; n++) {
472 ping_wsi[n] = lws_client_connect_via_info(&i);
473 if (ping_wsi[n] == NULL) {
474 lwsl_err("client %d failed to connect\n", n);
479 lws_get_peer_addresses(ping_wsi[0], lws_get_socket_fd(ping_wsi[0]),
480 peer_name, sizeof peer_name, ip, sizeof ip);
482 lwsl_notice("libwebsockets test server ping - license LGPL2.1+SLE\n");
483 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
484 fprintf(stderr, "Websocket PING %s (%s) %d bytes of data.\n",
485 peer_name, ip, size);
488 /* set the ^C handler */
489 sa.sa_sigaction = signal_handler;
490 sa.sa_flags = SA_SIGINFO;
491 sigemptyset(&sa.sa_mask);
492 sigaction(SIGINT, &sa, NULL);
495 gettimeofday(&tv, NULL);
496 started = (tv.tv_sec * 1000000) + tv.tv_usec;
503 gettimeofday(&tv, NULL);
504 l = (tv.tv_sec * 1000000) + tv.tv_usec;
506 /* servers can hang up on us */
513 if (!interrupted_time) {
514 if ((l - oldus) > interval_us) {
515 for (n = 0; n < clients; n++)
516 lws_callback_on_writable(ping_wsi[n]);
521 /* allow time for in-flight pongs to come */
523 if ((l - interrupted_time) > 250000) {
529 n = lws_service(context, 0);
531 n = lws_service(context, 1);
536 fprintf(stderr, "\n--- %s websocket ping statistics "
537 "using %d connections ---\n"
538 "%lu packets transmitted, %lu received, "
539 "%lu%% packet loss, time %ldms\n"
540 "rtt min/avg/max = %0.3f/%0.3f/%0.3f ms\n"
541 "payload bandwidth average %0.3f KiBytes/sec\n",
542 peer_name, clients, global_tx_count, global_rx_count,
543 ((global_tx_count - global_rx_count) * 100) / global_tx_count,
544 (l - started) / 1000,
545 ((double)rtt_min) / 1000.0,
546 ((double)rtt_avg / global_rx_count) / 1000.0,
547 ((double)rtt_max) / 1000.0,
548 ((double)global_rx_count * (double)size) /
549 ((double)(l - started) / 1000000.0) / 1024.0);
551 lws_context_destroy(context);
556 fprintf(stderr, "Usage: libwebsockets-test-ping "
557 "<server address> [--port=<p>] "
558 "[--ssl] [--interval=<float sec>] "
560 "[--protocol=<protocolname>] "
562 "[--replicate=clients>] "
563 "[--version <version>] "
564 "[-d <log bitfield> ]"