summaryrefslogtreecommitdiffstats
path: root/tests/profile/benchmark-dictionary.c
blob: 30c098d5dbe560b23491068873f2b589544645b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * 1. build netdata (as normally)
 * 2. cd tests/profile/
 * 3. compile with:
 *    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
 *
 */

#include "config.h"
#include "libnetdata/libnetdata.h"

struct myvalue {
	int i;
};

void netdata_cleanup_and_exit(int ret) { exit(ret); }

int main(int argc, char **argv) {
	if(argc || argv) {;}

//	DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_WITH_STATISTICS);
	DICTIONARY *dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS);
	if(!dict) fatal("Cannot create dictionary.");

	struct rusage start, end;
	unsigned long long dt;
	char buf[100 + 1];
	struct myvalue value, *v;
	int i, max = 30000000, max2;

	// ------------------------------------------------------------------------

	getrusage(RUSAGE_SELF, &start);
	dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
	fprintf(stderr, "Inserting %d entries in the dictionary\n", max);
	for(i = 0; i < max; i++) {
		value.i = i;
		snprintf(buf, 100, "%d", i);

		dictionary_set(dict, buf, &value, sizeof(struct myvalue));
	}
	getrusage(RUSAGE_SELF, &end);
	dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
	fprintf(stderr, "Added %d entries in %llu nanoseconds: %llu inserts per second\n", max, dt, max * 1000000ULL / dt);
	fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);

	// ------------------------------------------------------------------------

	getrusage(RUSAGE_SELF, &start);
	dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
	fprintf(stderr, "Retrieving %d entries from the dictionary\n", max);
	for(i = 0; i < max; i++) {
		value.i = i;
		snprintf(buf, 100, "%d", i);

		v = dictionary_get(dict, buf);
		if(!v)
			fprintf(stderr, "ERROR: cannot get value %d from the dictionary\n", i);
		else if(v->i != i)
			fprintf(stderr, "ERROR: expected %d but got %d\n", i, v->i);
	}
	getrusage(RUSAGE_SELF, &end);
	dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
	fprintf(stderr, "Read %d entries in %llu nanoseconds: %llu searches per second\n", max, dt, max * 1000000ULL / dt);
	fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);

	// ------------------------------------------------------------------------

	getrusage(RUSAGE_SELF, &start);
	dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
	fprintf(stderr, "Resetting %d entries in the dictionary\n", max);
	for(i = 0; i < max; i++) {
		value.i = i;
		snprintf(buf, 100, "%d", i);

		dictionary_set(dict, buf, &value, sizeof(struct myvalue));
	}
	getrusage(RUSAGE_SELF, &end);
	dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
	fprintf(stderr, "Reset %d entries in %llu nanoseconds: %llu resets per second\n", max, dt, max * 1000000ULL / dt);
	fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);

	// ------------------------------------------------------------------------

	getrusage(RUSAGE_SELF, &start);
	dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
	fprintf(stderr, "Searching  %d non-existing entries in the dictionary\n", max);
	max2 = max * 2;
	for(i = max; i < max2; i++) {
		value.i = i;
		snprintf(buf, 100, "%d", i);

		v = dictionary_get(dict, buf);
		if(v)
			fprintf(stderr, "ERROR: cannot got non-existing value %d from the dictionary\n", i);
	}
	getrusage(RUSAGE_SELF, &end);
	dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
	fprintf(stderr, "Searched %d non-existing entries in %llu nanoseconds: %llu not found searches per second\n", max, dt, max * 1000000ULL / dt);
	fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);

	// ------------------------------------------------------------------------

	getrusage(RUSAGE_SELF, &start);
	dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
	fprintf(stderr, "Deleting %d entries from the dictionary\n", max);
	for(i = 0; i < max; i++) {
		value.i = i;
		snprintf(buf, 100, "%d", i);

		dictionary_del(dict, buf);
	}
	getrusage(RUSAGE_SELF, &end);
	dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
	fprintf(stderr, "Deleted %d entries in %llu nanoseconds: %llu deletes per second\n", max, dt, max * 1000000ULL / dt);
	fprintf(stderr, " > Dictionary: %llu inserts, %llu deletes, %llu searches\n\n", dict->stats->inserts, dict->stats->deletes, dict->stats->searches);

	// ------------------------------------------------------------------------

	getrusage(RUSAGE_SELF, &start);
	dict->stats->inserts = dict->stats->deletes = dict->stats->searches = 0ULL;
	fprintf(stderr, "Destroying dictionary\n");
	dictionary_destroy(dict);
	getrusage(RUSAGE_SELF, &end);
	dt = (end.ru_utime.tv_sec * 1000000ULL + end.ru_utime.tv_usec) - (start.ru_utime.tv_sec * 1000000ULL + start.ru_utime.tv_usec);
	fprintf(stderr, "Destroyed in %llu nanoseconds\n", dt);

	return 0;
}