1 /* Simple speed test for TDB */
13 #include <ccan/tdb2/tdb2.h>
15 /* Nanoseconds per operation */
16 static size_t normalize(const struct timeval *start,
17 const struct timeval *stop,
22 timersub(stop, start, &diff);
24 /* Floating point is more accurate here. */
25 return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
29 static size_t file_size(void)
33 if (stat("/tmp/speed.tdb", &st) != 0)
38 static int count_record(struct tdb_context *tdb,
39 TDB_DATA key, TDB_DATA data, void *p)
42 *total += *(int *)data.dptr;
46 static void dump_and_clear_stats(struct tdb_attribute_stats *stats)
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);
94 memset(&stats->allocs, 0, (char *)(stats+1) - (char *)&stats->allocs);
97 int main(int argc, char *argv[])
99 unsigned int i, j, num = 1000, stage = 0, stopat = -1;
100 int flags = TDB_DEFAULT;
101 bool transaction = false;
103 struct tdb_context *tdb;
104 struct timeval start, stop;
105 union tdb_attribute seed, stats;
106 enum TDB_ERROR ecode;
108 /* Try to keep benchmarks even. */
109 seed.base.attr = TDB_ATTRIBUTE_SEED;
110 seed.base.next = NULL;
113 memset(&stats, 0, sizeof(stats));
114 stats.base.attr = TDB_ATTRIBUTE_STATS;
115 stats.base.next = NULL;
116 stats.stats.size = sizeof(stats);
118 if (argv[1] && strcmp(argv[1], "--internal") == 0) {
119 flags = TDB_INTERNAL;
123 if (argv[1] && strcmp(argv[1], "--transaction") == 0) {
128 if (argv[1] && strcmp(argv[1], "--no-sync") == 0) {
133 if (argv[1] && strcmp(argv[1], "--stats") == 0) {
134 seed.base.next = &stats;
139 tdb = tdb_open("/tmp/speed.tdb", flags, O_RDWR|O_CREAT|O_TRUNC,
142 err(1, "Opening /tmp/speed.tdb");
144 key.dptr = (void *)&i;
145 key.dsize = sizeof(i);
155 stopat = atoi(argv[1]);
160 if (transaction && (ecode = tdb_transaction_start(tdb)))
161 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
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());
177 dump_and_clear_stats(&stats.stats);
178 if (++stage == stopat)
181 if (transaction && (ecode = tdb_transaction_start(tdb)))
182 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
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);
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());
201 dump_and_clear_stats(&stats.stats);
202 if (++stage == stopat)
205 if (transaction && (ecode = tdb_transaction_start(tdb)))
206 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
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));
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());
224 dump_and_clear_stats(&stats.stats);
225 if (++stage == stopat)
228 if (transaction && (ecode = tdb_transaction_start(tdb)))
229 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
231 /* Traverse 1000 records. */
232 printf("Traversing %u records: ", num); fflush(stdout);
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());
245 dump_and_clear_stats(&stats.stats);
246 if (++stage == stopat)
249 if (transaction && (ecode = tdb_transaction_start(tdb)))
250 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
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));
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());
267 dump_and_clear_stats(&stats.stats);
268 if (++stage == stopat)
271 if (transaction && (ecode = tdb_transaction_start(tdb)))
272 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
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));
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());
289 dump_and_clear_stats(&stats.stats);
290 if (++stage == stopat)
293 if (transaction && (ecode = tdb_transaction_start(tdb)))
294 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
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)
311 if (transaction && (ecode = tdb_transaction_start(tdb)))
312 errx(1, "starting transaction: %s", tdb_errorstr(ecode));
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));
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));
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());
334 dump_and_clear_stats(&stats.stats);
335 if (++stage == stopat)