Skip to content

Commit

Permalink
test_utils: add UDP benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Sep 2, 2021
1 parent a59974c commit 73b929b
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sys/Makefile
Expand Up @@ -2,6 +2,9 @@
ifneq (,$(filter asymcute,$(USEMODULE)))
DIRS += net/application_layer/asymcute
endif
ifneq (,$(filter benchmark_udp,$(USEMODULE)))
DIRS += test_utils/benchmark_udp
endif
ifneq (,$(filter bluetil_%,$(USEMODULE)))
DIRS += net/ble/bluetil
endif
Expand Down
6 changes: 6 additions & 0 deletions sys/auto_init/auto_init.c
Expand Up @@ -292,4 +292,10 @@ void auto_init(void)
extern void auto_init_screen(void);
auto_init_screen();
}

if (IS_USED(MODULE_AUTO_INIT_BENCHMARK_UDP)) {
LOG_DEBUG("Auto init UDP benchmark\n");
extern void benchmark_udp_auto_init(void);
benchmark_udp_auto_init();
}
}
113 changes: 113 additions & 0 deletions sys/include/test_utils/benchmark_udp.h
@@ -0,0 +1,113 @@
/*
* Copyright (C) 2021 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @defgroup test_utils_benchmark_udp UDP benchmark
* @ingroup sys
*
* @{
* @file
* @brief Continuously send UDP packets with configurable size and interval.
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/

#ifndef TEST_UTILS_BENCHMARK_UDP_H
#define TEST_UTILS_BENCHMARK_UDP_H

#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Maximum size of a benchmark packet
*/
#ifndef BENCH_PAYLOAD_SIZE_MAX
#define BENCH_PAYLOAD_SIZE_MAX (1024)
#endif

/**
* @brief Default address of the benchmark server
*/
#ifndef BENCH_SERVER_DEFAULT
#define BENCH_SERVER_DEFAULT "fd00:dead:beef::1"
#endif

/**
* @brief Default port of the benchmark server
*/
#ifndef BENCH_PORT_DEFAULT
#define BENCH_PORT_DEFAULT (12345)
#endif

/**
* @brief Flag indicating the benchmark packet is a configuration command.
*/
#define BENCH_FLAG_CMD_PKT (1 << 0)

/**
* @brief Configuration Cookie mask.
*/
#define BENCH_MASK_COOKIE (0xFFFFFF00)

/**
* @brief Benchmark message to the server
* @note Both server and client are assumed to be little endian machines
* @{
*/
typedef struct {
uint32_t flags; /**< must include config cookie */
uint32_t seq_no; /**< number of packets sent sind config update */
uint32_t replies; /**< number of replies received from server */
uint32_t rtt_last; /**< round trip time of the last packet */
uint8_t payload[]; /**< variable length payload */
} benchmark_msg_ping_t;
/** @} */

/**
* @brief Command response from the server
* @note Both server and client are assumed to be little endian machines
* @{
*/
typedef struct {
uint32_t flags; /**< contains new config cookie */
uint32_t delay_us; /**< delay between benchmark messages in µs */
uint16_t payload_len; /**< payload of benchmark messages */
} benchmark_msg_cmd_t;
/** @} */

/**
* @brief This will start the benchmark process.
* Two threads will be spawned, one to send packets to the server
* and one to handle the response.
*
* @param[in] server benchmark server (address or hostname)
* @param[in] port benchmark server port
*
* @return 0 on success
* error otherwise
*/
int benchmark_udp_start(const char *server, uint16_t port);

/**
* @brief Stop the benchmark process
*
* @return true if the benchmark process was stopped
* false if no benchmark process was running
*/
bool benchmark_udp_stop(void);

#ifdef __cplusplus
}
#endif

#endif /* TEST_UTILS_BENCHMARK_UDP_H */
/** @} */
3 changes: 3 additions & 0 deletions sys/shell/commands/Makefile
Expand Up @@ -5,6 +5,9 @@ SRC = shell_commands.c sc_sys.c
ifneq (,$(filter app_metadata,$(USEMODULE)))
SRC += sc_app_metadata.c
endif
ifneq (,$(filter benchmark_udp,$(USEMODULE)))
SRC += sc_benchmark_udp.c
endif
ifneq (,$(filter dfplayer,$(USEMODULE)))
SRC += sc_dfplayer.c
endif
Expand Down
66 changes: 66 additions & 0 deletions sys/shell/commands/sc_benchmark_udp.c
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2021 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for
* more details.
*/

/**
* @ingroup sys_shell_commands
* @{
*
* @file
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test_utils/benchmark_udp.h"
#include "shell.h"

static const char *bench_server = BENCH_SERVER_DEFAULT;
static uint16_t bench_port = BENCH_PORT_DEFAULT;

int _benchmark_udp_handler(int argc, char **argv)
{
if (argc < 2) {
goto usage;
}

if (strcmp(argv[1], "start") == 0) {
if (argc > 2) {
bench_server = argv[2];
}
if (argc > 3) {
bench_port = atoi(argv[3]);
}
return benchmark_udp_start(bench_server, bench_port);
}
if (strcmp(argv[1], "config") == 0) {
if (argc < 3) {
printf("server: %s\n", bench_server);
printf("port : %u\n", bench_port);
} else {
bench_server = argv[2];
}
if (argc > 3) {
bench_port = atoi(argv[3]);
}
}
if (strcmp(argv[1], "stop") == 0) {
if (benchmark_udp_stop()) {
puts("benchmark process stopped");
} else {
puts("no benchmark was running");
}
return 0;
}

usage:
printf("usage: %s [start|stop|config] <server> <port>\n", argv[0]);
return -1;
}
/** @} */
7 changes: 7 additions & 0 deletions sys/shell/commands/shell_commands.c
Expand Up @@ -163,6 +163,10 @@ extern int _vfs_handler(int argc, char **argv);
extern int _ls_handler(int argc, char **argv);
#endif

#ifdef MODULE_BENCHMARK_UDP
extern int _benchmark_udp_handler(int argc, char **argv);
#endif

#ifdef MODULE_CONN_CAN
extern int _can_handler(int argc, char **argv);
#endif
Expand Down Expand Up @@ -209,6 +213,9 @@ const shell_command_t _shell_command_list[] = {
#ifdef MODULE_USB_BOARD_RESET
{"bootloader", "Reboot to bootloader", _bootloader_handler},
#endif
#ifdef MODULE_BENCHMARK_UDP
{"bench_udp", "UDP benchmark", _benchmark_udp_handler},
#endif
#ifdef MODULE_CONFIG
{"id", "Gets or sets the node's id.", _id_handler},
#endif
Expand Down
5 changes: 5 additions & 0 deletions sys/test_utils/Makefile.dep
Expand Up @@ -4,3 +4,8 @@ endif
ifneq (,$(filter test_utils_result_output_%,$(USEMODULE)))
USEMODULE += fmt
endif
ifneq (,$(filter benchmark_udp,$(USEMODULE)))
USEMODULE += netutils
USEMODULE += sema_inv
USEMODULE += sock_udp
endif
1 change: 1 addition & 0 deletions sys/test_utils/benchmark_udp/Makefile
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

0 comments on commit 73b929b

Please sign in to comment.