Add timings to memory test

This commit is contained in:
Rickey Fehr
2025-10-18 17:22:39 -07:00
parent ab07c9af04
commit a5c5f9cb15
+73
View File
@@ -6,6 +6,22 @@
#include <stdlib.h>
#include <time.h>
typedef struct timespec timestamp_t;
timestamp_t get_time(void)
{
timestamp_t t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t;
}
void print_time_diff(timestamp_t start, timestamp_t end)
{
int64_t diff_nsec = end.tv_nsec - start.tv_nsec;
printf("Elapsed: %ld ns\n", diff_nsec);
}
int get_random(int low, int high)
{
srand(time(NULL));
@@ -76,15 +92,72 @@ bool test_fill_and_remove_at_random_and_refill_and_empty(void)
int main(void)
{
// Test it twice to make sure empty works, kinda hacky.
printf("Testing Pool Fill and Empty 1x.\n");
if(!test_fill_and_empty()) return -1;
printf("Testing Pool Fill and Empty 2x.\n");
if(!test_fill_and_empty()) return -1;
// Similarly here, verify that fill, random num removal, refill, and empty
// by refilling and emptying again
printf("Testing Pool Fill, Partial Empty, Refill, Empty.\n");
if(!test_fill_and_remove_at_random_and_refill_and_empty()) return -1;
printf("Testing Pool Fill and Empty.\n");
if(!test_fill_and_empty()) return -1;
printf("---------------------------------------------------------\n");
printf("Pool Tests Passed\n");
printf("---------------------------------------------------------\n");
printf("Testing execution time for fun :)\n\n");
timestamp_t t1 = get_time();
ChunkOfData* myData = POOL_GET(ChunkOfData);
timestamp_t t2 = get_time();
printf("Pool Get One:\n\t");
print_time_diff(t1, t2);
printf("\n");
t1 = get_time();
POOL_FREE(ChunkOfData, myData);
t2 = get_time();
printf("Pool Free One:\n\t");
print_time_diff(t1, t2);
printf("\n");
t1 = get_time();
myData = (ChunkOfData*)malloc(sizeof(ChunkOfData));
t2 = get_time();
printf("Malloc One:\n\t");
print_time_diff(t1, t2);
printf("\n");
t1 = get_time();
free(myData);
t2 = get_time();
printf("Free One:\n\t");
print_time_diff(t1, t2);
printf("\n");
t1 = get_time();
if(!test_fill_and_empty()) return -1;
t2 = get_time();
printf("Fill and Empty Pool %d times:\n\t", TEST_SIZE);
print_time_diff(t1, t2);
printf("\n");
t1 = get_time();
ChunkOfData* myPtrs[TEST_SIZE];
for(int i = 0; i < TEST_SIZE; i++)
{
myPtrs[i] = (ChunkOfData*)malloc(sizeof(ChunkOfData));
}
for(int i = 0; i < TEST_SIZE; i++)
{
free(myPtrs[i]);
}
t2 = get_time();
printf("Fill and Empty Malloc %d times:\n\t", TEST_SIZE);
print_time_diff(t1, t2);
return 0;
}