touched up the xma tests

This commit is contained in:
hyung-hwan 2025-06-14 23:27:55 +09:00
parent 094010557d
commit 9d18c2ce2a
2 changed files with 38 additions and 27 deletions

View File

@ -6,13 +6,12 @@
#include "tap.h" #include "tap.h"
#define NUM_ITERATIONS 1000000 #define NUM_ITERATIONS 1000000
#define MIN_ALLOC_SIZE 16 // Minimum size of allocation in bytes #define MIN_ALLOC_SIZE 16
#define MAX_ALLOC_SIZE 1024 // Maximum size of allocation in bytes #define MAX_ALLOC_SIZE 1024
#define OK_X(test) OK(test, #test) #define OK_X(test) OK(test, #test)
static size_t random_size()
size_t random_size()
{ {
return MIN_ALLOC_SIZE + rand() % (MAX_ALLOC_SIZE - MIN_ALLOC_SIZE + 1); return MIN_ALLOC_SIZE + rand() % (MAX_ALLOC_SIZE - MIN_ALLOC_SIZE + 1);
} }
@ -22,22 +21,22 @@ int main()
int test_bad = 0; int test_bad = 0;
hawk_mmgr_t xma_mmgr; hawk_mmgr_t xma_mmgr;
clock_t start_time, end_time;
double malloc_time = 0.0, free_time = 0.0;
void **ptr_array;
no_plan(); no_plan();
hawk_init_xma_mmgr(&xma_mmgr, NUM_ITERATIONS * MAX_ALLOC_SIZE); hawk_init_xma_mmgr(&xma_mmgr, NUM_ITERATIONS * MAX_ALLOC_SIZE);
srand((unsigned int)time(NULL)); // Seed RNG srand((unsigned int)time(NULL));
clock_t start_time, end_time; ptr_array = malloc(NUM_ITERATIONS * sizeof(void *));
double malloc_time = 0.0, free_time = 0.0;
void **ptr_array = malloc(NUM_ITERATIONS * sizeof(void *));
OK_X (ptr_array != NULL); OK_X (ptr_array != NULL);
if (!ptr_array) { if (!ptr_array) {
fprintf(stderr, "malloc failed for pointer array\n"); fprintf(stderr, "malloc failed for pointer array\n");
return -1; return -1;
} }
// Benchmark malloc
start_time = clock(); start_time = clock();
for (size_t i = 0; i < NUM_ITERATIONS; ++i) { for (size_t i = 0; i < NUM_ITERATIONS; ++i) {
size_t size = random_size(); size_t size = random_size();
@ -56,7 +55,6 @@ int main()
end_time = clock(); end_time = clock();
malloc_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; malloc_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
// Benchmark free
start_time = clock(); start_time = clock();
for (size_t i = 0; i < NUM_ITERATIONS; ++i) { for (size_t i = 0; i < NUM_ITERATIONS; ++i) {
/*free(ptr_array[i]);*/ /*free(ptr_array[i]);*/
@ -65,7 +63,7 @@ int main()
end_time = clock(); end_time = clock();
free_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; free_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
free(ptr_array); // Free the pointer array itself free(ptr_array);
printf("Performed %d allocations and frees\n", NUM_ITERATIONS); printf("Performed %d allocations and frees\n", NUM_ITERATIONS);
printf("Total malloc time: %.6f seconds\n", malloc_time); printf("Total malloc time: %.6f seconds\n", malloc_time);

View File

@ -18,7 +18,16 @@ typedef struct {
size_t size; size_t size;
} Allocation; } Allocation;
size_t random_size()
static void print_xma (void* ctx, const hawk_bch_t* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
static size_t random_size()
{ {
return MIN_ALLOC_SIZE + rand() % (MAX_ALLOC_SIZE - MIN_ALLOC_SIZE + 1); return MIN_ALLOC_SIZE + rand() % (MAX_ALLOC_SIZE - MIN_ALLOC_SIZE + 1);
} }
@ -28,17 +37,16 @@ int main()
int test_bad = 0; int test_bad = 0;
hawk_mmgr_t xma_mmgr; hawk_mmgr_t xma_mmgr;
no_plan(); Allocation allocations[MAX_ALLOCATIONS] = {0}; /* pool of active allocations */
hawk_init_xma_mmgr(&xma_mmgr, NUM_OPERATIONS * MAX_ALLOC_SIZE);
srand((unsigned int)time(NULL));
Allocation allocations[MAX_ALLOCATIONS] = {0}; // pool of active allocations
size_t num_active = 0; size_t num_active = 0;
clock_t start_time, end_time; clock_t start_time, end_time;
double malloc_time = 0.0, free_time = 0.0; double malloc_time = 0.0, free_time = 0.0;
no_plan();
hawk_init_xma_mmgr(&xma_mmgr, NUM_OPERATIONS * MAX_ALLOC_SIZE);
srand((unsigned int)time(NULL));
start_time = clock(); start_time = clock();
for (size_t i = 0; i < NUM_OPERATIONS; ++i) for (size_t i = 0; i < NUM_OPERATIONS; ++i)
@ -46,12 +54,12 @@ int main()
int do_alloc = (num_active == 0) || (rand() % 2 == 0 && num_active < MAX_ALLOCATIONS); int do_alloc = (num_active == 0) || (rand() % 2 == 0 && num_active < MAX_ALLOCATIONS);
if (do_alloc) { if (do_alloc) {
// Allocate
size_t size = random_size(); size_t size = random_size();
//void *ptr = malloc(size); /*void *ptr = malloc(size);*/
void *ptr = HAWK_MMGR_ALLOC(&xma_mmgr, size); void *ptr = HAWK_MMGR_ALLOC(&xma_mmgr, size);
if (!ptr) { if (!ptr) {
fprintf(stderr, "malloc failed at operation %zu\n", i); fprintf(stderr, "malloc failed at operation %zu\n", i);
test_bad = 1;
break; break;
} }
@ -59,29 +67,34 @@ int main()
allocations[num_active].size = size; allocations[num_active].size = size;
++num_active; ++num_active;
} else { } else {
// Free a random active allocation /* Free a random active allocation */
size_t index = rand() % num_active; size_t index = rand() % num_active;
void *ptr_to_free = allocations[index].ptr; void *ptr_to_free = allocations[index].ptr;
clock_t t1 = clock(); clock_t t1 = clock();
//free(ptr_to_free); /*free(ptr_to_free); */
HAWK_MMGR_FREE(&xma_mmgr, ptr_to_free); HAWK_MMGR_FREE(&xma_mmgr, ptr_to_free);
clock_t t2 = clock(); clock_t t2 = clock();
free_time += (double)(t2 - t1) / CLOCKS_PER_SEC; free_time += (double)(t2 - t1) / CLOCKS_PER_SEC;
// Replace with last active allocation /* Replace with last active allocation */
allocations[index] = allocations[num_active - 1]; allocations[index] = allocations[num_active - 1];
--num_active; --num_active;
} }
} }
OK_X(test_bad == 0);
// Free remaining allocations /* hawk_xma_dump(xma_mmgr.ctx, print_xma, HAWK_NULL); */
/* Free remaining allocations */
for (size_t i = 0; i < num_active; ++i) { for (size_t i = 0; i < num_active; ++i) {
//free(allocations[i].ptr); /* free(allocations[i].ptr); */
HAWK_MMGR_FREE(&xma_mmgr, allocations[i].ptr); HAWK_MMGR_FREE(&xma_mmgr, allocations[i].ptr);
} }
end_time = clock(); end_time = clock();
hawk_xma_dump(xma_mmgr.ctx, print_xma, HAWK_NULL);
malloc_time = (double)(end_time - start_time) / CLOCKS_PER_SEC - free_time; malloc_time = (double)(end_time - start_time) / CLOCKS_PER_SEC - free_time;
printf("Performed %d interleaved malloc/free operations\n", NUM_OPERATIONS); printf("Performed %d interleaved malloc/free operations\n", NUM_OPERATIONS);
@ -89,6 +102,6 @@ int main()
printf("Total free time : %.6f seconds\n", free_time); printf("Total free time : %.6f seconds\n", free_time);
printf("Average time per operation : %.9f seconds\n", (malloc_time + free_time) / NUM_OPERATIONS); printf("Average time per operation : %.9f seconds\n", (malloc_time + free_time) / NUM_OPERATIONS);
return 0; return exit_status();
} }