|
| 1 | +// |
| 2 | +// Copyright (c) 2022 ZettaScale Technology |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | +// |
| 11 | +// Contributors: |
| 12 | +// ZettaScale Zenoh Team, <[email protected]> |
| 13 | +// |
| 14 | +#include <stdint.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <string.h> |
| 18 | +#include <time.h> |
| 19 | + |
| 20 | +#include "zenoh-pico.h" |
| 21 | + |
| 22 | +#define PACKET_NB 1000000 |
| 23 | + |
| 24 | +typedef struct { |
| 25 | + volatile unsigned long count; |
| 26 | + volatile unsigned long finished_rounds; |
| 27 | + z_clock_t start; |
| 28 | + z_clock_t first_start; |
| 29 | +} z_stats_t; |
| 30 | + |
| 31 | +#if Z_FEATURE_SUBSCRIPTION == 1 |
| 32 | + |
| 33 | +z_stats_t *z_stats_make(void) { |
| 34 | + z_stats_t *stats = malloc(sizeof(z_stats_t)); |
| 35 | + stats->count = 0; |
| 36 | + stats->finished_rounds = 0; |
| 37 | + stats->first_start.tv_nsec = 0; |
| 38 | + return stats; |
| 39 | +} |
| 40 | + |
| 41 | +void on_sample(const z_sample_t *sample, void *context) { |
| 42 | + (void)sample; |
| 43 | + z_stats_t *stats = (z_stats_t *)context; |
| 44 | + stats->count++; |
| 45 | + // Start set measurement |
| 46 | + if (stats->count == 1) { |
| 47 | + stats->start = z_clock_now(); |
| 48 | + if (stats->first_start.tv_nsec == 0) { |
| 49 | + stats->first_start = stats->start; |
| 50 | + } |
| 51 | + } else if (stats->count >= PACKET_NB) { |
| 52 | + // Stop set measurement |
| 53 | + stats->finished_rounds++; |
| 54 | + unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->start); |
| 55 | + printf("Received %d msg in %lu ms (%.1f msg/s)\n", PACKET_NB, elapsed_ms, |
| 56 | + (double)(PACKET_NB * 1000) / (double)elapsed_ms); |
| 57 | + stats->count = 0; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void drop_stats(void *context) { |
| 62 | + z_stats_t *stats = (z_stats_t *)context; |
| 63 | + unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->first_start); |
| 64 | + const unsigned long sent_messages = PACKET_NB * stats->finished_rounds + stats->count; |
| 65 | + printf("Stats after unsubscribing: received %ld messages over %lu miliseconds (%.1f msg/s)\n", sent_messages, |
| 66 | + elapsed_ms, (double)(sent_messages * 1000) / (double)elapsed_ms); |
| 67 | + free(context); |
| 68 | +} |
| 69 | + |
| 70 | +int main(int argc, char **argv) { |
| 71 | + char *keyexpr = "test/thr"; |
| 72 | + z_owned_config_t config = z_config_default(); |
| 73 | + |
| 74 | + // Set config |
| 75 | + if (argc > 1) { |
| 76 | + if (zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])) < 0) { |
| 77 | + printf("Failed to insert locator in config: %s\n", argv[1]); |
| 78 | + exit(-1); |
| 79 | + } |
| 80 | + } |
| 81 | + // Open session |
| 82 | + z_owned_session_t s = z_open(z_move(config)); |
| 83 | + if (!z_check(s)) { |
| 84 | + printf("Unable to open session!\n"); |
| 85 | + exit(-1); |
| 86 | + } |
| 87 | + // Start read and lease tasks for zenoh-pico |
| 88 | + if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { |
| 89 | + printf("Unable to start read and lease tasks"); |
| 90 | + exit(-1); |
| 91 | + } |
| 92 | + // Declare Subscriber/resource |
| 93 | + z_stats_t *context = z_stats_make(); |
| 94 | + z_owned_closure_sample_t callback = z_closure(on_sample, drop_stats, (void *)context); |
| 95 | + z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(callback), NULL); |
| 96 | + if (!z_check(sub)) { |
| 97 | + printf("Unable to create subscriber.\n"); |
| 98 | + exit(-1); |
| 99 | + } |
| 100 | + // Listen until stopped |
| 101 | + printf("Start listening.\n"); |
| 102 | + char c = 0; |
| 103 | + while (c != 'q') { |
| 104 | + c = fgetc(stdin); |
| 105 | + } |
| 106 | + // Wait for everything to settle |
| 107 | + printf("End of test\n"); |
| 108 | + z_sleep_s(1); |
| 109 | + // Clean up |
| 110 | + z_undeclare_subscriber(z_move(sub)); |
| 111 | + zp_stop_read_task(z_loan(s)); |
| 112 | + zp_stop_lease_task(z_loan(s)); |
| 113 | + z_close(z_move(s)); |
| 114 | + exit(0); |
| 115 | +} |
| 116 | +#else |
| 117 | +int main(void) { |
| 118 | + printf("ERROR: Zenoh pico was compiled without Z_FEATURE_SUBSCRIPTION but this example requires it.\n"); |
| 119 | + return -2; |
| 120 | +} |
| 121 | +#endif |
0 commit comments