Skip to content

Commit efc5873

Browse files
committed
feat: add throughput examples
1 parent 2a371f0 commit efc5873

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed

examples/unix/c11/z_pub_thr.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
19+
#include "zenoh-pico.h"
20+
21+
#if Z_FEATURE_PUBLICATION == 1
22+
int main(int argc, char **argv) {
23+
if (argc < 2) {
24+
printf("USAGE:\n\tz_pub_thr <payload-size> [<zenoh-locator>]\n\n");
25+
exit(-1);
26+
}
27+
char *keyexpr = "test/thr";
28+
size_t len = atoi(argv[1]);
29+
uint8_t *value = (uint8_t *)malloc(len);
30+
memset(value, 1, len);
31+
32+
// Set config
33+
z_owned_config_t config = z_config_default();
34+
if (argc > 2) {
35+
if (zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[2])) < 0) {
36+
printf("Couldn't insert locator in config: %s\n", argv[2]);
37+
exit(-1);
38+
}
39+
}
40+
// Open session
41+
z_owned_session_t s = z_open(z_move(config));
42+
if (!z_check(s)) {
43+
printf("Unable to open session!\n");
44+
exit(-1);
45+
}
46+
// Start read and lease tasks for zenoh-pico
47+
if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) {
48+
printf("Unable to start read and lease tasks");
49+
exit(-1);
50+
}
51+
// Declare publisher
52+
z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(keyexpr), NULL);
53+
if (!z_check(pub)) {
54+
printf("Unable to declare publisher for key expression!\n");
55+
exit(-1);
56+
}
57+
58+
// Send packets
59+
while (1) {
60+
z_publisher_put(z_loan(pub), (const uint8_t *)value, len, NULL);
61+
}
62+
// Clean up
63+
z_undeclare_publisher(z_move(pub));
64+
zp_stop_read_task(z_loan(s));
65+
zp_stop_lease_task(z_loan(s));
66+
z_close(z_move(s));
67+
free(value);
68+
exit(0);
69+
}
70+
#else
71+
int main(void) {
72+
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_PUBLICATION but this example requires it.\n");
73+
return -2;
74+
}
75+
#endif

examples/unix/c11/z_sub_thr.c

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)