-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrote a benchmark to test TOY_BUCKET_IDEAL sizes
- Loading branch information
1 parent
04c7999
commit 8b5cc3b
Showing
3 changed files
with
71 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,78 @@ | ||
#include "toy_table.h" | ||
#include "toy.h" | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
//utils | ||
unsigned int hashUInt(unsigned int x) { | ||
x = ((x >> 16) ^ x) * 0x45d9f3b; | ||
x = ((x >> 16) ^ x) * 0x45d9f3b; | ||
x = (x >> 16) ^ x; | ||
return x; | ||
//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 | ||
|
||
static unsigned int hash(unsigned int x) { | ||
x = ((x >> 16) ^ x) * 0x45d9f3b; | ||
x = ((x >> 16) ^ x) * 0x45d9f3b; | ||
x = ((x >> 16) ^ x); | ||
return x; | ||
} | ||
|
||
void stress_inserts(unsigned int seed, unsigned int iterations, unsigned int limit) { | ||
//randomly generate a series of key-value pairs (from a seed) and insert them | ||
{ | ||
//setup | ||
Toy_Table* table = Toy_allocateTable(); | ||
static unsigned int seed = 42; | ||
|
||
for (unsigned int i = 0; i < iterations; i++) { | ||
//next seed | ||
seed = hashUInt(seed); | ||
static unsigned int rng() { | ||
return seed = hash(seed); | ||
} | ||
|
||
//don't exceed a certain number of entries | ||
unsigned int masked = seed & (limit-1); //lol | ||
#define MAX 9 | ||
const char* samples[] = { //9 entries | ||
"the", | ||
"quick", | ||
"brown", | ||
"fox", | ||
"jumped", | ||
"over", | ||
"the", | ||
"lazy", | ||
"dog", | ||
}; | ||
|
||
//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); | ||
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_insertTable(&table, key, value); | ||
} | ||
Toy_String* l = Toy_concatStrings(bucketHandle, a, b); | ||
Toy_String* r = Toy_concatStrings(bucketHandle, c, d); | ||
Toy_concatStrings(bucketHandle, l, r); | ||
|
||
//cleanup | ||
Toy_freeTable(table); | ||
// char* buffer = Toy_getStringRawBuffer(s); | ||
// printf("%s\n", buffer); | ||
// free(buffer); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
if (argc != 3) { | ||
printf("Usage: %s iterations limit\n", argv[0]); | ||
return 0; | ||
} | ||
static unsigned long long int measureDepth(Toy_Bucket* bucket) { | ||
return bucket == NULL ? 0 : 1 + measureDepth(bucket->next); | ||
} | ||
|
||
static unsigned long long int measureCapacity(Toy_Bucket* bucket) { | ||
return bucket == NULL ? 0 : bucket->capacity + measureCapacity(bucket->next); | ||
} | ||
|
||
unsigned int iterations = 0; | ||
unsigned int limit = 0; | ||
static unsigned long long int measureCount(Toy_Bucket* bucket) { | ||
return bucket == NULL ? 0 : bucket->count + measureCount(bucket->next); | ||
} | ||
|
||
sscanf(argv[1], "%u", &iterations); | ||
sscanf(argv[2], "%u", &limit); | ||
int main() { | ||
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); | ||
|
||
//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; | ||
} | ||
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); | ||
|
||
//run the stress test | ||
stress_inserts(42, iterations, limit); | ||
Toy_freeBucket(&bucket); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters