From 73774c3981208b63cce223e2f816c1f509099ff5 Mon Sep 17 00:00:00 2001 From: Thomas Strauss Date: Sat, 21 Dec 2024 12:54:15 +0100 Subject: [PATCH] Add second perf test for dynamic alloc --- acu_test/acu_perf.c | 10 +++- acu_test/acu_perf.h | 2 +- cutest.lib/perf/palc_tst.c | 109 +++++++++++++++++++++++++++---------- cutest.lib/perf/pmsc_tst.c | 6 +- 4 files changed, 90 insertions(+), 37 deletions(-) diff --git a/acu_test/acu_perf.c b/acu_test/acu_perf.c index f8b64ba..0ae62a1 100644 --- a/acu_test/acu_perf.c +++ b/acu_test/acu_perf.c @@ -22,14 +22,18 @@ #include "acu_perf.h" #include -unsigned long acu_measureLoop(TestFunc* func, clock_t duration, void* context) { +unsigned long acu_measureLoop(TestFunc* func, clock_t duration, int disableCache, void* context) { unsigned long count = 0; clock_t end = clock() + duration; - acu_disableCache(); + if (disableCache) { + acu_disableCache(); + } while (clock() < end) { count++; func(context); } - acu_enableCache(); + if (disableCache) { + acu_enableCache(); + } return count; } \ No newline at end of file diff --git a/acu_test/acu_perf.h b/acu_test/acu_perf.h index d93e7a0..077a0c1 100644 --- a/acu_test/acu_perf.h +++ b/acu_test/acu_perf.h @@ -30,6 +30,6 @@ typedef void TestFunc(void* context); -__EXPORT unsigned long acu_measureLoop(TestFunc* func, clock_t duration, void* context); +__EXPORT unsigned long acu_measureLoop(TestFunc* func, clock_t duration, int disableCache, void* context); #endif diff --git a/cutest.lib/perf/palc_tst.c b/cutest.lib/perf/palc_tst.c index b69897a..379debf 100644 --- a/cutest.lib/perf/palc_tst.c +++ b/cutest.lib/perf/palc_tst.c @@ -33,15 +33,6 @@ #include #include -static void voidFunc(void* context) { - int i; - for (i = 0; i < 100; i++) { - } - for (i = 0; i < 100; i++) { - } - UNUSED(context); -} - typedef struct AllocContext_ { char** buffer; ACU_StaticAllocator* allocator; @@ -64,37 +55,45 @@ static void allocFunc(void* context) { UNUSED(context); } -static void acu_allocStaticAllocatorFunc(void* context) { + +static void acu_emallocFunc(void* context) { int i; AllocContext* c = context; for (i = 0; i < 100; i++) { - c->buffer[i] = acu_allocStaticAllocator(c->allocator); + c->buffer[i] = acu_emalloc(10); } for (i = 0; i < 100; i++) { - acu_freeStaticAllocator(c->buffer[i]); + acu_free(c->buffer[i]); } UNUSED(context); } -static void acu_allocDynamicAllocatorFunc(void* context) { - int i; - AllocDynamicContext* c = (AllocDynamicContext*) context; - for (i = 0; i < 100; i++) { - c->buffer[i] = acu_allocAllocator(c->allocator); - } - for (i = 0; i < 100; i++) { - acu_freeAllocator(c->buffer[i]); - } +static void allocPerformanceTest(ACU_ExecuteEnv* environment, const void* context) { + + char* buffer[100]; + AllocContext c; + c.buffer = buffer; + c.allocator = NULL; + + printf("\n\r"); + +#define DIVISOR 3 + printf("allocFunc\t%ld\n\r", (acu_measureLoop(allocFunc, CLK_TCK / DIVISOR, 0, &c)) * DIVISOR); + printf("acu_emallocFunc\t%ld\n\r", (acu_measureLoop(acu_emallocFunc, CLK_TCK / DIVISOR, 0, &c)) * DIVISOR); +#undef DIVISOR + + UNUSED(environment); + UNUSED(context); } -static void acu_emallocFunc(void* context) { +static void acu_allocStaticAllocatorFunc(void* context) { int i; AllocContext* c = context; for (i = 0; i < 100; i++) { - c->buffer[i] = acu_emalloc(10); + c->buffer[i] = acu_allocStaticAllocator(c->allocator); } for (i = 0; i < 100; i++) { - acu_free(c->buffer[i]); + acu_freeStaticAllocator(c->buffer[i]); } UNUSED(context); } @@ -107,13 +106,10 @@ static void staticAllocPerformanceTest(ACU_ExecuteEnv* environment, const void* c.buffer = buffer; c.allocator = &allocator; - acu_initStaticAllocator(&allocator, 10, 100, (ACU_FreeContext*) NULL); + acu_initStaticAllocator(&allocator, 10, 100, (ACU_FreeContext*)NULL); #define DIVISOR 3 - printf("voidFunc\t%ld\n\r", (acu_measureLoop(voidFunc, CLK_TCK / DIVISOR, &c)) * DIVISOR); - printf("allocFunc\t%ld\n\r", (acu_measureLoop(allocFunc, CLK_TCK / DIVISOR, &c)) * DIVISOR); - printf("acu_emallocFunc\t%ld\n\r", (acu_measureLoop(acu_emallocFunc, CLK_TCK / DIVISOR, &c)) * DIVISOR); - printf("acu_allocStaticAllocator\t%ld\n\r", (acu_measureLoop(acu_allocStaticAllocatorFunc, CLK_TCK / DIVISOR, &c)) * DIVISOR); + printf("acu_allocStaticAllocator\t%ld\n\r", (acu_measureLoop(acu_allocStaticAllocatorFunc, CLK_TCK / DIVISOR, 0, &c)) * DIVISOR); #undef DIVISOR acu_destroyStaticAllocator(&allocator); @@ -122,6 +118,17 @@ static void staticAllocPerformanceTest(ACU_ExecuteEnv* environment, const void* UNUSED(context); } +static void acu_allocDynamicAllocatorFunc(void* context) { + int i; + AllocDynamicContext* c = (AllocDynamicContext*)context; + for (i = 0; i < 100; i++) { + c->buffer[i] = acu_allocAllocator(c->allocator); + } + for (i = 0; i < 100; i++) { + acu_freeAllocator(c->buffer[i]); + } +} + static void dynamicAllocPerformanceTest(ACU_ExecuteEnv* environment, const void* context) { char* buffer[100]; @@ -133,7 +140,46 @@ static void dynamicAllocPerformanceTest(ACU_ExecuteEnv* environment, const void* acu_initAllocator(&allocator, 10, 101); #define DIVISOR 3 - printf("acu_allocDynamicAllocator\t%ld\n\r", (acu_measureLoop(acu_allocDynamicAllocatorFunc, CLK_TCK / DIVISOR, &c)) * DIVISOR); + printf("acu_allocDynamicAllocator\t%ld\n\r", (acu_measureLoop(acu_allocDynamicAllocatorFunc, CLK_TCK / DIVISOR, 0, &c)) * DIVISOR); +#undef DIVISOR + + acu_destroyAllocator(&allocator); + + UNUSED(environment); + UNUSED(context); +} + +static void acu_randomAllocAllocatorFunc(void* context) { + int i; + AllocDynamicContext* c = context; + char** buffer1 = &(c->buffer[0]); + + for (i = 1; i < 100; i++) { + acu_freeAllocator(buffer1[0]); + acu_freeAllocator(buffer1[i]); + buffer1[i] = acu_allocAllocator(c->allocator); + buffer1[0] = acu_allocAllocator(c->allocator); + } + UNUSED(context); +} + +static void randomAllocPerformanceTest(ACU_ExecuteEnv* environment, const void* context) { + + char* buffer[100]; + ACU_DynamicAllocator allocator; + AllocDynamicContext c; + int i; + c.buffer = buffer; + c.allocator = &allocator; + + acu_initAllocator(&allocator, 10, 50); + + for (i = 0; i < 100; i++) { + buffer[i] = acu_allocAllocator(&allocator); + } + +#define DIVISOR 3 + printf("acu_randomAllocAllocatorFunc\t%ld\n\r", (acu_measureLoop(acu_randomAllocAllocatorFunc, CLK_TCK / DIVISOR, 0, &c)) * DIVISOR); #undef DIVISOR acu_destroyAllocator(&allocator); @@ -142,13 +188,16 @@ static void dynamicAllocPerformanceTest(ACU_ExecuteEnv* environment, const void* UNUSED(context); } + ACU_Fixture* allocPerformanceFixture(void) { ACU_Fixture* fixture = acu_mallocFixture(); acu_initFixture(fixture, "alloc performance Tests"); + acu_addTestCase(fixture, "allocPerformanceTest", allocPerformanceTest); acu_addTestCase(fixture, "staticAllocPerformanceTest", staticAllocPerformanceTest); acu_addTestCase(fixture, "dynamicAllocPerformanceTest", dynamicAllocPerformanceTest); + acu_addTestCase(fixture, "randomAllocPerformanceTest", randomAllocPerformanceTest); return fixture; } diff --git a/cutest.lib/perf/pmsc_tst.c b/cutest.lib/perf/pmsc_tst.c index 8eb3b9d..5d43b33 100644 --- a/cutest.lib/perf/pmsc_tst.c +++ b/cutest.lib/perf/pmsc_tst.c @@ -56,9 +56,9 @@ static void acu_strlenFunc(void* context) { static void strlenPerformanceTest(ACU_ExecuteEnv* environment, const void* context) { #define DIVISOR 3 - printf("voidFunc\t%ld\n\r", (acu_measureLoop(voidFunc, CLK_TCK / DIVISOR, NULL)) * DIVISOR); - printf("strLenFunc\t%ld\n\r", (acu_measureLoop(strLenFunc, CLK_TCK / DIVISOR, NULL)) * DIVISOR); - printf("acu_strlenFunc\t%ld\n\r", (acu_measureLoop(acu_strlenFunc, CLK_TCK / DIVISOR, NULL)) * DIVISOR); + printf("voidFunc\t%ld\n\r", (acu_measureLoop(voidFunc, CLK_TCK / DIVISOR, 0, NULL)) * DIVISOR); + printf("strLenFunc\t%ld\n\r", (acu_measureLoop(strLenFunc, CLK_TCK / DIVISOR, 0, NULL)) * DIVISOR); + printf("acu_strlenFunc\t%ld\n\r", (acu_measureLoop(acu_strlenFunc, CLK_TCK / DIVISOR, 0, NULL)) * DIVISOR); #undef DIVISOR UNUSED(environment);