Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/utest/testcases/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rsource "posix/Kconfig"
rsource "mm/Kconfig"
rsource "tmpfs/Kconfig"
rsource "smp_call/Kconfig"
rsource "perf/Kconfig"
endif

endmenu
24 changes: 24 additions & 0 deletions examples/utest/testcases/perf/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
menu "System Performance Testcase"

config UTEST_SYS_PERF_TC
bool "system performance test"
default n

config UTEST_SYS_PERF_TC_COUNT
int "Test the number of cycles"
default 1000
depends on UTEST_SYS_PERF_TC

config UTEST_HWTIMER_DEV_NAME
string "Hardware timer device name"
default "timer0"
depends on RT_USING_HWTIMER && UTEST_SYS_PERF_TC
help
Specify the hardware timer device name used for context switch testing (e.g., timer0).

config UTEST_SYS_IRQ_LATENCY
bool "system IRQ LATENCY test"
default n
depends on RT_USING_HWTIMER && UTEST_SYS_PERF_TC

endmenu
12 changes: 12 additions & 0 deletions examples/utest/testcases/perf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# testcases 说明

## 一、测试用例说明

| 文件 | 说明 |
|--------|--------|
| context_switch.c | 上下文切换测试代码 |
| irq_latency.c | 中断延时测试代码 |
| rt_perf_thread_event.c | 线程事件性能测试 |
| rt_perf_thread_mbox.c | 线程邮箱性能测试 |
| rt_perf_thread_mq.c | 线程消息队列性能测试 |
| rt_perf_thread_sem.c | 线程信号量性能测试 |
10 changes: 10 additions & 0 deletions examples/utest/testcases/perf/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Import('rtconfig')
from building import *

cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]

group = DefineGroup('utestcases', src, depend = ['UTEST_SYS_PERF_TC'], CPPPATH = CPPPATH)

Return('group')
106 changes: 106 additions & 0 deletions examples/utest/testcases/perf/context_switch_tc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-07-03 rcitach test case for context_switch
*/

#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include <utest.h>
#include <utest_assert.h>
#include <perf_tc.h>

static rt_sem_t sem1, sem2;
static rt_sem_t complete_sem = RT_NULL;

static void local_modify_time(rt_perf_t *perf)
{
if(perf)
perf->real_time = perf->real_time - perf->tmp_time;
}

static void perf_thread_event1(void *parameter)
{
while (1)
{
rt_sem_take(sem1, RT_WAITING_FOREVER);
rt_sem_release(sem2);
}
}

static void perf_thread_event2(void *parameter)
{
rt_perf_t *perf = (rt_perf_t *)parameter;

for (rt_uint32_t i = 0; i < UTEST_SYS_PERF_TC_COUNT; i++)
{
perf->tmp_time = 0;
rt_perf_start(perf);
rt_sem_take(sem2, RT_WAITING_FOREVER);
rt_sem_release(sem2);
rt_perf_stop(perf);

rt_mutex_take(perf->lock,RT_WAITING_FOREVER);
perf->count -= 1;
perf->tmp_time = perf->real_time;
rt_mutex_release(perf->lock);

rt_perf_start(perf);
rt_sem_take(sem2, RT_WAITING_FOREVER);
rt_sem_release(sem1);
rt_perf_stop(perf);
}
rt_sem_release(complete_sem);
}

rt_err_t context_switch_test(rt_perf_t *perf)
{
rt_thread_t thread1 = RT_NULL;
rt_thread_t thread2 = RT_NULL;

# if __STDC_VERSION__ >= 199901L
rt_strcpy(perf->name,__func__);
#else
rt_strcpy(perf->name,"context_switch_test");
#endif

perf->local_modify = local_modify_time;
sem1 = rt_sem_create("sem1", 1, RT_IPC_FLAG_FIFO);
sem2 = rt_sem_create("sem2", 0, RT_IPC_FLAG_FIFO);
complete_sem = rt_sem_create("complete_sem", 0, RT_IPC_FLAG_FIFO);

thread1 = rt_thread_create("perf_thread_event1", perf_thread_event1, perf,
THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
if (thread1 == RT_NULL)
{
LOG_E("perf_thread_event1 create failed.");
return -RT_ERROR;
}

thread2 = rt_thread_create("perf_thread_event2", perf_thread_event2, perf,
THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
if (thread2 == RT_NULL)
{
LOG_E("perf_thread_event2 create failed.");
return -RT_ERROR;
}

rt_thread_startup(thread1);
rt_thread_startup(thread2);

rt_sem_take(complete_sem, RT_WAITING_FOREVER);

rt_perf_dump(perf);
rt_thread_delete(thread1);
rt_sem_delete(complete_sem);
rt_sem_delete(sem1);
rt_sem_delete(sem2);

return RT_EOK;
}

80 changes: 80 additions & 0 deletions examples/utest/testcases/perf/irq_latency_tc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-07-03 rcitach test case for irq latency
*/

#include <rtthread.h>
#include <rtdevice.h>
#include <utest.h>
#include <utest_assert.h>
#include <perf_tc.h>

static rt_device_t hw_dev = RT_NULL;
static rt_sem_t complete_sem = RT_NULL;
static rt_hwtimerval_t timeout = {0};
static rt_perf_t *perf_local = RT_NULL;

static void modify_time(rt_perf_t *perf)
{
if(perf)
perf->real_time = perf->real_time - perf->tmp_time;
}

static rt_err_t timer_callback(rt_device_t dev, rt_size_t size)
{
rt_perf_stop(perf_local);
if (perf_local->count >= UTEST_SYS_PERF_TC_COUNT)
{
rt_sem_release(complete_sem);
return RT_EOK;
}
rt_perf_start_impl(perf_local, &timeout);
return RT_EOK;
}

rt_err_t rt_perf_irq_latency(rt_perf_t *perf)
{
# if __STDC_VERSION__ >= 199901L
rt_strcpy(perf->name,__func__);
#else
rt_strcpy(perf->name,"rt_perf_irq_latency");
#endif
int ret = RT_EOK;
rt_hwtimer_mode_t mode = HWTIMER_MODE_PERIOD;

perf_local = perf;
hw_dev = rt_device_find(UTEST_HWTIMER_DEV_NAME);
if (hw_dev == RT_NULL)
{
ret = RT_ERROR;
LOG_E("hwtimer sample run failed! can't find %s device!", UTEST_HWTIMER_DEV_NAME);
return ret;
}

complete_sem = rt_sem_create("complete", 0, RT_IPC_FLAG_FIFO);
timeout.sec = 0;
timeout.usec = 50; /* No modification is necessary here, use the fixed value */

rt_mutex_take(perf->lock,RT_WAITING_FOREVER);
perf_local->tmp_time = (rt_uint32_t)(timeout.sec * 1000000u + timeout.usec);
perf_local->local_modify = modify_time;
rt_mutex_release(perf->lock);

rt_device_set_rx_indicate(hw_dev, timer_callback);
rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, (void *)&mode);

rt_perf_start_impl(perf_local, &timeout);

rt_sem_take(complete_sem, RT_WAITING_FOREVER);
rt_perf_dump(perf_local);
rt_sem_delete(complete_sem);
rt_device_close(hw_dev);

return RT_EOK;
}

Loading