Skip to content

Commit

Permalink
feat: add throughput examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Dec 19, 2023
1 parent 2a371f0 commit efc5873
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
75 changes: 75 additions & 0 deletions examples/unix/c11/z_pub_thr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// 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>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdio.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.6 rule Note

MISRA 21.6 rule
#include <stdlib.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "zenoh-pico.h"

#if Z_FEATURE_PUBLICATION == 1

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 2009 with no text in the supplied rule-texts-file Warning

misra violation 2009 with no text in the supplied rule-texts-file
int main(int argc, char **argv) {
if (argc < 2) {
printf("USAGE:\n\tz_pub_thr <payload-size> [<zenoh-locator>]\n\n");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
exit(-1);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.8 rule Note

MISRA 21.8 rule
}
char *keyexpr = "test/thr";

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
size_t len = atoi(argv[1]);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.7 rule Note

MISRA 21.7 rule
uint8_t *value = (uint8_t *)malloc(len);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.3 rule Note

MISRA 21.3 rule
memset(value, 1, len);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule

// Set config
z_owned_config_t config = z_config_default();
if (argc > 2) {
if (zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[2])) < 0) {
printf("Couldn't insert locator in config: %s\n", argv[2]);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
exit(-1);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.8 rule Note

MISRA 21.8 rule
}
}
// Open session
z_owned_session_t s = z_open(z_move(config));
if (!z_check(s)) {
printf("Unable to open session!\n");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
exit(-1);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.8 rule Note

MISRA 21.8 rule
}
// 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) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
printf("Unable to start read and lease tasks");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
exit(-1);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.8 rule Note

MISRA 21.8 rule
}
// 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");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
exit(-1);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.8 rule Note

MISRA 21.8 rule
}

// Send packets
while (1) {
z_publisher_put(z_loan(pub), (const uint8_t *)value, len, 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);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.3 rule Note

MISRA 21.3 rule
exit(0);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.8 rule Note

MISRA 21.8 rule
}
#else
int main(void) {
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_PUBLICATION but this example requires it.\n");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
return -2;
}
#endif
121 changes: 121 additions & 0 deletions examples/unix/c11/z_sub_thr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//
// 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>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdio.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdlib.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <time.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <time.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <time.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "zenoh-pico.h"

#define N 1000000

typedef struct {
volatile unsigned long count;

Check warning

Code scanning / Cppcheck (reported by Codacy)

struct member 'z_stats_t::count' is never used. Warning

struct member 'z_stats_t::count' is never used.
volatile unsigned long finished_rounds;

Check warning

Code scanning / Cppcheck (reported by Codacy)

struct member 'z_stats_t::finished_rounds' is never used. Warning

struct member 'z_stats_t::finished_rounds' is never used.
volatile clock_t start;

Check warning

Code scanning / Cppcheck (reported by Codacy)

struct member 'z_stats_t::start' is never used. Warning

struct member 'z_stats_t::start' is never used.
volatile clock_t stop;

Check warning

Code scanning / Cppcheck (reported by Codacy)

struct member 'z_stats_t::stop' is never used. Warning

struct member 'z_stats_t::stop' is never used.
volatile clock_t first_start;

Check warning

Code scanning / Cppcheck (reported by Codacy)

struct member 'z_stats_t::first_start' is never used. Warning

struct member 'z_stats_t::first_start' is never used.
} z_stats_t;

#if Z_FEATURE_SUBSCRIPTION == 1

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 2009 with no text in the supplied rule-texts-file Warning

misra violation 2009 with no text in the supplied rule-texts-file

z_stats_t *z_stats_make() {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
z_stats_t *stats = malloc(sizeof(z_stats_t));
stats->count = 0;
stats->finished_rounds = 0;
stats->first_start = 0;
return stats;
}

void on_sample(const z_sample_t *sample, void *context) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 207 with no text in the supplied rule-texts-file Warning

misra violation 207 with no text in the supplied rule-texts-file
z_stats_t *stats = (z_stats_t *)context;
if (stats->count == 0) {
stats->start = clock();
if (!stats->first_start) {
stats->first_start = stats->start;
}
stats->count++;
} else if (stats->count < N) {
stats->count++;
} else {
stats->stop = clock();
stats->finished_rounds++;
printf("%f msg/s\n", N * (double)CLOCKS_PER_SEC / (double)(stats->stop - stats->start));
stats->count = 0;
}
}

void drop_stats(void *context) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
const clock_t end = clock();
const z_stats_t *stats = (z_stats_t *)context;
const double elapsed = (double)(end - stats->first_start) / (double)CLOCKS_PER_SEC;
const unsigned long sent_messages = N * stats->finished_rounds + stats->count;
printf("Stats being dropped after unsubscribing: sent %ld messages over %f seconds (%f msg/s)\n", sent_messages,
elapsed, (double)sent_messages / elapsed);
free(context);
}

int main(int argc, char **argv) {
char *keyexpr = "test/thr";

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 704 with no text in the supplied rule-texts-file Warning

misra violation 704 with no text in the supplied rule-texts-file
z_owned_config_t config = z_config_default();

// Set config
if (argc > 1) {
if (zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])) < 0) {
printf("Failed to insert locator in config: %s\n", argv[1]);
exit(-1);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe
}
}
// Open session
z_owned_session_t s = z_open(z_move(config));
if (!z_check(s)) {
printf("Unable to open session!\n");
exit(-1);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe
}
// 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);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe
}
// Declare Subscriber/resource
z_stats_t *context = z_stats_make();
z_owned_closure_sample_t callback = z_closure(on_sample, drop_stats, (void *)context);
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);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe
}
// Listen until stopped
printf("Start listening.\n");
char c = 0;
while (c != 'q') {
c = fgetc(stdin);

Check warning

Code scanning / Cppcheck (reported by Codacy)

stdin is MT-unsafe Warning

stdin is MT-unsafe
}
// 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);

Check warning

Code scanning / Cppcheck (reported by Codacy)

exit is MT-unsafe Warning

exit is MT-unsafe
}
#else
int main(void) {
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_SUBSCRIPTION but this example requires it.\n");
return -2;
}
#endif

0 comments on commit efc5873

Please sign in to comment.