]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tools/speed.c
216a051fe9f060dae65d96de48fa53f9f0bb8ee3
[ccan] / ccan / tdb2 / tools / speed.c
1 /* Simple speed test for TDB */
2 #include <err.h>
3 #include <time.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <sys/time.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdbool.h>
13 #include <ccan/tdb2/tdb2.h>
14
15 /* Nanoseconds per operation */
16 static size_t normalize(const struct timeval *start,
17                         const struct timeval *stop,
18                         unsigned int num)
19 {
20         struct timeval diff;
21
22         timersub(stop, start, &diff);
23
24         /* Floating point is more accurate here. */
25         return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
26                 / num * 1000;
27 }
28
29 static size_t file_size(void)
30 {
31         struct stat st;
32
33         if (stat("/tmp/speed.tdb", &st) != 0)
34                 return -1;
35         return st.st_size;
36 }
37
38 static int count_record(struct tdb_context *tdb,
39                         TDB_DATA key, TDB_DATA data, void *p)
40 {
41         int *total = p;
42         *total += *(int *)data.dptr;
43         return 0;
44 }
45
46 static void dump_and_clear_stats(struct tdb_attribute_stats *stats)
47 {
48         printf("allocs = %llu\n",
49                (unsigned long long)stats->allocs);
50         printf("  alloc_subhash = %llu\n",
51                (unsigned long long)stats->alloc_subhash);
52         printf("  alloc_chain = %llu\n",
53                (unsigned long long)stats->alloc_chain);
54         printf("  alloc_bucket_exact = %llu\n",
55                (unsigned long long)stats->alloc_bucket_exact);
56         printf("  alloc_bucket_max = %llu\n",
57                (unsigned long long)stats->alloc_bucket_max);
58         printf("  alloc_leftover = %llu\n",
59                (unsigned long long)stats->alloc_leftover);
60         printf("  alloc_coalesce_tried = %llu\n",
61                (unsigned long long)stats->alloc_coalesce_tried);
62         printf("    alloc_coalesce_lockfail = %llu\n",
63                (unsigned long long)stats->alloc_coalesce_lockfail);
64         printf("    alloc_coalesce_race = %llu\n",
65                (unsigned long long)stats->alloc_coalesce_race);
66         printf("    alloc_coalesce_succeeded = %llu\n",
67                (unsigned long long)stats->alloc_coalesce_succeeded);
68         printf("       alloc_coalesce_num_merged = %llu\n",
69                (unsigned long long)stats->alloc_coalesce_num_merged);
70         printf("compares = %llu\n",
71                (unsigned long long)stats->compares);
72         printf("  compare_wrong_bucket = %llu\n",
73                (unsigned long long)stats->compare_wrong_bucket);
74         printf("  compare_wrong_offsetbits = %llu\n",
75                (unsigned long long)stats->compare_wrong_offsetbits);
76         printf("  compare_wrong_keylen = %llu\n",
77                (unsigned long long)stats->compare_wrong_keylen);
78         printf("  compare_wrong_rechash = %llu\n",
79                (unsigned long long)stats->compare_wrong_rechash);
80         printf("  compare_wrong_keycmp = %llu\n",
81                (unsigned long long)stats->compare_wrong_keycmp);
82         printf("expands = %llu\n",
83                (unsigned long long)stats->expands);
84         printf("frees = %llu\n",
85                (unsigned long long)stats->frees);
86         printf("locks = %llu\n",
87                (unsigned long long)stats->locks);
88         printf("   lock_lowlevel = %llu\n",
89                (unsigned long long)stats->lock_lowlevel);
90         printf("   lock_nonblock = %llu\n",
91                (unsigned long long)stats->lock_nonblock);
92
93         /* Now clear. */
94         memset(&stats->allocs, 0, (char *)(stats+1) - (char *)&stats->allocs);
95 }
96
97 int main(int argc, char *argv[])
98 {
99         unsigned int i, j, num = 1000, stage = 0, stopat = -1;
100         int flags = TDB_DEFAULT;
101         bool transaction = false;
102         TDB_DATA key, data;
103         struct tdb_context *tdb;
104         struct timeval start, stop;
105         union tdb_attribute seed, stats;
106         enum TDB_ERROR ecode;
107
108         /* Try to keep benchmarks even. */
109         seed.base.attr = TDB_ATTRIBUTE_SEED;
110         seed.base.next = NULL;
111         seed.seed.seed = 0;
112
113         memset(&stats, 0, sizeof(stats));
114         stats.base.attr = TDB_ATTRIBUTE_STATS;
115         stats.base.next = NULL;
116         stats.stats.size = sizeof(stats);
117
118         if (argv[1] && strcmp(argv[1], "--internal") == 0) {
119                 flags = TDB_INTERNAL;
120                 argc--;
121                 argv++;
122         }
123         if (argv[1] && strcmp(argv[1], "--transaction") == 0) {
124                 transaction = true;
125                 argc--;
126                 argv++;
127         }
128         if (argv[1] && strcmp(argv[1], "--no-sync") == 0) {
129                 flags |= TDB_NOSYNC;
130                 argc--;
131                 argv++;
132         }
133         if (argv[1] && strcmp(argv[1], "--stats") == 0) {
134                 seed.base.next = &stats;
135                 argc--;
136                 argv++;
137         }
138
139         tdb = tdb_open("/tmp/speed.tdb", flags, O_RDWR|O_CREAT|O_TRUNC,
140                        0600, &seed);
141         if (!tdb)
142                 err(1, "Opening /tmp/speed.tdb");
143
144         key.dptr = (void *)&i;
145         key.dsize = sizeof(i);
146         data = key;
147
148         if (argv[1]) {
149                 num = atoi(argv[1]);
150                 argv++;
151                 argc--;
152         }
153
154         if (argv[1]) {
155                 stopat = atoi(argv[1]);
156                 argv++;
157                 argc--;
158         }
159
160         if (transaction && (ecode = tdb_transaction_start(tdb)))
161                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
162
163         /* Add 1000 records. */
164         printf("Adding %u records: ", num); fflush(stdout);
165         gettimeofday(&start, NULL);
166         for (i = 0; i < num; i++)
167                 if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
168                         errx(1, "Inserting key %u in tdb: %s",
169                              i, tdb_errorstr(ecode));
170         gettimeofday(&stop, NULL);
171         if (transaction && (ecode = tdb_transaction_commit(tdb)))
172                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
173         printf(" %zu ns (%zu bytes)\n",
174                normalize(&start, &stop, num), file_size());
175
176         if (seed.base.next)
177                 dump_and_clear_stats(&stats.stats);
178         if (++stage == stopat)
179                 exit(0);
180
181         if (transaction && (ecode = tdb_transaction_start(tdb)))
182                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
183
184         /* Finding 1000 records. */
185         printf("Finding %u records: ", num); fflush(stdout);
186         gettimeofday(&start, NULL);
187         for (i = 0; i < num; i++) {
188                 struct tdb_data dbuf;
189                 if ((ecode = tdb_fetch(tdb, key, &dbuf)) != TDB_SUCCESS
190                     || *(int *)dbuf.dptr != i) {
191                         errx(1, "Fetching key %u in tdb gave %u",
192                              i, ecode ? ecode : *(int *)dbuf.dptr);
193                 }
194         }
195         gettimeofday(&stop, NULL);
196         if (transaction && (ecode = tdb_transaction_commit(tdb)))
197                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
198         printf(" %zu ns (%zu bytes)\n",
199                normalize(&start, &stop, num), file_size());
200         if (seed.base.next)
201                 dump_and_clear_stats(&stats.stats);
202         if (++stage == stopat)
203                 exit(0);
204
205         if (transaction && (ecode = tdb_transaction_start(tdb)))
206                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
207
208         /* Missing 1000 records. */
209         printf("Missing %u records: ", num); fflush(stdout);
210         gettimeofday(&start, NULL);
211         for (i = num; i < num*2; i++) {
212                 struct tdb_data dbuf;
213                 ecode = tdb_fetch(tdb, key, &dbuf);
214                 if (ecode != TDB_ERR_NOEXIST)
215                         errx(1, "Fetching key %u in tdb gave %s",
216                              i, tdb_errorstr(ecode));
217         }
218         gettimeofday(&stop, NULL);
219         if (transaction && (ecode = tdb_transaction_commit(tdb)))
220                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
221         printf(" %zu ns (%zu bytes)\n",
222                normalize(&start, &stop, num), file_size());
223         if (seed.base.next)
224                 dump_and_clear_stats(&stats.stats);
225         if (++stage == stopat)
226                 exit(0);
227
228         if (transaction && (ecode = tdb_transaction_start(tdb)))
229                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
230
231         /* Traverse 1000 records. */
232         printf("Traversing %u records: ", num); fflush(stdout);
233         i = 0;
234         gettimeofday(&start, NULL);
235         if (tdb_traverse(tdb, count_record, &i) != num)
236                 errx(1, "Traverse returned wrong number of records");
237         if (i != (num - 1) * (num / 2))
238                 errx(1, "Traverse tallied to %u", i);
239         gettimeofday(&stop, NULL);
240         if (transaction && (ecode = tdb_transaction_commit(tdb)))
241                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
242         printf(" %zu ns (%zu bytes)\n",
243                normalize(&start, &stop, num), file_size());
244         if (seed.base.next)
245                 dump_and_clear_stats(&stats.stats);
246         if (++stage == stopat)
247                 exit(0);
248
249         if (transaction && (ecode = tdb_transaction_start(tdb)))
250                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
251
252         /* Delete 1000 records (not in order). */
253         printf("Deleting %u records: ", num); fflush(stdout);
254         gettimeofday(&start, NULL);
255         for (j = 0; j < num; j++) {
256                 i = (j + 100003) % num;
257                 if ((ecode = tdb_delete(tdb, key)) != TDB_SUCCESS)
258                         errx(1, "Deleting key %u in tdb: %s",
259                              i, tdb_errorstr(ecode));
260         }
261         gettimeofday(&stop, NULL);
262         if (transaction && (ecode = tdb_transaction_commit(tdb)))
263                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
264         printf(" %zu ns (%zu bytes)\n",
265                normalize(&start, &stop, num), file_size());
266         if (seed.base.next)
267                 dump_and_clear_stats(&stats.stats);
268         if (++stage == stopat)
269                 exit(0);
270
271         if (transaction && (ecode = tdb_transaction_start(tdb)))
272                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
273
274         /* Re-add 1000 records (not in order). */
275         printf("Re-adding %u records: ", num); fflush(stdout);
276         gettimeofday(&start, NULL);
277         for (j = 0; j < num; j++) {
278                 i = (j + 100003) % num;
279                 if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
280                         errx(1, "Inserting key %u in tdb: %s",
281                              i, tdb_errorstr(ecode));
282         }
283         gettimeofday(&stop, NULL);
284         if (transaction && (ecode = tdb_transaction_commit(tdb)))
285                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
286         printf(" %zu ns (%zu bytes)\n",
287                normalize(&start, &stop, num), file_size());
288         if (seed.base.next)
289                 dump_and_clear_stats(&stats.stats);
290         if (++stage == stopat)
291                 exit(0);
292
293         if (transaction && (ecode = tdb_transaction_start(tdb)))
294                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
295
296         /* Append 1000 records. */
297         printf("Appending %u records: ", num); fflush(stdout);
298         gettimeofday(&start, NULL);
299         for (i = 0; i < num; i++)
300                 if ((ecode = tdb_append(tdb, key, data)) != TDB_SUCCESS)
301                         errx(1, "Appending key %u in tdb: %s",
302                              i, tdb_errorstr(ecode));
303         gettimeofday(&stop, NULL);
304         if (transaction && (ecode = tdb_transaction_commit(tdb)))
305                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
306         printf(" %zu ns (%zu bytes)\n",
307                normalize(&start, &stop, num), file_size());
308         if (++stage == stopat)
309                 exit(0);
310
311         if (transaction && (ecode = tdb_transaction_start(tdb)))
312                 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
313
314         /* Churn 1000 records: not in order! */
315         printf("Churning %u records: ", num); fflush(stdout);
316         gettimeofday(&start, NULL);
317         for (j = 0; j < num; j++) {
318                 i = (j + 1000019) % num;
319                 if ((ecode = tdb_delete(tdb, key)) != TDB_SUCCESS)
320                         errx(1, "Deleting key %u in tdb: %s",
321                              i, tdb_errorstr(ecode));
322                 i += num;
323                 if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
324                         errx(1, "Inserting key %u in tdb: %s",
325                              i, tdb_errorstr(ecode));
326         }
327         gettimeofday(&stop, NULL);
328         if (transaction && (ecode = tdb_transaction_commit(tdb)))
329                 errx(1, "committing transaction: %s", tdb_errorstr(ecode));
330         printf(" %zu ns (%zu bytes)\n",
331                normalize(&start, &stop, num), file_size());
332
333         if (seed.base.next)
334                 dump_and_clear_stats(&stats.stats);
335         if (++stage == stopat)
336                 exit(0);
337
338         return 0;
339 }