Skip to content

Commit

Permalink
Measure wall time in ping tests (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland authored Oct 26, 2023
1 parent 5bd1370 commit 995f082
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 61 deletions.
36 changes: 21 additions & 15 deletions examples/unix/c11/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include "zenoh-pico/system/platform.h"

#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1

#define DEFAULT_PKT_SIZE 8
#define DEFAULT_PING_NB 100
#define DEFAULT_WARMUP_MS 1000

// WARNING: for the sake of this example we are using "internal" structs and functions (starting with "_").
// Synchronisation primitives are planned to be added to the API in the future.
_z_condvar_t cond;
Expand Down Expand Up @@ -50,11 +55,12 @@ int main(int argc, char** argv) {
if (args.help_requested) {
printf(
"\
-n (optional, int, default=4): the number of pings to be attempted\n\
-s (optional, int, default=8): the size of the payload embedded in the ping and repeated by the pong\n\
-w (optional, int, default=0): the warmup time in ms during which pings will be emitted but not measured\n\
-n (optional, int, default=%d): the number of pings to be attempted\n\
-s (optional, int, default=%d): the size of the payload embedded in the ping and repeated by the pong\n\
-w (optional, int, default=%d): the warmup time in ms during which pings will be emitted but not measured\n\
-c (optional, string): the path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
");
",
DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS);
return 1;
}
_z_mutex_init(&mutex);
Expand Down Expand Up @@ -94,23 +100,23 @@ int main(int argc, char** argv) {
_z_mutex_lock(&mutex);
if (args.warmup_ms) {
printf("Warming up for %dms...\n", args.warmup_ms);
clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000;
for (clock_t now = clock(); now < warmup_end; now = clock()) {
z_clock_t warmup_start = z_clock_now();
unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
elapsed_us = z_clock_elapsed_us(&warmup_start);
}
}
clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings);
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t start = clock();
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
clock_t end = clock();
results[i] = end - start;
results[i] = z_clock_elapsed_us(&measure_start);
}
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC;
printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2);
printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2);
}
_z_mutex_unlock(&mutex);
z_free(results);
Expand Down Expand Up @@ -145,17 +151,17 @@ struct args_t parse_args(int argc, char** argv) {
}
}
char* arg = getopt(argc, argv, 's');
unsigned int size = 8;
unsigned int size = DEFAULT_PKT_SIZE;
if (arg) {
size = atoi(arg);
}
arg = getopt(argc, argv, 'n');
unsigned int number_of_pings = 4;
unsigned int number_of_pings = DEFAULT_PING_NB;
if (arg) {
number_of_pings = atoi(arg);
}
arg = getopt(argc, argv, 'w');
unsigned int warmup_ms = 0;
unsigned int warmup_ms = DEFAULT_WARMUP_MS;
if (arg) {
warmup_ms = atoi(arg);
}
Expand Down
36 changes: 21 additions & 15 deletions examples/unix/c99/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
#include "zenoh-pico/system/platform.h"

#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1

#define DEFAULT_PKT_SIZE 8
#define DEFAULT_PING_NB 100
#define DEFAULT_WARMUP_MS 1000

_z_condvar_t cond;
_z_mutex_t mutex;

Expand All @@ -49,11 +54,12 @@ int main(int argc, char** argv) {
if (args.help_requested) {
printf(
"\
-n (optional, int, default=4): the number of pings to be attempted\n\
-s (optional, int, default=8): the size of the payload embedded in the ping and repeated by the pong\n\
-w (optional, int, default=0): the warmup time in ms during which pings will be emitted but not measured\n\
-n (optional, int, default=%d): the number of pings to be attempted\n\
-s (optional, int, default=%d): the size of the payload embedded in the ping and repeated by the pong\n\
-w (optional, int, default=%d): the warmup time in ms during which pings will be emitted but not measured\n\
-c (optional, string): the path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
");
",
DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS);
return 1;
}
_z_mutex_init(&mutex);
Expand Down Expand Up @@ -94,23 +100,23 @@ int main(int argc, char** argv) {
_z_mutex_lock(&mutex);
if (args.warmup_ms) {
printf("Warming up for %dms...\n", args.warmup_ms);
clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000;
for (clock_t now = clock(); now < warmup_end; now = clock()) {
z_clock_t warmup_start = z_clock_now();
unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
elapsed_us = z_clock_elapsed_us(&warmup_start);
}
}
clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings);
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t start = clock();
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
clock_t end = clock();
results[i] = end - start;
results[i] = z_clock_elapsed_us(&measure_start);
}
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC;
printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2);
printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2);
}
_z_mutex_unlock(&mutex);
z_free(results);
Expand Down Expand Up @@ -145,17 +151,17 @@ struct args_t parse_args(int argc, char** argv) {
}
}
char* arg = getopt(argc, argv, 's');
unsigned int size = 8;
unsigned int size = DEFAULT_PKT_SIZE;
if (arg) {
size = atoi(arg);
}
arg = getopt(argc, argv, 'n');
unsigned int number_of_pings = 4;
unsigned int number_of_pings = DEFAULT_PING_NB;
if (arg) {
number_of_pings = atoi(arg);
}
arg = getopt(argc, argv, 'w');
unsigned int warmup_ms = 0;
unsigned int warmup_ms = DEFAULT_WARMUP_MS;
if (arg) {
warmup_ms = atoi(arg);
}
Expand Down
36 changes: 21 additions & 15 deletions examples/windows/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include "zenoh-pico/system/platform.h"

#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1

#define DEFAULT_PKT_SIZE 8
#define DEFAULT_PING_NB 100
#define DEFAULT_WARMUP_MS 1000

_z_condvar_t cond;
_z_mutex_t mutex;

Expand All @@ -48,11 +53,12 @@ int main(int argc, char** argv) {
if (args.help_requested) {
printf(
"\
-n (optional, int, default=4): the number of pings to be attempted\n\
-s (optional, int, default=8): the size of the payload embedded in the ping and repeated by the pong\n\
-w (optional, int, default=0): the warmup time in ms during which pings will be emitted but not measured\n\
-n (optional, int, default=%d): the number of pings to be attempted\n\
-s (optional, int, default=%d): the size of the payload embedded in the ping and repeated by the pong\n\
-w (optional, int, default=%d): the warmup time in ms during which pings will be emitted but not measured\n\
-c (optional, string): the path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
");
",
DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS);
return 1;
}
_z_mutex_init(&mutex);
Expand Down Expand Up @@ -91,23 +97,23 @@ int main(int argc, char** argv) {
_z_mutex_lock(&mutex);
if (args.warmup_ms) {
printf("Warming up for %dms...\n", args.warmup_ms);
clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000;
for (clock_t now = clock(); now < warmup_end; now = clock()) {
z_clock_t warmup_start = z_clock_now();
unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
elapsed_us = z_clock_elapsed_us(&warmup_start);
}
}
clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings);
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t start = clock();
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
clock_t end = clock();
results[i] = end - start;
results[i] = z_clock_elapsed_us(&measure_start);
}
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC;
printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2);
printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2);
}
_z_mutex_unlock(&mutex);
z_free(results);
Expand Down Expand Up @@ -142,17 +148,17 @@ struct args_t parse_args(int argc, char** argv) {
}
}
char* arg = getopt(argc, argv, 's');
unsigned int size = 8;
unsigned int size = DEFAULT_PKT_SIZE;
if (arg) {
size = atoi(arg);
}
arg = getopt(argc, argv, 'n');
unsigned int number_of_pings = 4;
unsigned int number_of_pings = DEFAULT_PING_NB;
if (arg) {
number_of_pings = atoi(arg);
}
arg = getopt(argc, argv, 'w');
unsigned int warmup_ms = 0;
unsigned int warmup_ms = DEFAULT_WARMUP_MS;
if (arg) {
warmup_ms = atoi(arg);
}
Expand Down
8 changes: 4 additions & 4 deletions src/system/arduino/esp32/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,29 @@ int z_sleep_s(size_t time) { return sleep(time); }
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down
8 changes: 4 additions & 4 deletions src/system/espidf/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,29 @@ int z_sleep_s(size_t time) { return sleep(time); }
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down
8 changes: 4 additions & 4 deletions src/system/unix/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,29 @@ int z_sleep_s(size_t time) { return sleep(time); }
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down
8 changes: 4 additions & 4 deletions src/system/zephyr/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,29 @@ int z_sleep_s(size_t time) {
/*------------------ Instant ------------------*/
z_clock_t z_clock_now(void) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
return now;
}

unsigned long z_clock_elapsed_us(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000);
return elapsed;
}

unsigned long z_clock_elapsed_ms(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000);
return elapsed;
}

unsigned long z_clock_elapsed_s(z_clock_t *instant) {
z_clock_t now;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);

unsigned long elapsed = now.tv_sec - instant->tv_sec;
return elapsed;
Expand Down

0 comments on commit 995f082

Please sign in to comment.