Skip to content

Commit a952247

Browse files
authored
Add fragment test to integration testing (#302)
* test: add fragment test to integration testing * fix: change perf test config * feat: add throughput examples * fix: remove test for performance * fix: use monotonic time for throughput example * fix: convert type before divide operation
1 parent a0b421c commit a952247

File tree

7 files changed

+258
-11
lines changed

7 files changed

+258
-11
lines changed

.github/workflows/integration.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444

4545
- name: Test debug
4646
run: make test
47+
timeout-minutes: 15
4748
env:
4849
BUILD_TYPE: Debug # Workaround for Windows as it seems the previous step is being ignored
4950
BUILD_TESTING: OFF # Workaround for Windows as it seems the previous step is being ignored

examples/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ if(UNIX)
4545
add_example(z_scout unix/c11/z_scout.c)
4646
add_example(z_ping unix/c11/z_ping.c)
4747
add_example(z_pong unix/c11/z_pong.c)
48+
add_example(z_pub_thr unix/c11/z_pub_thr.c)
49+
add_example(z_sub_thr unix/c11/z_sub_thr.c)
4850
endif()
4951
elseif(MSVC)
5052
add_example(z_put windows/z_put.c)

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 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

tests/z_client_test.c

+34-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#define MSG 1000
2727
#define MSG_LEN 1024
28+
#define FRAGMENT_MSG_NB 100
29+
#define FRAGMENT_MSG_LEN 100000
2830
#define QRY 100
2931
#define QRY_CLT 10
3032
#define SET 100
@@ -92,7 +94,7 @@ void data_handler(const z_sample_t *sample, void *arg) {
9294
printf(">> Received data: %s\t(%u/%u)\n", res, datas, total);
9395

9496
z_owned_str_t k_str = z_keyexpr_to_string(sample->keyexpr);
95-
assert(sample->payload.len == MSG_LEN);
97+
assert((sample->payload.len == MSG_LEN) || (sample->payload.len == FRAGMENT_MSG_LEN));
9698
assert(_z_str_eq(z_loan(k_str), res) == true);
9799

98100
datas++;
@@ -233,6 +235,37 @@ int main(int argc, char **argv) {
233235

234236
z_sleep_s(SLEEP);
235237

238+
// Write fragment data from first session
239+
if (is_reliable) {
240+
z_free((uint8_t *)payload);
241+
len = FRAGMENT_MSG_LEN;
242+
payload = (uint8_t *)z_malloc(len);
243+
memset(payload, 1, FRAGMENT_MSG_LEN);
244+
245+
total = FRAGMENT_MSG_NB * SET;
246+
for (unsigned int n = 0; n < FRAGMENT_MSG_NB; n++) {
247+
for (unsigned int i = 0; i < SET; i++) {
248+
z_put_options_t opt = z_put_options_default();
249+
opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK;
250+
z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt);
251+
printf("Wrote fragment data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len,
252+
n * SET + (i + 1), total);
253+
}
254+
}
255+
// Wait to receive all the data
256+
now = z_clock_now();
257+
while (datas < total) {
258+
assert(z_clock_elapsed_s(&now) < TIMEOUT);
259+
printf("Waiting for fragment datas... %u/%u\n", datas, total);
260+
z_sleep_s(SLEEP);
261+
}
262+
if (is_reliable == true) {
263+
assert(datas == total);
264+
}
265+
datas = 0;
266+
z_sleep_s(SLEEP);
267+
}
268+
236269
// Query data from first session
237270
total = QRY * SET;
238271
for (unsigned int n = 0; n < QRY; n++) {

tests/z_perf_rx.c

+12-3
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,30 @@ void on_sample(const z_sample_t *sample, void *context) {
6060

6161
int main(int argc, char **argv) {
6262
char *keyexpr = "test/thr";
63-
const char *mode = "client";
63+
const char *mode = NULL;
6464
char *llocator = NULL;
65+
char *clocator = NULL;
6566
(void)argv;
6667

67-
// Check if peer mode
68+
// Check if peer or client mode
6869
if (argc > 1) {
6970
mode = "peer";
7071
llocator = "udp/224.0.0.224:7447#iface=lo";
72+
} else {
73+
mode = "client";
74+
clocator = "tcp/127.0.0.1:7447";
7175
}
7276
// Set config
7377
z_owned_config_t config = z_config_default();
74-
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode));
78+
if (mode != NULL) {
79+
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode));
80+
}
7581
if (llocator != NULL) {
7682
zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator));
7783
}
84+
if (clocator != NULL) {
85+
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator));
86+
}
7887
// Open session
7988
z_owned_session_t s = z_open(z_move(config));
8089
if (!z_check(s)) {

tests/z_perf_tx.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ int send_packets(unsigned long pkt_len, z_owned_publisher_t *pub, uint8_t *value
2626
z_clock_t test_start = z_clock_now();
2727
unsigned long elapsed_us = 0;
2828
while (elapsed_us < TEST_DURATION_US) {
29-
if (z_publisher_put(z_loan(*pub), (const uint8_t *)value, pkt_len, NULL) != 0) {
30-
printf("Put failed for pkt len: %lu\n", pkt_len);
31-
return -1;
32-
}
29+
z_publisher_put(z_loan(*pub), (const uint8_t *)value, pkt_len, NULL);
3330
elapsed_us = z_clock_elapsed_us(&test_start);
3431
}
3532
return 0;
@@ -41,21 +38,30 @@ int main(int argc, char **argv) {
4138
uint8_t *value = (uint8_t *)malloc(len_array[0]);
4239
memset(value, 1, len_array[0]);
4340
char *keyexpr = "test/thr";
44-
const char *mode = "client";
41+
const char *mode = NULL;
4542
char *llocator = NULL;
43+
char *clocator = NULL;
4644
(void)argv;
4745

48-
// Check if peer mode
46+
// Check if peer or client mode
4947
if (argc > 1) {
5048
mode = "peer";
5149
llocator = "udp/224.0.0.224:7447#iface=lo";
50+
} else {
51+
mode = "client";
52+
clocator = "tcp/127.0.0.1:7447";
5253
}
5354
// Set config
5455
z_owned_config_t config = z_config_default();
55-
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode));
56+
if (mode != NULL) {
57+
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode));
58+
}
5659
if (llocator != NULL) {
5760
zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator));
5861
}
62+
if (clocator != NULL) {
63+
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator));
64+
}
5965
// Open session
6066
z_owned_session_t s = z_open(z_move(config));
6167
if (!z_check(s)) {

0 commit comments

Comments
 (0)