From: Laurent MAZET Date: Fri, 3 Oct 2025 15:40:21 +0000 (+0200) Subject: add test for udp latency X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=bea91172a914b0b135360d2b73401c60f738e9c1;p=benchmarks.git add test for udp latency --- diff --git a/udp.c b/udp.c deleted file mode 100644 index a66511a..0000000 --- a/udp.c +++ /dev/null @@ -1,216 +0,0 @@ -/* depend: */ -/* cflags: */ -/* linker: mtime.o test.o stat.o -lm -lrt */ - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include - -#include "mtime.h" -#include "test.h" - -/* global variables */ - -dts_t *deltas = NULL; -int nb_measurements = 0; - -char *local_host = "localhost"; -int local_port = 1024; -char *remote_host = "localhost"; -int remote_port = 1025; -int mode = 0; - -char *message = "udp socket"; - -void _usage_ext (FILE *fd) -{ - fprintf (fd, "... lhost lport rhost rport [mode]\n"); - fprintf (fd, " lhost: local host name (%s)\n", local_host); - fprintf (fd, " lport: local port number (%d)\n", local_port); - fprintf (fd, " rhost: remote host name (%s)\n", remote_host); - fprintf (fd, " rport: remote port number (%d)\n", remote_port); - fprintf (fd, " mode: 0 for ping-pong, 1 for ping, 2 for pong (%d)\n", mode); -} -void (*usage_ext) (FILE *) = _usage_ext; - -int _parse_arg_ext (char *arg) -{ - static int narg = 0; - - int rc = 0; - switch (narg) { - case 0: local_host= arg; break; - case 1: local_port = atoi (arg); if (local_port < 0) rc = 1; break; - case 2: remote_host = arg; break; - case 3: remote_port = atoi (arg); if (remote_port < 0) rc = 1; break; - case 4: mode = atoi (arg); break; - default: rc = 1; - } - narg++; - - return rc; -} -int (*parse_arg_ext) (char *) = _parse_arg_ext; - -#define MAXBUF 1024 - -int ping (int local_port, int remote_addr, int remote_port) -{ - - int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (sock == -1) { - fprintf (stderr, "ping error: socket\n"); - return 1; - } - - struct sockaddr_in local = { 0 }; - local.sin_family = AF_INET; - local.sin_port = htons (local_port); - local.sin_addr.s_addr = htonl (INADDR_ANY); - - if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { - fprintf (stderr, "ping error: bind\n"); - return 1; - } - - struct sockaddr_in remote = { 0 }; - remote.sin_family = AF_INET; - remote.sin_port = htons (remote_port); - remote.sin_addr.s_addr = htonl (remote_addr); - - printf ("Sending ping...\n"); - for (int i = 0; i < nb_measurements; i++) { - - char buffer[MAXBUF] = { 0 }; - sprintf (buffer, "ping %d", i); - - ts_t ts1; - sys_timestamp (&ts1); - - if (sendto (sock, buffer, strlen (buffer) + 1, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { - fprintf (stderr, "ping error: sendto (%d)\n", i); - return 1; - } - //printf ("sendto '%s'\n", buffer); - - int len = recv (sock, buffer, MAXBUF, 0); - if (len == -1) { - fprintf (stderr, "ping error: recv_from (%d)\n", i); - return 1; - } - //printf ("recv_from '%s'\n", buffer); - - ts_t ts2; - sys_timestamp (&ts2); - deltas[i] = diff_timestamp (&ts2, &ts1); - } - - close (sock); - - return 0; -} - -int pong (int local_port, int remote_addr, int remote_port) -{ - - int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (sock == -1) { - fprintf (stderr, "pong error: socket\n"); - return 1; - } - - struct sockaddr_in local = { 0 }; - local.sin_family = AF_INET; - local.sin_port = htons (local_port); - local.sin_addr.s_addr = htonl (INADDR_ANY); - - if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { - fprintf (stderr, "pong error: bind\n"); - return 1; - } - - struct sockaddr_in remote = { 0 }; - remote.sin_family = AF_INET; - remote.sin_port = htons (remote_port); - remote.sin_addr.s_addr = htonl (remote_addr); - - printf ("Sending pong...\n"); - for (int i = 0; i < nb_measurements; i++) { - - char buffer[MAXBUF] = { 0 }; - - int len = recv (sock, buffer, MAXBUF, 0); - if (len == -1) { - fprintf (stderr, "pong error: recv_from (%d)\n", i); - return 1; - } - //printf ("recv_from '%s'\n", buffer); - - if (sendto (sock, buffer, len, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { - fprintf (stderr, "pong error: sendto (%d)\n", i); - return 1; - } - //printf ("sendto '%s'\n", buffer); - } - - close (sock); - - return 0; -} - -int get_ip (char *hostname) { - struct hostent *he = gethostbyname (hostname); - if (he == NULL) { - fprintf (stderr, "error: gethostbyname (%s)\n", hostname); - return -1; - } - struct in_addr **addr_list = (struct in_addr **) he->h_addr_list; - return ntohl (addr_list[0]->s_addr); -} - -int test (dts_t *buffer, int nb) -{ - - /* set global variables */ - - deltas = buffer; - nb_measurements = nb; - - /* udp socket test */ - int local_ip = get_ip (local_host); - if (local_ip == -1) { - return 1; - } - int remote_ip = get_ip (remote_host); - if (remote_ip == -1) { - return 1; - } - - pid_t pid = (mode == 0 ) ? fork () : -2; - if (pid == -1) { - fprintf (stderr, "error: fork\n"); - return 1; - } else if ((pid == 0) || (mode == 2)) { - int rc = pong (remote_port, local_ip, local_port); - exit (rc); - } - - int rc = ping (local_port, remote_ip, remote_port); - - if ((pid > 0) && (kill (pid, SIGTERM) == 0)) { - int wstatus; - if (waitpid (pid, &wstatus, WUNTRACED | WCONTINUED) == -1) { - fprintf (stderr, "error: waitpid\n"); - } - } - - return rc; -} diff --git a/udp_lat.c b/udp_lat.c new file mode 100644 index 0000000..2d7c744 --- /dev/null +++ b/udp_lat.c @@ -0,0 +1,189 @@ +/* depend: */ +/* cflags: */ +/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */ + +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include "mtime.h" +#include "test.h" + +/* global variables */ + +dts_t *deltas = NULL; +int nb_measurements = 0; + +int port = 1024; + +char *message = "UDP socket latency"; + +void _usage_ext (FILE *fd) +{ + fprintf (fd, "... port\n"); + fprintf (fd, " port: port number (%d)\n", port); +} +void (*usage_ext) (FILE *) = _usage_ext; + +int _parse_arg_ext (char *arg) +{ + static int narg = 0; + + int rc = 0; + switch (narg) { + case 0: port = atoi (arg); if (port < 0) rc = 1; break; + default: rc = 1; + } + narg++; + + return rc; +} +int (*parse_arg_ext) (char *) = _parse_arg_ext; + +#define MAXBUF 1024 +#define TIMER 1000 + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +ts_t ts1, ts2; +int rc = 0; +int localhost_ip = 0x7f000001; + +void *ping (__attribute__((unused)) void *arg) +{ + + int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock == -1) { + fprintf (stderr, "ping error: socket\n"); + rc = 1; + pthread_exit (NULL); + } +/* + struct sockaddr_in local = { 0 }; + local.sin_family = AF_INET; + local.sin_port = htons (port); + local.sin_addr.s_addr = htonl (INADDR_ANY); + + if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { + fprintf (stderr, "ping error: bind\n"); + rc = 1; + pthread_exit (NULL); + } +*/ + struct sockaddr_in remote = { 0 }; + remote.sin_family = AF_INET; + remote.sin_port = htons (port); + remote.sin_addr.s_addr = htonl (localhost_ip); + + printf ("Sending ping...\n"); + + pthread_mutex_unlock (&mutex); + usleep (TIMER); + + for (int i = 0; i < nb_measurements; i++) { + + char buffer[MAXBUF] = { 0 }; + sprintf (buffer, "ping %d", i); + + pthread_mutex_lock (&mutex); + pthread_mutex_unlock (&mutex); + + sys_timestamp (&ts1); + if (sendto (sock, buffer, strlen (buffer) + 1, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { + fprintf (stderr, "ping error: sendto (%d)\n", i); + rc = 1; + pthread_exit (NULL); + } + + usleep (TIMER); + } + + close (sock); + + pthread_exit (NULL); +} + +void *pong (__attribute__((unused)) void *arg) +{ + + int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock == -1) { + fprintf (stderr, "pong error: socket\n"); + rc = 1; + pthread_exit (NULL); + } + + struct sockaddr_in local = { 0 }; + local.sin_family = AF_INET; + local.sin_port = htons (port); + local.sin_addr.s_addr = htonl (INADDR_ANY); + + if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { + fprintf (stderr, "pong error: bind\n"); + rc = 1; + pthread_exit (NULL); + } + + printf ("Sending pong...\n"); + + for (int i = 0; i < nb_measurements; i++) { + + pthread_mutex_unlock (&mutex); + + char buffer[MAXBUF] = { 0 }; + if (recv (sock, buffer, MAXBUF, 0) == -1) { + fprintf (stderr, "pong error: recv_from (%d)\n", i); + rc = 1; + pthread_exit (NULL); + } + sys_timestamp (&ts2); + deltas[i] = diff_timestamp (&ts2, &ts1); + + pthread_mutex_lock (&mutex); + usleep (TIMER); + } + + close (sock); + + pthread_exit (NULL); +} + +int test (dts_t *buffer, int nb) +{ + + /* set global variables */ + + deltas = buffer; + nb_measurements = nb; + + /* udp socket test */ + + pthread_mutex_lock (&mutex); + + pthread_t tid1; + if (pthread_create (&tid1, NULL, ping, NULL) != 0) { + fprintf (stderr, "error on pthread_create\n"); + return 1; + } + + pthread_mutex_lock (&mutex); + + pthread_t tid2; + if (pthread_create (&tid2, NULL, pong, NULL) != 0) { + fprintf (stderr, "error on pthread_create\n"); + return 1; + } + + pthread_join (tid1, NULL); + pthread_join (tid2, NULL); + + pthread_mutex_destroy (&mutex); + + return rc; +} diff --git a/udp_rtt.c b/udp_rtt.c new file mode 100644 index 0000000..de97b85 --- /dev/null +++ b/udp_rtt.c @@ -0,0 +1,216 @@ +/* depend: */ +/* cflags: */ +/* linker: mtime.o test.o stat.o -lm -lrt */ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include "mtime.h" +#include "test.h" + +/* global variables */ + +dts_t *deltas = NULL; +int nb_measurements = 0; + +char *local_host = "localhost"; +int local_port = 1024; +char *remote_host = "localhost"; +int remote_port = 1025; +int mode = 0; + +char *message = "UDP socket RTT"; + +void _usage_ext (FILE *fd) +{ + fprintf (fd, "... lhost lport rhost rport [mode]\n"); + fprintf (fd, " lhost: local host name (%s)\n", local_host); + fprintf (fd, " lport: local port number (%d)\n", local_port); + fprintf (fd, " rhost: remote host name (%s)\n", remote_host); + fprintf (fd, " rport: remote port number (%d)\n", remote_port); + fprintf (fd, " mode: 0 for ping-pong, 1 for ping, 2 for pong (%d)\n", mode); +} +void (*usage_ext) (FILE *) = _usage_ext; + +int _parse_arg_ext (char *arg) +{ + static int narg = 0; + + int rc = 0; + switch (narg) { + case 0: local_host= arg; break; + case 1: local_port = atoi (arg); if (local_port < 0) rc = 1; break; + case 2: remote_host = arg; break; + case 3: remote_port = atoi (arg); if (remote_port < 0) rc = 1; break; + case 4: mode = atoi (arg); break; + default: rc = 1; + } + narg++; + + return rc; +} +int (*parse_arg_ext) (char *) = _parse_arg_ext; + +#define MAXBUF 1024 + +int ping (int local_port, int remote_addr, int remote_port) +{ + + int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock == -1) { + fprintf (stderr, "ping error: socket\n"); + return 1; + } + + struct sockaddr_in local = { 0 }; + local.sin_family = AF_INET; + local.sin_port = htons (local_port); + local.sin_addr.s_addr = htonl (INADDR_ANY); + + if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { + fprintf (stderr, "ping error: bind\n"); + return 1; + } + + struct sockaddr_in remote = { 0 }; + remote.sin_family = AF_INET; + remote.sin_port = htons (remote_port); + remote.sin_addr.s_addr = htonl (remote_addr); + + printf ("Sending ping...\n"); + for (int i = 0; i < nb_measurements; i++) { + + char buffer[MAXBUF] = { 0 }; + sprintf (buffer, "ping %d", i); + + ts_t ts1; + sys_timestamp (&ts1); + + if (sendto (sock, buffer, strlen (buffer) + 1, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { + fprintf (stderr, "ping error: sendto (%d)\n", i); + return 1; + } + //printf ("sendto '%s'\n", buffer); + + int len = recv (sock, buffer, MAXBUF, 0); + if (len == -1) { + fprintf (stderr, "ping error: recv_from (%d)\n", i); + return 1; + } + //printf ("recv_from '%s'\n", buffer); + + ts_t ts2; + sys_timestamp (&ts2); + deltas[i] = diff_timestamp (&ts2, &ts1); + } + + close (sock); + + return 0; +} + +int pong (int local_port, int remote_addr, int remote_port) +{ + + int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock == -1) { + fprintf (stderr, "pong error: socket\n"); + return 1; + } + + struct sockaddr_in local = { 0 }; + local.sin_family = AF_INET; + local.sin_port = htons (local_port); + local.sin_addr.s_addr = htonl (INADDR_ANY); + + if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { + fprintf (stderr, "pong error: bind\n"); + return 1; + } + + struct sockaddr_in remote = { 0 }; + remote.sin_family = AF_INET; + remote.sin_port = htons (remote_port); + remote.sin_addr.s_addr = htonl (remote_addr); + + printf ("Sending pong...\n"); + for (int i = 0; i < nb_measurements; i++) { + + char buffer[MAXBUF] = { 0 }; + + int len = recv (sock, buffer, MAXBUF, 0); + if (len == -1) { + fprintf (stderr, "pong error: recv_from (%d)\n", i); + return 1; + } + //printf ("recv_from '%s'\n", buffer); + + if (sendto (sock, buffer, len, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { + fprintf (stderr, "pong error: sendto (%d)\n", i); + return 1; + } + //printf ("sendto '%s'\n", buffer); + } + + close (sock); + + return 0; +} + +int get_ip (char *hostname) { + struct hostent *he = gethostbyname (hostname); + if (he == NULL) { + fprintf (stderr, "error: gethostbyname (%s)\n", hostname); + return -1; + } + struct in_addr **addr_list = (struct in_addr **) he->h_addr_list; + return ntohl (addr_list[0]->s_addr); +} + +int test (dts_t *buffer, int nb) +{ + + /* set global variables */ + + deltas = buffer; + nb_measurements = nb; + + /* udp socket test */ + int local_ip = get_ip (local_host); + if (local_ip == -1) { + return 1; + } + int remote_ip = get_ip (remote_host); + if (remote_ip == -1) { + return 1; + } + + pid_t pid = (mode == 0 ) ? fork () : -2; + if (pid == -1) { + fprintf (stderr, "error: fork\n"); + return 1; + } else if ((pid == 0) || (mode == 2)) { + int rc = pong (remote_port, local_ip, local_port); + exit (rc); + } + + int rc = ping (local_port, remote_ip, remote_port); + + if ((pid > 0) && (kill (pid, SIGTERM) == 0)) { + int wstatus; + if (waitpid (pid, &wstatus, WUNTRACED | WCONTINUED) == -1) { + fprintf (stderr, "error: waitpid\n"); + } + } + + return rc; +}