Wrote a benchmark to test TOY_BUCKET_IDEAL sizes

This commit is contained in:
2024-12-18 00:40:17 +11:00
parent 04c799954c
commit 8b5cc3b493
3 changed files with 72 additions and 58 deletions

View File

@@ -6,11 +6,11 @@
#include "toy_print.h" #include "toy_print.h"
//basic structures //basic structures
#include "toy_bucket.h"
#include "toy_string.h"
#include "toy_value.h" #include "toy_value.h"
#include "toy_array.h" #include "toy_array.h"
#include "toy_stack.h" #include "toy_stack.h"
#include "toy_bucket.h"
#include "toy_string.h"
#include "toy_table.h" #include "toy_table.h"
//IR structures and other components //IR structures and other components

View File

@@ -1,60 +1,78 @@
#include "toy_table.h" #include "toy.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
//utils //generate an immense series of Toy_String instances to fill the buckets, thrn compare the time taken for each possible vale of TOY_BUCKET_IDEAL
unsigned int hashUInt(unsigned int x) {
static unsigned int hash(unsigned int x) {
x = ((x >> 16) ^ x) * 0x45d9f3b; x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b; x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x; x = ((x >> 16) ^ x);
return x; return x;
} }
void stress_inserts(unsigned int seed, unsigned int iterations, unsigned int limit) { static unsigned int seed = 42;
//randomly generate a series of key-value pairs (from a seed) and insert them
{
//setup
Toy_Table* table = Toy_allocateTable();
for (unsigned int i = 0; i < iterations; i++) { static unsigned int rng() {
//next seed return seed = hash(seed);
seed = hashUInt(seed);
//don't exceed a certain number of entries
unsigned int masked = seed & (limit-1); //lol
//actual values don't matter, as long as they can be recreated
Toy_Value key = TOY_VALUE_FROM_INTEGER(masked);
Toy_Value value = TOY_VALUE_FROM_INTEGER(masked);
Toy_insertTable(&table, key, value);
} }
//cleanup #define MAX 9
Toy_freeTable(table); const char* samples[] = { //9 entries
"the",
"quick",
"brown",
"fox",
"jumped",
"over",
"the",
"lazy",
"dog",
};
void stress_fillBucket(Toy_Bucket** bucketHandle) {
for (unsigned int i = 0; i < 10000000; i++) {
//create some leaf and node strings
Toy_String* a = Toy_createString(bucketHandle, samples[rng() % MAX]);
Toy_String* b = Toy_createString(bucketHandle, samples[rng() % MAX]);
Toy_String* c = Toy_createString(bucketHandle, samples[rng() % MAX]);
Toy_String* d = Toy_createString(bucketHandle, samples[rng() % MAX]);
Toy_String* l = Toy_concatStrings(bucketHandle, a, b);
Toy_String* r = Toy_concatStrings(bucketHandle, c, d);
Toy_concatStrings(bucketHandle, l, r);
// char* buffer = Toy_getStringRawBuffer(s);
// printf("%s\n", buffer);
// free(buffer);
} }
} }
int main(int argc, char* argv[]) { static unsigned long long int measureDepth(Toy_Bucket* bucket) {
if (argc != 3) { return bucket == NULL ? 0 : 1 + measureDepth(bucket->next);
printf("Usage: %s iterations limit\n", argv[0]);
return 0;
} }
unsigned int iterations = 0; static unsigned long long int measureCapacity(Toy_Bucket* bucket) {
unsigned int limit = 0; return bucket == NULL ? 0 : bucket->capacity + measureCapacity(bucket->next);
sscanf(argv[1], "%u", &iterations);
sscanf(argv[2], "%u", &limit);
//limit to 16mb
if (limit * sizeof(Toy_TableEntry) > (1024 * 1024 * 16)) {
printf("Error: limit must be below %u for safety reasons\n", (1024 * 1024 * 16)/sizeof(Toy_TableEntry));
return 0;
} }
//run the stress test static unsigned long long int measureCount(Toy_Bucket* bucket) {
stress_inserts(42, iterations, limit); return bucket == NULL ? 0 : bucket->count + measureCount(bucket->next);
}
int main() {
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
stress_fillBucket(&bucket);
unsigned long long int depth = measureDepth(bucket);
unsigned long long int capacity = measureCapacity(bucket);
unsigned long long int count = measureCount(bucket);
printf(TOY_CC_FONT_RED TOY_CC_BACK_YELLOW "Result: %u: %llu, %llu, %llu" TOY_CC_RESET "\n", TOY_BUCKET_IDEAL, depth, capacity, count);
Toy_freeBucket(&bucket);
return 0; return 0;
} }

View File

@@ -31,21 +31,18 @@ TEST_CASESFILES=$(wildcard $(TEST_CASESDIR)/bench_*.c)
#build the object files, compile the test cases, and run #build the object files, compile the test cases, and run
all: clean all: clean
$(MAKE) TOY_BUCKET_IDEAL=16372 all-override
$(MAKE) TOY_BUCKET_IDEAL=32756 all-override
$(MAKE) TOY_BUCKET_IDEAL=65524 all-override
$(MAKE) TOY_BUCKET_IDEAL=131060 all-override
$(MAKE) TOY_BUCKET_IDEAL=262132 all-override
all-override: clean
$(MAKE) build-source $(MAKE) build-source
$(MAKE) build-cases $(MAKE) build-cases
$(MAKE) build-link $(MAKE) build-link
$(MAKE) build-run $(MAKE) build-run
all-override: clean
$(MAKE) TEST_SOURCEFILES='$(subst $(TEST_SOURCEDIR)/$(OVERRIDE),$(OVERRIDE),$(TEST_SOURCEFILES))' build-source-override
$(MAKE) build-cases
$(MAKE) build-link
$(MAKE) build-run
.PHONY: build-source-override
build-source-override: $(TEST_OUTDIR) $(TEST_OBJDIR) $(addprefix $(TEST_OBJDIR)/,$(notdir $(TEST_SOURCEFILES:.c=.o)))
$(CC) -c -o $(TEST_OBJDIR)/$(OVERRIDE:.c=.o) $(OVERRIDE) $(addprefix -I,$(TEST_SOURCEDIR)) $(CFLAGS) -fdata-sections -ffunction-sections
#targets for each step #targets for each step
.PHONY: build-source .PHONY: build-source
build-source: $(TEST_OUTDIR) $(TEST_OBJDIR) $(addprefix $(TEST_OBJDIR)/,$(notdir $(TEST_SOURCEFILES:.c=.o))) build-source: $(TEST_OUTDIR) $(TEST_OBJDIR) $(addprefix $(TEST_OBJDIR)/,$(notdir $(TEST_SOURCEFILES:.c=.o)))
@@ -61,19 +58,18 @@ build-run: $(addprefix $(TEST_OUTDIR)/,$(notdir $(TEST_CASESFILES:%.c=%.exe))) $
#compilation steps #compilation steps
$(TEST_OBJDIR)/%.o: $(TEST_SOURCEDIR)/%.c $(TEST_OBJDIR)/%.o: $(TEST_SOURCEDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(TEST_SOURCEDIR)) $(CFLAGS) -fdata-sections -ffunction-sections $(CC) -DTOY_BUCKET_IDEAL=$(TOY_BUCKET_IDEAL) -c -o $@ $< $(addprefix -I,$(TEST_SOURCEDIR)) $(CFLAGS) -fdata-sections -ffunction-sections
$(TEST_OBJDIR)/%.o: $(TEST_CASESDIR)/%.c $(TEST_OBJDIR)/%.o: $(TEST_CASESDIR)/%.c
$(CC) -c -o $@ $< $(addprefix -I,$(TEST_SOURCEDIR) $(TEST_CASESDIR)) $(CFLAGS) -fdata-sections -ffunction-sections $(CC) -DTOY_BUCKET_IDEAL=$(TOY_BUCKET_IDEAL) -c -o $@ $< $(addprefix -I,$(TEST_SOURCEDIR) $(TEST_CASESDIR)) $(CFLAGS) -fdata-sections -ffunction-sections
$(TEST_OUTDIR)/%.exe: $(TEST_OBJDIR)/%.o $(TEST_OUTDIR)/%.exe: $(TEST_OBJDIR)/%.o
@$(CC) -o $@ $< $(addprefix $(TEST_OBJDIR)/,$(notdir $(TEST_SOURCEFILES:.c=.o))) $(CFLAGS) $(LIBS) $(LDFLAGS) @$(CC) -DTOY_BUCKET_IDEAL=$(TOY_BUCKET_IDEAL) -o $@ $< $(addprefix $(TEST_OBJDIR)/,$(notdir $(TEST_SOURCEFILES:.c=.o))) $(CFLAGS) $(LIBS) $(LDFLAGS)
strip $@
.PRECIOUS: $(TEST_OUTDIR)/%.run .PRECIOUS: $(TEST_OUTDIR)/%.run
$(TEST_OUTDIR)/%.run: $(TEST_OUTDIR)/%.exe $(TEST_OUTDIR)/%.run: $(TEST_OUTDIR)/%.exe
@/usr/bin/time --format "%C; $(OVERRIDE)\nUser System\n%U %E" $< 100000000 512 @/usr/bin/time --format "User System\n%U %E" $<
@/usr/bin/time --format "%C; $(OVERRIDE)\nUser System\n%U %E" $< 100000000 1024
@/usr/bin/time --format "%C; $(OVERRIDE)\nUser System\n%U %E" $< 100000000 4096
#util targets #util targets
$(TEST_OUTDIR): $(TEST_OUTDIR):