Skip to content

Commit 2cb69c8

Browse files
authored
testcase: add system performance testcase (#10452)
* add sys pref frameworks * add context switch test code * add irq latency test code * add thread sem test code * add thread event test code * add thread mbox test code * add thread mq test code * Adding a Test Documentation Note * Modification of text description
1 parent 156259b commit 2cb69c8

File tree

12 files changed

+942
-0
lines changed

12 files changed

+942
-0
lines changed

examples/utest/testcases/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rsource "posix/Kconfig"
1717
rsource "mm/Kconfig"
1818
rsource "tmpfs/Kconfig"
1919
rsource "smp_call/Kconfig"
20+
rsource "perf/Kconfig"
2021
endif
2122

2223
endmenu

examples/utest/testcases/perf/Kconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
menu "SyStem Performance Testcase"
2+
3+
config UTEST_SYS_PERF_TC
4+
bool "SyStem Performance test"
5+
default n
6+
7+
config UTEST_SYS_PERF_TC_COUNT
8+
int "Test the number of cycles"
9+
default 1000
10+
depends on UTEST_SYS_PERF_TC
11+
12+
config UTEST_HWTIMER_DEV_NAME
13+
string "Hardware timer device name"
14+
default "timer0"
15+
depends on RT_USING_HWTIMER && UTEST_SYS_PERF_TC
16+
help
17+
Specify the hardware timer device name used for context switch testing (e.g., timer0).
18+
19+
config UTEST_SYS_IRQ_LATENCY
20+
bool "SyStem IRQ LATENCY test"
21+
default n
22+
depends on RT_USING_HWTIMER && UTEST_SYS_PERF_TC
23+
24+
endmenu
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# testcases 说明
2+
3+
## 一、测试用例说明
4+
5+
| 文件 | 说明 |
6+
|--------|--------|
7+
| context_switch.c | 上下文切换测试代码 |
8+
| irq_latency.c | 中断延时测试代码 |
9+
| rt_perf_thread_event.c | 线程事件性能测试 |
10+
| rt_perf_thread_mbox.c | 线程邮箱性能测试 |
11+
| rt_perf_thread_mq.c | 线程消息队列性能测试 |
12+
| rt_perf_thread_sem.c | 线程信号量性能测试 |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Import('rtconfig')
2+
from building import *
3+
4+
cwd = GetCurrentDir()
5+
src = Glob('*.c')
6+
CPPPATH = [cwd]
7+
8+
group = DefineGroup('utestcases', src, depend = ['UTEST_SYS_PERF_TC'], CPPPATH = CPPPATH)
9+
10+
Return('group')
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 context_switch
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rthw.h>
13+
#include <rtdevice.h>
14+
#include <utest.h>
15+
#include <utest_assert.h>
16+
#include <perf_tc.h>
17+
18+
static rt_sem_t sem1, sem2;
19+
static rt_sem_t complete_sem = RT_NULL;
20+
21+
static void local_modify_time(rt_perf_t *perf)
22+
{
23+
if(perf)
24+
perf->real_time = perf->real_time - perf->tmp_time;
25+
}
26+
27+
static void perf_thread_event1(void *parameter)
28+
{
29+
while (1)
30+
{
31+
rt_sem_take(sem1, RT_WAITING_FOREVER);
32+
rt_sem_release(sem2);
33+
}
34+
}
35+
36+
static void perf_thread_event2(void *parameter)
37+
{
38+
rt_perf_t *perf = (rt_perf_t *)parameter;
39+
40+
for (rt_uint32_t i = 0; i < UTEST_SYS_PERF_TC_COUNT; i++)
41+
{
42+
perf->tmp_time = 0;
43+
rt_perf_start(perf);
44+
rt_sem_take(sem2, RT_WAITING_FOREVER);
45+
rt_sem_release(sem2);
46+
rt_perf_stop(perf);
47+
48+
rt_mutex_take(perf->lock,RT_WAITING_FOREVER);
49+
perf->count -= 1;
50+
perf->tmp_time = perf->real_time;
51+
rt_mutex_release(perf->lock);
52+
53+
rt_perf_start(perf);
54+
rt_sem_take(sem2, RT_WAITING_FOREVER);
55+
rt_sem_release(sem1);
56+
rt_perf_stop(perf);
57+
}
58+
rt_sem_release(complete_sem);
59+
}
60+
61+
rt_err_t context_switch_test(rt_perf_t *perf)
62+
{
63+
rt_thread_t thread1 = RT_NULL;
64+
rt_thread_t thread2 = RT_NULL;
65+
66+
# if __STDC_VERSION__ >= 199901L
67+
rt_strcpy(perf->name,__func__);
68+
#else
69+
rt_strcpy(perf->name,"context_switch_test");
70+
#endif
71+
72+
perf->local_modify = local_modify_time;
73+
sem1 = rt_sem_create("sem1", 1, RT_IPC_FLAG_FIFO);
74+
sem2 = rt_sem_create("sem2", 0, RT_IPC_FLAG_FIFO);
75+
complete_sem = rt_sem_create("complete_sem", 0, RT_IPC_FLAG_FIFO);
76+
77+
thread1 = rt_thread_create("perf_thread_event1", perf_thread_event1, perf,
78+
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
79+
if (thread1 == RT_NULL)
80+
{
81+
LOG_E("perf_thread_event1 create failed.");
82+
return -RT_ERROR;
83+
}
84+
85+
thread2 = rt_thread_create("perf_thread_event2", perf_thread_event2, perf,
86+
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
87+
if (thread2 == RT_NULL)
88+
{
89+
LOG_E("perf_thread_event2 create failed.");
90+
return -RT_ERROR;
91+
}
92+
93+
rt_thread_startup(thread1);
94+
rt_thread_startup(thread2);
95+
96+
rt_sem_take(complete_sem, RT_WAITING_FOREVER);
97+
98+
rt_perf_dump(perf);
99+
rt_thread_delete(thread1);
100+
rt_sem_delete(complete_sem);
101+
rt_sem_delete(sem1);
102+
rt_sem_delete(sem2);
103+
104+
return RT_EOK;
105+
}
106+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 irq latency
9+
*/
10+
11+
#include <rtthread.h>
12+
#include <rtdevice.h>
13+
#include <utest.h>
14+
#include <utest_assert.h>
15+
#include <perf_tc.h>
16+
17+
static rt_device_t hw_dev = RT_NULL;
18+
static rt_sem_t complete_sem = RT_NULL;
19+
static rt_hwtimerval_t timeout = {0};
20+
static rt_perf_t *perf_local = RT_NULL;
21+
22+
static void modify_time(rt_perf_t *perf)
23+
{
24+
if(perf)
25+
perf->real_time = perf->real_time - perf->tmp_time;
26+
}
27+
28+
static rt_err_t timer_callback(rt_device_t dev, rt_size_t size)
29+
{
30+
rt_perf_stop(perf_local);
31+
if (perf_local->count >= UTEST_SYS_PERF_TC_COUNT)
32+
{
33+
rt_sem_release(complete_sem);
34+
return RT_EOK;
35+
}
36+
rt_perf_start_impl(perf_local, &timeout);
37+
return RT_EOK;
38+
}
39+
40+
rt_err_t rt_perf_irq_latency(rt_perf_t *perf)
41+
{
42+
# if __STDC_VERSION__ >= 199901L
43+
rt_strcpy(perf->name,__func__);
44+
#else
45+
rt_strcpy(perf->name,"rt_perf_irq_latency");
46+
#endif
47+
int ret = RT_EOK;
48+
rt_hwtimer_mode_t mode = HWTIMER_MODE_PERIOD;
49+
50+
perf_local = perf;
51+
hw_dev = rt_device_find(UTEST_HWTIMER_DEV_NAME);
52+
if (hw_dev == RT_NULL)
53+
{
54+
ret = RT_ERROR;
55+
LOG_E("hwtimer sample run failed! can't find %s device!", UTEST_HWTIMER_DEV_NAME);
56+
return ret;
57+
}
58+
59+
complete_sem = rt_sem_create("complete", 0, RT_IPC_FLAG_FIFO);
60+
timeout.sec = 0;
61+
timeout.usec = 50; /* No modification is necessary here, use the fixed value */
62+
63+
rt_mutex_take(perf->lock,RT_WAITING_FOREVER);
64+
perf_local->tmp_time = (rt_uint32_t)(timeout.sec * 1000000u + timeout.usec);
65+
perf_local->local_modify = modify_time;
66+
rt_mutex_release(perf->lock);
67+
68+
rt_device_set_rx_indicate(hw_dev, timer_callback);
69+
rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, (void *)&mode);
70+
71+
rt_perf_start_impl(perf_local, &timeout);
72+
73+
rt_sem_take(complete_sem, RT_WAITING_FOREVER);
74+
rt_perf_dump(perf_local);
75+
rt_sem_delete(complete_sem);
76+
rt_device_close(hw_dev);
77+
78+
return RT_EOK;
79+
}
80+

0 commit comments

Comments
 (0)