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>
32 #include <sys/socket.h>
35 #include <sys/ioctl.h>
37 #include "../lib/libwebsockets.h"
41 * this is specified in the 04 standard, control frames can only have small
42 * payload length styles
44 #define MAX_PING_PAYLOAD 125
45 #define MAX_MIRROR_PAYLOAD 4096
46 #define MAX_PING_CLIENTS 256
47 #define PING_RINGBUFFER_SIZE 256
49 static struct libwebsocket *ping_wsi[MAX_PING_CLIENTS];
50 static unsigned int interval_us = 1000000;
51 static unsigned int size = 64;
53 static const char *address;
54 static unsigned char pingbuf[LWS_SEND_BUFFER_PRE_PADDING + MAX_MIRROR_PAYLOAD +
55 LWS_SEND_BUFFER_POST_PADDING];
56 static char peer_name[128];
57 static unsigned long started;
58 static int screen_width = 80;
59 static int use_mirror;
60 static unsigned int write_options;
62 static unsigned long rtt_min = 100000000;
63 static unsigned long rtt_max;
64 static unsigned long rtt_avg;
65 static unsigned long global_rx_count;
66 static unsigned long global_tx_count;
67 static int clients = 1;
68 static unsigned long interrupted_time;
71 unsigned long issue_timestamp;
76 struct per_session_data__ping {
77 unsigned long ping_index;
79 struct ping ringbuffer[PING_RINGBUFFER_SIZE];
83 unsigned long rx_count;
87 * uses the ping pong protocol features to provide an equivalent for the
88 * ping utility for 04+ websockets
101 callback_lws_mirror(struct libwebsocket_context * this,
102 struct libwebsocket *wsi,
103 enum libwebsocket_callback_reasons reason,
104 void *user, void *in, size_t len)
113 struct per_session_data__ping *psd = user;
116 case LWS_CALLBACK_CLOSED:
118 fprintf(stderr, "LWS_CALLBACK_CLOSED on %p\n", (void *)wsi);
120 /* remove closed guy */
122 for (n = 0; n < clients; n++)
123 if (ping_wsi[n] == wsi) {
125 while (n < clients) {
126 ping_wsi[n] = ping_wsi[n + 1];
133 case LWS_CALLBACK_CLIENT_ESTABLISHED:
137 psd->ringbuffer_head = 0;
138 psd->ringbuffer_tail = 0;
141 * start the ball rolling,
142 * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
145 libwebsocket_callback_on_writable(this, wsi);
148 case LWS_CALLBACK_CLIENT_RECEIVE:
149 case LWS_CALLBACK_CLIENT_RECEIVE_PONG:
150 gettimeofday(&tv, NULL);
151 iv = (tv.tv_sec * 1000000) + tv.tv_usec;
160 l |= (*p++) << shift;
164 /* find it in the ringbuffer, look backwards from head */
165 n = psd->ringbuffer_head;
168 if (psd->ringbuffer[n].index == l) {
169 psd->ringbuffer[n].seen++;
174 if (n == psd->ringbuffer_tail) {
180 n = PING_RINGBUFFER_SIZE - 1;
188 fprintf(stderr, "%d bytes from %s: req=%ld "
189 "time=(unknown)\n", (int)len, address, l);
191 fprintf(stderr, "\b \b");
196 if (psd->ringbuffer[n].seen > 1)
197 fprintf(stderr, "DUP! ");
199 if ((iv - psd->ringbuffer[n].issue_timestamp) < rtt_min)
200 rtt_min = iv - psd->ringbuffer[n].issue_timestamp;
202 if ((iv - psd->ringbuffer[n].issue_timestamp) > rtt_max)
203 rtt_max = iv - psd->ringbuffer[n].issue_timestamp;
205 rtt_avg += iv - psd->ringbuffer[n].issue_timestamp;
209 fprintf(stderr, "%d bytes from %s: req=%ld "
210 "time=%lu.%lums\n", (int)len, address, l,
211 (iv - psd->ringbuffer[n].issue_timestamp) / 1000,
212 ((iv - psd->ringbuffer[n].issue_timestamp) / 100) % 10);
214 fprintf(stderr, "\b \b");
217 case LWS_CALLBACK_CLIENT_WRITEABLE:
220 p = &pingbuf[LWS_SEND_BUFFER_PRE_PADDING];
222 /* 64-bit ping index in network byte order */
225 *p++ = psd->ping_index >> shift;
229 gettimeofday(&tv, NULL);
231 psd->ringbuffer[psd->ringbuffer_head].issue_timestamp =
232 (tv.tv_sec * 1000000) + tv.tv_usec;
233 psd->ringbuffer[psd->ringbuffer_head].index = psd->ping_index++;
234 psd->ringbuffer[psd->ringbuffer_head].seen = 0;
236 if (psd->ringbuffer_head == PING_RINGBUFFER_SIZE - 1)
237 psd->ringbuffer_head = 0;
239 psd->ringbuffer_head++;
241 /* snip any re-used tail so we keep to the ring length */
243 if (psd->ringbuffer_tail == psd->ringbuffer_head) {
244 if (psd->ringbuffer_tail == PING_RINGBUFFER_SIZE - 1)
245 psd->ringbuffer_tail = 0;
247 psd->ringbuffer_tail++;
253 libwebsocket_write(wsi,
254 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
255 size, write_options | LWS_WRITE_BINARY);
257 libwebsocket_write(wsi,
258 &pingbuf[LWS_SEND_BUFFER_PRE_PADDING],
259 size, write_options | LWS_WRITE_PING);
262 (psd->ping_index - psd->rx_count) < (screen_width - 1))
263 fprintf(stderr, ".");
274 /* list of supported protocols and callbacks */
276 static struct libwebsocket_protocols protocols[] = {
278 [PROTOCOL_LWS_MIRROR] = {
279 .name = "lws-mirror-protocol",
280 .callback = callback_lws_mirror,
281 .per_session_data_size = sizeof (struct per_session_data__ping),
283 [DEMO_PROTOCOL_COUNT] = { /* end of list */
288 static struct option options[] = {
289 { "help", no_argument, NULL, 'h' },
290 { "debug", required_argument, NULL, 'd' },
291 { "port", required_argument, NULL, 'p' },
292 { "ssl", no_argument, NULL, 't' },
293 { "interval", required_argument, NULL, 'i' },
294 { "size", required_argument, NULL, 's' },
295 { "protocol", required_argument, NULL, 'n' },
296 { "flood", no_argument, NULL, 'f' },
297 { "mirror", no_argument, NULL, 'm' },
298 { "replicate", required_argument, NULL, 'r' },
299 { "killmask", no_argument, NULL, 'k' },
300 { "version", required_argument, NULL, 'v' },
306 signal_handler(int sig, siginfo_t *si, void *v)
310 gettimeofday(&tv, NULL);
311 interrupted_time = (tv.tv_sec * 1000000) + tv.tv_usec;
315 int main(int argc, char **argv)
320 struct libwebsocket_context *context;
321 char protocol_name[256];
326 unsigned long oldus = 0;
328 int ietf_version = -1;
337 n = getopt_long(argc, argv, "v:kr:hmfts:n:i:p:d:", options, NULL);
342 lws_set_log_level(atoi(optarg), NULL);
348 use_ssl = 2; /* 2 = allow selfsigned */
354 strncpy(protocol_name, optarg, sizeof protocol_name);
355 protocol_name[(sizeof protocol_name) - 1] = '\0';
356 protocols[PROTOCOL_LWS_MIRROR].name = protocol_name;
359 interval_us = 1000000.0 * atof(optarg);
368 clients = atoi(optarg);
369 if (clients > MAX_PING_CLIENTS || clients < 1) {
370 fprintf(stderr, "Max clients supportd = %d\n",
376 write_options = LWS_WRITE_CLIENT_IGNORE_XOR_MASK;
379 ietf_version = atoi(optarg);
388 if (size > MAX_PING_PAYLOAD) {
389 fprintf(stderr, "Max ping opcode payload size %d\n",
394 if (size > MAX_MIRROR_PAYLOAD) {
395 fprintf(stderr, "Max mirror payload size %d\n",
402 if (isatty(STDOUT_FILENO))
403 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
405 screen_width = w.ws_col;
407 context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
409 libwebsocket_internal_extensions,
410 NULL, NULL, NULL, -1, -1, 0, NULL);
411 if (context == NULL) {
412 fprintf(stderr, "Creating libwebsocket context failed\n");
416 /* create client websockets using dumb increment protocol */
418 for (n = 0; n < clients; n++) {
419 ping_wsi[n] = libwebsocket_client_connect(context, address,
420 port, use_ssl, "/", address,
421 "origin", protocols[PROTOCOL_LWS_MIRROR].name,
423 if (ping_wsi[n] == NULL) {
424 fprintf(stderr, "client connnection %d failed to "
430 libwebsockets_get_peer_addresses(
431 libwebsocket_get_socket_fd(ping_wsi[0]),
432 peer_name, sizeof peer_name, ip, sizeof ip);
434 fprintf(stderr, "Websocket PING %s (%s) %d bytes of data.\n",
435 peer_name, ip, size);
437 /* set the ^C handler */
439 sa.sa_sigaction = signal_handler;
440 sa.sa_flags = SA_SIGINFO;
441 sigemptyset(&sa.sa_mask);
442 sigaction(SIGINT, &sa, NULL);
444 gettimeofday(&tv, NULL);
445 started = (tv.tv_sec * 1000000) + tv.tv_usec;
452 gettimeofday(&tv, NULL);
453 l = (tv.tv_sec * 1000000) + tv.tv_usec;
455 /* servers can hang up on us */
462 if (!interrupted_time) {
463 if ((l - oldus) > interval_us) {
464 for (n = 0; n < clients; n++)
465 libwebsocket_callback_on_writable(
466 context, ping_wsi[n]);
471 /* allow time for in-flight pongs to come */
473 if ((l - interrupted_time) > 250000) {
479 n = libwebsocket_service(context, 0);
481 n = libwebsocket_service(context, 1);
486 fprintf(stderr, "\n--- %s websocket ping statistics "
487 "using %d connections ---\n"
488 "%lu packets transmitted, %lu received, "
489 "%lu%% packet loss, time %ldms\n"
490 "rtt min/avg/max = %0.3f/%0.3f/%0.3f ms\n"
491 "payload bandwidth average %0.3f KiBytes/sec\n",
492 peer_name, clients, global_tx_count, global_rx_count,
493 ((global_tx_count - global_rx_count) * 100) / global_tx_count,
494 (l - started) / 1000,
495 ((double)rtt_min) / 1000.0,
496 ((double)rtt_avg / global_rx_count) / 1000.0,
497 ((double)rtt_max) / 1000.0,
498 ((double)global_rx_count * (double)size) /
499 ((double)(l - started) / 1000000.0) / 1024.0);
501 libwebsocket_context_destroy(context);
506 fprintf(stderr, "Usage: libwebsockets-test-ping "
507 "<server address> [--port=<p>] "
508 "[--ssl] [--interval=<float sec>] "
510 "[--protocol=<protocolname>] "
512 "[--replicate=clients>] "
513 "[--version <version>] "
514 "[-d <log bitfield> ]"