| 1 | /* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 | /* |
| 3 | * 1. build netdata (as normally) |
| 4 | * 2. cd tests/profile/ |
| 5 | * 3. compile with: |
| 6 | * gcc -O3 -Wall -Wextra -I ../../src/ -I ../../ -o benchmark-dictionary benchmark-dictionary.c ../../src/dictionary.o ../../src/log.o ../../src/avl.o ../../src/common.o -pthread |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include "config.h" |
| 11 | #include "libnetdata/libnetdata.h" |
| 12 | |
| 13 | struct myvalue { |
| 14 | int i; |
| 15 | }; |
| 16 | |
| 17 | void netdata_cleanup_and_exit(int ret) { exit(ret); } |
| 18 | |
| 19 | int main(int argc, char **argv) { |
| 20 | if(argc || argv) {;} |
| 21 | |
| 22 | // DICTIONARY *dict = dictionary_create(DICT_OPTION_SINGLE_THREADED|DICT_OPTION_WITH_STATISTICS); |
| 23 | DICTIONARY *dict = dictionary_create(DICT_OPTION_STATS); |
| 24 | if(!dict) fatal("Cannot create dictionary."); |
| 25 | |
| 26 | struct rusage start, end; |
| 27 | unsigned long long dt; |
| 28 | char buf[100 + 1]; |
| 29 | struct myvalue value, *v; |
| 30 | int i, max = 30000000, max2; |
| 31 | |
| 32 | // ------------------------------------------------------------------------ |
| 33 | |
| 34 | getrusage(RUSAGE_SELF, &start); |
| 35 | dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; |
| 36 | fprintf(stderr, "Inserting %d entries in the dictionary\n", max); |
| 37 | for(i = 0; i < max; i++) { |
| 38 | value.i = i; |
| 39 | snprintf(buf, 100, "%d", i); |
| 40 | |
| 41 | dictionary_set(dict, buf, &value, sizeof(struct myvalue)); |
| 42 | } |
| 43 | getrusage(RUSAGE_SELF, &end); |
| 44 | dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); |
| 45 | fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt); |
| 46 | fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 50 | getrusage(RUSAGE_SELF, &start); |
| 51 | dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; |
| 52 | fprintf(stderr, "Retrieving %d entries from the dictionary\n", max); |
| 53 | for(i = 0; i < max; i++) { |
| 54 | value.i = i; |
| 55 | snprintf(buf, 100, "%d", i); |
| 56 | |
| 57 | v = dictionary_get(dict, buf); |
| 58 | if(!v) |
| 59 | fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i); |
| 60 | else if(v->i != i) |
| 61 | fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i); |
| 62 | } |
| 63 | getrusage(RUSAGE_SELF, &end); |
| 64 | dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); |
| 65 | fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt); |
| 66 | fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); |
| 67 | |
| 68 | // ------------------------------------------------------------------------ |
| 69 | |
| 70 | getrusage(RUSAGE_SELF, &start); |
| 71 | dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; |
| 72 | fprintf(stderr, "Resetting %d entries in the dictionary\n", max); |
| 73 | for(i = 0; i < max; i++) { |
| 74 | value.i = i; |
| 75 | snprintf(buf, 100, "%d", i); |
| 76 | |
| 77 | dictionary_set(dict, buf, &value, sizeof(struct myvalue)); |
| 78 | } |
| 79 | getrusage(RUSAGE_SELF, &end); |
| 80 | dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); |
| 81 | fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu resets per second\n", max, dt, max * 1000000ULL / dt); |
| 82 | fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); |
| 83 | |
| 84 | // ------------------------------------------------------------------------ |
| 85 | |
| 86 | getrusage(RUSAGE_SELF, &start); |
| 87 | dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; |
| 88 | fprintf(stderr, "Searching %d non-existing entries in the dictionary\n", max); |
| 89 | max2 = max * 2; |
| 90 | for(i = max; i < max2; i++) { |
| 91 | value.i = i; |
| 92 | snprintf(buf, 100, "%d", i); |
| 93 | |
| 94 | v = dictionary_get(dict, buf); |
| 95 | if(v) |
| 96 | fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i); |
| 97 | } |
| 98 | getrusage(RUSAGE_SELF, &end); |
| 99 | dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); |
| 100 | fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu not found searches per second\n", max, dt, max * 1000000ULL / dt); |
| 101 | fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); |
| 102 | |
| 103 | // ------------------------------------------------------------------------ |
| 104 | |
| 105 | getrusage(RUSAGE_SELF, &start); |
| 106 | dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; |
| 107 | fprintf(stderr, "Deleting %d entries from the dictionary\n", max); |
| 108 | for(i = 0; i < max; i++) { |
| 109 | value.i = i; |
| 110 | snprintf(buf, 100, "%d", i); |
| 111 | |
| 112 | dictionary_del(dict, buf); |
| 113 | } |
| 114 | getrusage(RUSAGE_SELF, &end); |
| 115 | dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); |
| 116 | fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt); |
| 117 | fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches); |
| 118 | |
| 119 | // ------------------------------------------------------------------------ |
| 120 | |
| 121 | getrusage(RUSAGE_SELF, &start); |
| 122 | dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL; |
| 123 | fprintf(stderr, "Destroying dictionary\n"); |
| 124 | dictionary_destroy(dict); |
| 125 | getrusage(RUSAGE_SELF, &end); |
| 126 | dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec); |
| 127 | fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt); |
| 128 | |
| 129 | return 0; |
| 130 | } |