|
| 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 N 1000000 |
| 23 | + |
| 24 | +typedef struct { |
| 25 | + volatile unsigned long count; |
| 26 | + volatile unsigned long finished_rounds; |
| 27 | + volatile clock_t start; |
| 28 | + volatile clock_t stop; |
| 29 | + volatile clock_t first_start; |
| 30 | +} z_stats_t; |
| 31 | + |
| 32 | +#if Z_FEATURE_SUBSCRIPTION == 1 |
| 33 | + |
| 34 | +z_stats_t *z_stats_make() { |
| 35 | + z_stats_t *stats = malloc(sizeof(z_stats_t)); |
| 36 | + stats->count = 0; |
| 37 | + stats->finished_rounds = 0; |
| 38 | + stats->first_start = 0; |
| 39 | + return stats; |
| 40 | +} |
| 41 | + |
| 42 | +void on_sample(const z_sample_t *sample, void *context) { |
| 43 | + z_stats_t *stats = (z_stats_t *)context; |
| 44 | + if (stats->count == 0) { |
| 45 | + stats->start = clock(); |
| 46 | + if (!stats->first_start) { |
| 47 | + stats->first_start = stats->start; |
| 48 | + } |
| 49 | + stats->count++; |
| 50 | + } else if (stats->count < N) { |
| 51 | + stats->count++; |
| 52 | + } else { |
| 53 | + stats->stop = clock(); |
| 54 | + stats->finished_rounds++; |
| 55 | + printf("%f msg/s\n", N * (double)CLOCKS_PER_SEC / (double)(stats->stop - stats->start)); |
| 56 | + stats->count = 0; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void drop_stats(void *context) { |
| 61 | + const clock_t end = clock(); |
| 62 | + const z_stats_t *stats = (z_stats_t *)context; |
| 63 | + const double elapsed = (double)(end - stats->first_start) / (double)CLOCKS_PER_SEC; |
| 64 | + const unsigned long sent_messages = N * stats->finished_rounds + stats->count; |
| 65 | + printf("Stats being dropped after unsubscribing: sent %ld messages over %f seconds (%f msg/s)\n", sent_messages, |
| 66 | + elapsed, (double)sent_messages / elapsed); |
| 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