mirror of
https://github.com/espressif/esp-idf.git
synced 2025-07-30 18:57:19 +02:00
test(freertos): Fixed flaky scheduling time test
This commit updates the scheduling time test to test for median scheduling time values instead of average values.
This commit is contained in:
@ -14,12 +14,26 @@
|
|||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
#include "test_utils.h"
|
#include "test_utils.h"
|
||||||
|
|
||||||
#define NUMBER_OF_ITERATIONS 10
|
#define NUMBER_OF_ITERATIONS 1023
|
||||||
|
|
||||||
|
static int compare_uint32(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return (*(uint32_t *)a - * (uint32_t *)b);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t calculate_median(uint32_t *values, int size)
|
||||||
|
{
|
||||||
|
/* Sort the array of values containing the scheduling time of each iteration */
|
||||||
|
qsort(values, size, sizeof(uint32_t), compare_uint32);
|
||||||
|
|
||||||
|
/* Return the median value. (size is an odd number) */
|
||||||
|
return values[size / 2];
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
SemaphoreHandle_t end_sema;
|
SemaphoreHandle_t end_sema;
|
||||||
uint32_t before_sched;
|
uint32_t before_sched;
|
||||||
uint32_t cycles_to_sched;
|
uint32_t cycles_to_sched[NUMBER_OF_ITERATIONS];
|
||||||
TaskHandle_t t1_handle;
|
TaskHandle_t t1_handle;
|
||||||
} test_context_t;
|
} test_context_t;
|
||||||
|
|
||||||
@ -36,20 +50,18 @@ static void test_task_1(void *arg) {
|
|||||||
|
|
||||||
static void test_task_2(void *arg) {
|
static void test_task_2(void *arg) {
|
||||||
test_context_t *context = (test_context_t *)arg;
|
test_context_t *context = (test_context_t *)arg;
|
||||||
uint64_t accumulator = 0;
|
|
||||||
|
|
||||||
vTaskPrioritySet(NULL, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
|
vTaskPrioritySet(NULL, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
|
||||||
vTaskPrioritySet(context->t1_handle, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
|
vTaskPrioritySet(context->t1_handle, CONFIG_UNITY_FREERTOS_PRIORITY + 1);
|
||||||
vPortYield();
|
vPortYield();
|
||||||
|
|
||||||
for(int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
|
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
|
||||||
accumulator += (portGET_RUN_TIME_COUNTER_VALUE() - context->before_sched);
|
context->cycles_to_sched[i] = portGET_RUN_TIME_COUNTER_VALUE() - context->before_sched;
|
||||||
vPortYield();
|
vPortYield();
|
||||||
}
|
}
|
||||||
|
|
||||||
context->cycles_to_sched = accumulator / NUMBER_OF_ITERATIONS;
|
|
||||||
vTaskDelete(context->t1_handle);
|
|
||||||
xSemaphoreGive(context->end_sema);
|
xSemaphoreGive(context->end_sema);
|
||||||
|
vTaskDelete(context->t1_handle);
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,5 +82,11 @@ TEST_CASE("scheduling time test", "[freertos]")
|
|||||||
|
|
||||||
BaseType_t result = xSemaphoreTake(context.end_sema, portMAX_DELAY);
|
BaseType_t result = xSemaphoreTake(context.end_sema, portMAX_DELAY);
|
||||||
TEST_ASSERT_EQUAL_HEX32(pdTRUE, result);
|
TEST_ASSERT_EQUAL_HEX32(pdTRUE, result);
|
||||||
TEST_PERFORMANCE_LESS_THAN(SCHEDULING_TIME , "%d cycles" ,context.cycles_to_sched);
|
|
||||||
|
/* We calculate the median instead of the average to avoid outliers which may occur due to cache speed variabilities */
|
||||||
|
uint32_t median_cycles = calculate_median(context.cycles_to_sched, NUMBER_OF_ITERATIONS);
|
||||||
|
TEST_PERFORMANCE_LESS_THAN(SCHEDULING_TIME, "%d cycles", median_cycles);
|
||||||
|
|
||||||
|
/* Cleanup */
|
||||||
|
vSemaphoreDelete(context.end_sema);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user