Skip to content

Commit 141e7c8

Browse files
committed
add thread mq test code
1 parent bb7b344 commit 141e7c8

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright (c) 2006-2025, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2025-07-03 rcitach test case for messagequeue
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rthw.h>
13+
#include <rtdevice.h>
14+
#include <utest.h>
15+
#include <utest_assert.h>
16+
17+
#define PERF_COUNT 1000
18+
static rt_sem_t sem1 = RT_NULL;
19+
static rt_mq_t perf_thread_mq = RT_NULL;
20+
volatile static uint16_t thread_event_count = 0;
21+
#define THREAD_STACK_SIZE 1024
22+
#define THREAD_PRIORITY 10
23+
#define THREAD_TIMESLICE 5
24+
25+
struct perf_time
26+
{
27+
rt_uint32_t start_time;
28+
rt_uint32_t end_time;
29+
rt_uint32_t use_time;
30+
};
31+
static struct perf_time use_time[PERF_COUNT];
32+
33+
#ifdef USING_HWTIME_AS_TIME_REF
34+
static rt_device_t hw_dev = RT_NULL;
35+
static rt_hwtimerval_t timer_val;
36+
37+
static rt_uint32_t get_timer_us(void)
38+
{
39+
if (hw_dev && rt_device_read(hw_dev, 0, &timer_val, sizeof(rt_hwtimerval_t)))
40+
{
41+
return (rt_uint32_t)(timer_val.sec * 1000000 + timer_val.usec);
42+
}
43+
return 0;
44+
}
45+
#endif
46+
47+
static void perf_thread_mq1(void *parameter)
48+
{
49+
rt_ubase_t recv = 0;
50+
rt_uint32_t total_time = 0;
51+
while (1)
52+
{
53+
rt_mq_recv(perf_thread_mq, &recv, 1, RT_WAITING_FOREVER);
54+
#ifdef USING_HWTIME_AS_TIME_REF
55+
use_time[thread_event_count].end_time = get_timer_us();
56+
use_time[thread_event_count].use_time = use_time[thread_event_count].end_time - use_time[thread_event_count].start_time;
57+
#else
58+
use_time[thread_event_count].end_time = rt_tick_get();
59+
use_time[thread_event_count].use_time = (use_time[thread_event_count].end_time - use_time[thread_event_count].start_time) / RT_TICK_PER_SECOND;
60+
use_time[thread_event_count].use_time += ((use_time[thread_event_count].end_time - use_time[thread_event_count].start_time) % RT_TICK_PER_SECOND) * (1000000u / RT_TICK_PER_SECOND);
61+
#endif
62+
if (recv != 'A')
63+
{
64+
LOG_E("mq recv value error!");
65+
rt_mq_delete(perf_thread_mq);
66+
return;
67+
}
68+
69+
recv = 0;
70+
total_time += use_time[thread_event_count].use_time;
71+
thread_event_count ++;
72+
if (thread_event_count >= PERF_COUNT)
73+
{
74+
LOG_I("Messagequeue test completed: %d iterations, total time = %lu us, average = %lu us per messagequeue",
75+
PERF_COUNT, total_time, total_time / PERF_COUNT);
76+
77+
rt_mq_delete(perf_thread_mq);
78+
rt_sem_delete(sem1);
79+
return;
80+
}
81+
rt_sem_release(sem1);
82+
}
83+
}
84+
85+
static void perf_thread_mq2(void *parameter)
86+
{
87+
char send = 'A';
88+
while (1)
89+
{
90+
if (thread_event_count >= PERF_COUNT)
91+
{
92+
return;
93+
}
94+
rt_sem_take(sem1, RT_WAITING_FOREVER);
95+
#ifdef USING_HWTIME_AS_TIME_REF
96+
use_time[thread_event_count].start_time = get_timer_us();
97+
#else
98+
use_time[thread_event_count].start_time = rt_tick_get();
99+
#endif
100+
rt_mq_send(perf_thread_mq, &send, 1);
101+
}
102+
}
103+
104+
static void rt_perf_thread_mq(void)
105+
{
106+
rt_thread_t thread1 = RT_NULL;
107+
rt_thread_t thread2 = RT_NULL;
108+
109+
perf_thread_mq = rt_mq_create("perf_thread_mq", 1, 1, RT_IPC_FLAG_PRIO);
110+
if (perf_thread_mq == RT_NULL)
111+
{
112+
LOG_E("perf_thread_mq create failed.");
113+
return;
114+
}
115+
116+
sem1 = rt_sem_create("sem1", 1, RT_IPC_FLAG_FIFO);
117+
118+
thread1 = rt_thread_create("perf_thread_mq1", perf_thread_mq1, RT_NULL,
119+
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
120+
if (thread1 == RT_NULL)
121+
{
122+
LOG_E("perf_thread_mq1 create failed.");
123+
return;
124+
}
125+
126+
thread2 = rt_thread_create("perf_thread_mq2", perf_thread_mq2, RT_NULL,
127+
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
128+
if (thread2 == RT_NULL)
129+
{
130+
LOG_E("perf_thread_mq2 create failed.");
131+
return;
132+
}
133+
134+
rt_thread_startup(thread1);
135+
rt_thread_startup(thread2);
136+
}
137+
138+
static rt_err_t utest_tc_init(void)
139+
{
140+
#ifdef USING_HWTIME_AS_TIME_REF
141+
rt_hwtimerval_t timeout_s;
142+
int ret = RT_EOK;
143+
hw_dev = rt_device_find(UTEST_HWTIMER_DEV_NAME);
144+
if (hw_dev == RT_NULL)
145+
{
146+
ret = RT_ERROR;
147+
LOG_E("hwtimer sample run failed! can't find %s device!", UTEST_HWTIMER_DEV_NAME);
148+
return ret;
149+
}
150+
151+
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
152+
if (ret != RT_EOK)
153+
{
154+
LOG_E("open %s device failed!", UTEST_HWTIMER_DEV_NAME);
155+
return ret;
156+
}
157+
158+
timeout_s.sec = 10; /* s */
159+
timeout_s.usec = 0; /* us */
160+
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(rt_hwtimerval_t)) != sizeof(rt_hwtimerval_t))
161+
{
162+
ret = RT_ERROR;
163+
LOG_E("set timeout value failed");
164+
return ret;
165+
}
166+
#endif
167+
168+
return RT_EOK;
169+
}
170+
171+
static rt_err_t utest_tc_cleanup(void)
172+
{
173+
#ifdef USING_HWTIME_AS_TIME_REF
174+
if (hw_dev) rt_device_close(hw_dev);
175+
#endif
176+
return RT_EOK;
177+
}
178+
179+
static void testcase(void)
180+
{
181+
UTEST_UNIT_RUN(rt_perf_thread_mq);
182+
}
183+
184+
UTEST_TC_EXPORT(testcase, "testcase.pref.messagequeue", utest_tc_init, utest_tc_cleanup, 10);

0 commit comments

Comments
 (0)