Skip to content

Commit

Permalink
feat: add source files for performance test
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Dec 18, 2023
1 parent 5ac84e7 commit b805d31
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
124 changes: 124 additions & 0 deletions tests/z_perf_rx.c
Original file line number Diff line number Diff line change
@@ -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, <[email protected]>
//
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#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);
}
113 changes: 113 additions & 0 deletions tests/z_perf_tx.c
Original file line number Diff line number Diff line change
@@ -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, <[email protected]>
//
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#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);
}

0 comments on commit b805d31

Please sign in to comment.