1 /*
2  * Copyright (c) 2024 Måns Ansgariusson <mansgariusson@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdint.h>
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/ztress.h>
10 #include <zephyr/logging/log.h>
11 #include <zephyr/random/random.h>
12 #include <zephyr/timing/timing.h>
13 
14 #define WRITE_LEN 512
15 #define READ_LEN  512
16 
17 LOG_MODULE_REGISTER(k_k_pipe_stress, LOG_LEVEL_INF);
18 
19 ZTEST_SUITE(k_pipe_stress, NULL, NULL, NULL, NULL, NULL);
20 
21 static struct k_pipe pipe;
22 
ZTEST(k_pipe_stress,test_write)23 ZTEST(k_pipe_stress, test_write)
24 {
25 	int rc;
26 	const size_t len = WRITE_LEN;
27 	uint8_t buffer[WRITE_LEN];
28 	uint8_t buf[WRITE_LEN];
29 	size_t sent;
30 	uint32_t start_cycles, end_cycles;
31 
32 	k_pipe_init(&pipe, buffer, sizeof(buffer));
33 	start_cycles = k_uptime_get_32();
34 	sent = 0;
35 	while (sent < len) {
36 		rc = k_pipe_write(&pipe, &buf[sent], len - sent, K_FOREVER);
37 		zassert_true(rc > 0, "Failed to write to pipe");
38 		sent += rc;
39 	}
40 	end_cycles = k_uptime_get_32();
41 	LOG_INF("Elapsed cycles: %u\n", end_cycles - start_cycles);
42 }
43 
ZTEST(k_pipe_stress,test_read)44 ZTEST(k_pipe_stress, test_read)
45 {
46 	int rc;
47 	const size_t len = READ_LEN;
48 	uint8_t buffer[READ_LEN];
49 	uint8_t buf[READ_LEN];
50 	size_t sent, read;
51 	uint32_t start_cycles, end_cycles;
52 
53 	k_pipe_init(&pipe, buffer, sizeof(buffer));
54 	start_cycles = k_uptime_get_32();
55 	for (int i = 0; i < 100; i++) {
56 		sent = 0;
57 		while (sent < len) {
58 			rc = k_pipe_write(&pipe, &buf[sent], len - sent, K_FOREVER);
59 			zassert_true(rc > 0, "Failed to write to pipe");
60 			sent += rc;
61 		}
62 		read = 0;
63 		while (read < len) {
64 			rc = k_pipe_read(&pipe, &buf[read], len - read, K_FOREVER);
65 			zassert_true(rc > 0, "Failed to read from pipe");
66 			read += rc;
67 		}
68 	}
69 	end_cycles = k_uptime_get_32();
70 	LOG_INF("Elapsed cycles: %u\n", end_cycles - start_cycles);
71 }
72