benchmark.c (1605B)
1 #include <errno.h> 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <sys/time.h> 7 #include <time.h> 8 #include <unistd.h> 9 10 #include "finwo/benchmark.h" 11 #include "mindex.h" 12 13 static void fn_purge(void *item, void *udata) { 14 free(item); 15 // Intentionally empty 16 } 17 18 static int fn_compare_string(const void *a, const void *b, void *udata) { 19 const char *sa = (char*)a; 20 const char *sb = (char*)b; 21 return strcmp(sa, sb); 22 } 23 24 const char *alphabet = "0123456789abcdef"; 25 int alen = sizeof(alphabet); 26 char *random_str(int length) { 27 char *str = malloc(length + 1); 28 for(int i=0; i<length; i++) { 29 str[i] = alphabet[rand() % alen]; 30 } 31 str[length] = '\0'; 32 return str; 33 } 34 35 void mindex_bmark_rstr_1024() { 36 for(int i=0; i<1024; i++) { 37 free(random_str(16)); 38 } 39 } 40 41 void mindex_bmark_rstr_65536() { 42 for(int i=0; i<65536; i++) { 43 free(random_str(16)); 44 } 45 } 46 47 void mindex_bmark_assign_1024() { 48 struct mindex_t *mindex = mindex_init(fn_compare_string, fn_purge, NULL); 49 for(int i=0; i<1024; i++) { 50 mindex_set(mindex, random_str(16)); 51 } 52 mindex_free(mindex); 53 } 54 55 void mindex_bmark_assign_65536() { 56 struct mindex_t *mindex = mindex_init(fn_compare_string, fn_purge, NULL); 57 for(int i=0; i<65536; i++) { 58 mindex_set(mindex, random_str(16)); 59 } 60 mindex_free(mindex); 61 } 62 63 int main() { 64 65 char percentiles[] = { 66 1, 5, 50, 95, 99, 0 67 }; 68 69 BMARK(mindex_bmark_rstr_1024); 70 BMARK(mindex_bmark_assign_1024); 71 BMARK(mindex_bmark_rstr_65536); 72 BMARK(mindex_bmark_assign_65536); 73 return bmark_run(100, percentiles); 74 return 0; 75 }