From b805d319dd8f1f8a426a2903b1286854a39938c3 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Mon, 18 Dec 2023 11:22:54 +0100 Subject: [PATCH] feat: add source files for performance test --- tests/z_perf_rx.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++ tests/z_perf_tx.c | 113 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 tests/z_perf_rx.c create mode 100644 tests/z_perf_tx.c diff --git a/tests/z_perf_rx.c b/tests/z_perf_rx.c new file mode 100644 index 000000000..b862d5ecf --- /dev/null +++ b/tests/z_perf_rx.c @@ -0,0 +1,124 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// +#include +#include +#include +#include +#include + +#include "zenoh-pico.h" + +typedef struct { + volatile unsigned long count; + size_t curr_len; + z_clock_t start; +} z_stats_t; + +static z_stats_t test_stats; +static volatile bool test_end; + +void z_stats_stop(z_stats_t *stats) { + // Ignore default value + if (stats->curr_len == 0) { + return; + } + // Print values + unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->start); + printf("End test for pkt len: %lu, msg nb: %lu, time ms: %lu\n", stats->curr_len, stats->count, elapsed_ms); + stats->count = 0; +} + +void on_sample(const z_sample_t *sample, void *context) { + z_stats_t *stats = (z_stats_t *)context; + + if (stats->curr_len != sample->payload.len) { + // End previous measurement + z_stats_stop(stats); + // Check for end packet + stats->curr_len = sample->payload.len; + if (sample->payload.len == 1) { + test_end = true; + return; + } + // Start new measurement + printf("Starting test for pkt len: %lu\n", stats->curr_len); + stats->start = z_clock_now(); + } + stats->count++; +} + +int main(int argc, char **argv) { + char *keyexpr = "test/thr"; + const char *mode = "client"; + char *llocator = NULL; + char *clocator = NULL; + + // Get args + int opt; + while ((opt = getopt(argc, argv, "m:l:e:")) != -1) { + switch (opt) { + case 'm': + mode = optarg; + break; + case 'l': + llocator = optarg; + break; + case 'e': + clocator = optarg; + break; + default: + return -1; + } + } + // Set config + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (llocator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + } + if (clocator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + } + // Open session + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + exit(-1); + } + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + exit(-1); + } + // Declare Subscriber/resource + z_owned_closure_sample_t callback = z_closure(on_sample, NULL, (void *)&test_stats); + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); + if (!z_check(sub)) { + printf("Unable to create subscriber.\n"); + exit(-1); + } + // Listen until stopped + printf("Start listening.\n"); + while (!test_end) { + } + // Wait for everything to settle + printf("End of test\n"); + z_sleep_s(1); + // Clean up + z_undeclare_subscriber(z_move(sub)); + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + z_close(z_move(s)); + exit(0); +} diff --git a/tests/z_perf_tx.c b/tests/z_perf_tx.c new file mode 100644 index 000000000..a404b1ba0 --- /dev/null +++ b/tests/z_perf_tx.c @@ -0,0 +1,113 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// +#include +#include +#include +#include +#include + +#include "zenoh-pico.h" + +#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) +#define TEST_DURATION_US 10000000 + +int send_packets(size_t pkt_len, z_owned_publisher_t *pub, uint8_t *value) { + z_clock_t test_start = z_clock_now(); + unsigned long elapsed_us = 0; + while (elapsed_us < TEST_DURATION_US) { + if (z_publisher_put(z_loan(*pub), (const uint8_t *)value, pkt_len, NULL) != 0) { + printf("Put failed for pkt len: %lu\n", pkt_len); + return -1; + } + elapsed_us = z_clock_elapsed_us(&test_start); + } + return 0; +} + +int main(int argc, char **argv) { + size_t len_array[] = {1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, + 2048, 1024, 512, 256, 128, 64, 32, 16, 8}; // Biggest value first + uint8_t *value = (uint8_t *)malloc(len_array[0]); + memset(value, 1, len_array[0]); + char *keyexpr = "test/thr"; + const char *mode = "client"; + char *llocator = NULL; + char *clocator = NULL; + + // Get args + int opt; + while ((opt = getopt(argc, argv, "m:l:e:")) != -1) { + switch (opt) { + case 'm': + mode = optarg; + break; + case 'l': + llocator = optarg; + break; + case 'e': + clocator = optarg; + break; + default: + return -1; + } + } + // Set config + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (llocator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); + } + if (clocator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + } + // Open session + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + exit(-1); + } + // Start read and lease tasks for zenoh-pico + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { + printf("Unable to start read and lease tasks"); + exit(-1); + } + // Declare publisher + z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(keyexpr), NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + exit(-1); + } + // Wait for joins + if (strcmp(mode, "peer") == 0) { + printf("Waiting for JOIN messages\n"); + z_sleep_s(3); + } + // Send packets + for (size_t i = 0; i < ARRAY_SIZE(len_array); i++) { + printf("Start sending pkt len: %lu\n", len_array[i]); + if (send_packets(len_array[i], &pub, value) != 0) { + break; + } + } + // Send end packet + printf("Sending end pkt\n"); + z_publisher_put(z_loan(pub), (const uint8_t *)value, 1, NULL); + // Clean up + z_undeclare_publisher(z_move(pub)); + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + z_close(z_move(s)); + free(value); + exit(0); +}