1 /*
2  * Copyright (c) 2025 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <internal/nrfs_backend.h>
9 #include <nrfs_backend_ipc_service.h>
10 #include <nrfs_mram.h>
11 #include <nrfs_temp.h>
12 #include <zephyr/ztest.h>
13 
14 #define IPC_BACKEND_CONNECTION_TIMEOUT_MS 5000
15 #define NUM_OF_MRAM_REQUESTS              10000
16 #define MRAM_REQUESTS_DEAD_TIME_MS        1
17 #define NUM_OF_TEMP_REQUESTS              100
18 #define TEMP_REQUESTS_DEAD_TIME_MS        100
19 
20 struct ipc_perf_result {
21 	uint32_t sent_requests;
22 	uint32_t handled_requests;
23 	uint32_t failed_to_send;
24 };
25 
26 static volatile uint32_t tst_perf_served_mram_requests;
27 static volatile uint32_t tst_perf_served_temp_meas_requests;
28 
29 /*
30  * Callback function for counting handled TEMP service requests
31  */
temp_handler_for_performance_test(nrfs_temp_evt_t const * p_evt,void * context)32 static void temp_handler_for_performance_test(nrfs_temp_evt_t const *p_evt, void *context)
33 {
34 	switch (p_evt->type) {
35 	case NRFS_TEMP_EVT_MEASURE_DONE:
36 		tst_perf_served_temp_meas_requests++;
37 	default:
38 		break;
39 	}
40 }
41 
42 /*
43  * Callback function for counting handled MRAM service requests
44  */
mram_latency_handler_for_performance_test(nrfs_mram_latency_evt_t const * p_evt,void * context)45 static void mram_latency_handler_for_performance_test(nrfs_mram_latency_evt_t const *p_evt,
46 						      void *context)
47 {
48 	switch (p_evt->type) {
49 	case NRFS_MRAM_LATENCY_REQ_APPLIED:
50 		tst_perf_served_mram_requests++;
51 	default:
52 		break;
53 	}
54 }
55 
56 /*
57  * Test NRFS MRAM latency service requests handling performance
58  */
ZTEST(nrfs_stress_test,test_mram_nrfs_requests_performance)59 ZTEST(nrfs_stress_test, test_mram_nrfs_requests_performance)
60 {
61 	struct ipc_perf_result tst_ipc_perf_result;
62 	uint32_t request_counter = 0;
63 	volatile int32_t tst_ctx = 1;
64 
65 	TC_PRINT("START test_mram_nrfs_requests_performance\n");
66 	zassert_equal(nrfs_mram_init(mram_latency_handler_for_performance_test), NRFS_SUCCESS,
67 		      "Failed to initialise NRFS MRAM latency service");
68 	memset(&tst_ipc_perf_result, 0, sizeof(tst_ipc_perf_result));
69 	while (request_counter < NUM_OF_MRAM_REQUESTS) {
70 		if (nrfs_mram_set_latency(true, (void *)tst_ctx) == NRFS_SUCCESS) {
71 			tst_ipc_perf_result.sent_requests++;
72 		} else {
73 			tst_ipc_perf_result.failed_to_send++;
74 		}
75 		k_msleep(MRAM_REQUESTS_DEAD_TIME_MS);
76 		tst_ctx++;
77 		request_counter++;
78 	}
79 	/* wait for any remaining requests responses */
80 	k_msleep(10 * MRAM_REQUESTS_DEAD_TIME_MS);
81 	tst_ipc_perf_result.handled_requests = tst_perf_served_mram_requests;
82 	TC_PRINT("STOP test_mram_nrfs_requests_performance\n");
83 	TC_PRINT("SENT: %d, HANDLED: %d, FAILED TO SEND: %d\n", tst_ipc_perf_result.sent_requests,
84 		 tst_ipc_perf_result.handled_requests, tst_ipc_perf_result.failed_to_send);
85 	zassert_equal(tst_ipc_perf_result.sent_requests, tst_ipc_perf_result.handled_requests,
86 		      "NRFS MRAM requests sent != served");
87 }
88 
89 /*
90  * Test temperature service requests handling performance
91  */
ZTEST(nrfs_stress_test,test_temperature_nrfs_requests_performance)92 ZTEST(nrfs_stress_test, test_temperature_nrfs_requests_performance)
93 {
94 	struct ipc_perf_result tst_ipc_perf_result;
95 	uint32_t request_counter = 0;
96 	volatile int32_t tst_ctx = 1;
97 
98 	TC_PRINT("START test_temperature_nrfs_requests_performance\n");
99 	zassert_equal(nrfs_temp_init(temp_handler_for_performance_test), NRFS_SUCCESS,
100 		      "Failed to initialise NRFS temperature service");
101 	memset((void *)&tst_ipc_perf_result, 0, sizeof(tst_ipc_perf_result));
102 	while (request_counter < NUM_OF_TEMP_REQUESTS) {
103 		if (nrfs_temp_measure_request((void *)tst_ctx) == NRFS_SUCCESS) {
104 			tst_ipc_perf_result.sent_requests++;
105 		} else {
106 			tst_ipc_perf_result.failed_to_send++;
107 		}
108 		k_msleep(TEMP_REQUESTS_DEAD_TIME_MS);
109 		tst_ctx++;
110 		request_counter++;
111 	}
112 	/* wait for any remaining requests responses */
113 	k_msleep(10 * TEMP_REQUESTS_DEAD_TIME_MS);
114 	tst_ipc_perf_result.handled_requests = tst_perf_served_temp_meas_requests;
115 	TC_PRINT("STOP test_temperature_nrfs_requests_performance\n");
116 	TC_PRINT("SENT: %d, HANDLED: %d, FAILED TO SEND: %d\n", tst_ipc_perf_result.sent_requests,
117 		 tst_ipc_perf_result.handled_requests, tst_ipc_perf_result.failed_to_send);
118 	zassert_equal(tst_ipc_perf_result.sent_requests, tst_ipc_perf_result.handled_requests,
119 		      "NRFS TEMP requests sent != served");
120 }
121 
122 /*
123  * Test setup
124  */
test_setup(void)125 static void *test_setup(void)
126 {
127 	int ret;
128 
129 	tst_perf_served_mram_requests = 0;
130 	tst_perf_served_temp_meas_requests = 0;
131 
132 	TC_PRINT("Hello World! %s\n", CONFIG_BOARD_TARGET);
133 	TC_PRINT("Waiting for NRFS backend init\n");
134 
135 	/* Wait for IPC backend connection */
136 	ret = nrfs_backend_wait_for_connection(K_MSEC(IPC_BACKEND_CONNECTION_TIMEOUT_MS));
137 	zassert_equal(ret, 0, "Failed to establih NRFS backend connection. err: %d", ret);
138 	return NULL;
139 }
140 
141 ZTEST_SUITE(nrfs_stress_test, NULL, test_setup, NULL, NULL, NULL);
142