Skip to content

Commit

Permalink
Add second perf test for dynamic alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
thstrauss committed Dec 21, 2024
1 parent e24409f commit 73774c3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 37 deletions.
10 changes: 7 additions & 3 deletions acu_test/acu_perf.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
#include "acu_perf.h"
#include <acu_cach.h>

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;
}
2 changes: 1 addition & 1 deletion acu_test/acu_perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
109 changes: 79 additions & 30 deletions cutest.lib/perf/palc_tst.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@
#include <stdio.h>
#include <time.h>

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;
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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;
}
6 changes: 3 additions & 3 deletions cutest.lib/perf/pmsc_tst.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 73774c3

Please sign in to comment.