TDB2: Goodbye TDB2, Hello NTDB.
[obnox/samba/samba-obnox.git] / lib / ntdb / tools / speed.c
similarity index 60%
rename from lib/tdb2/tools/speed.c
rename to lib/ntdb/tools/speed.c
index 259d53f6c81ee1eac49897daace63bc3d68586eb..868494b89891e63eb534a2872ebc90294631b9f9 100644 (file)
@@ -1,4 +1,4 @@
-/* Simple speed test for TDB */
+/* Simple speed test for NTDB */
 #include <ccan/err/err.h>
 #include <time.h>
 #include <sys/types.h>
@@ -10,7 +10,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
-#include "tdb2.h"
+#include "ntdb.h"
 
 /* Nanoseconds per operation */
 static size_t normalize(const struct timeval *start,
@@ -30,31 +30,31 @@ static size_t file_size(void)
 {
        struct stat st;
 
-       if (stat("/tmp/speed.tdb", &st) != 0)
+       if (stat("/tmp/speed.ntdb", &st) != 0)
                return -1;
        return st.st_size;
 }
 
-static int count_record(struct tdb_context *tdb,
-                       TDB_DATA key, TDB_DATA data, void *p)
+static int count_record(struct ntdb_context *ntdb,
+                       NTDB_DATA key, NTDB_DATA data, void *p)
 {
        int *total = p;
        *total += *(int *)data.dptr;
        return 0;
 }
 
-static void dump_and_clear_stats(struct tdb_context **tdb,
+static void dump_and_clear_stats(struct ntdb_context **ntdb,
                                 int flags,
-                                union tdb_attribute *attr)
+                                union ntdb_attribute *attr)
 {
-       union tdb_attribute stats;
-       enum TDB_ERROR ecode;
+       union ntdb_attribute stats;
+       enum NTDB_ERROR ecode;
 
-       stats.base.attr = TDB_ATTRIBUTE_STATS;
+       stats.base.attr = NTDB_ATTRIBUTE_STATS;
        stats.stats.size = sizeof(stats.stats);
-       ecode = tdb_get_attribute(*tdb, &stats);
-       if (ecode != TDB_SUCCESS)
-               errx(1, "Getting stats: %s", tdb_errorstr(ecode));
+       ecode = ntdb_get_attribute(*ntdb, &stats);
+       if (ecode != NTDB_SUCCESS)
+               errx(1, "Getting stats: %s", ntdb_errorstr(ecode));
 
        printf("allocs = %llu\n",
               (unsigned long long)stats.stats.allocs);
@@ -122,43 +122,43 @@ static void dump_and_clear_stats(struct tdb_context **tdb,
               (unsigned long long)stats.stats.lock_nonblock_fail);
 
        /* Now clear. */
-       tdb_close(*tdb);
-       *tdb = tdb_open("/tmp/speed.tdb", flags, O_RDWR, 0, attr);
+       ntdb_close(*ntdb);
+       *ntdb = ntdb_open("/tmp/speed.ntdb", flags, O_RDWR, 0, attr);
 }
 
-static void tdb_log(struct tdb_context *tdb,
-                   enum tdb_log_level level,
-                   enum TDB_ERROR ecode,
+static void ntdb_log(struct ntdb_context *ntdb,
+                   enum ntdb_log_level level,
+                   enum NTDB_ERROR ecode,
                    const char *message,
                    void *data)
 {
-       fprintf(stderr, "tdb:%s:%s:%s\n",
-               tdb_name(tdb), tdb_errorstr(ecode), message);
+       fprintf(stderr, "ntdb:%s:%s:%s\n",
+               ntdb_name(ntdb), ntdb_errorstr(ecode), message);
 }
 
 int main(int argc, char *argv[])
 {
        unsigned int i, j, num = 1000, stage = 0, stopat = -1;
-       int flags = TDB_DEFAULT;
+       int flags = NTDB_DEFAULT;
        bool transaction = false, summary = false;
-       TDB_DATA key, data;
-       struct tdb_context *tdb;
+       NTDB_DATA key, data;
+       struct ntdb_context *ntdb;
        struct timeval start, stop;
-       union tdb_attribute seed, log;
+       union ntdb_attribute seed, log;
        bool do_stats = false;
-       enum TDB_ERROR ecode;
+       enum NTDB_ERROR ecode;
 
        /* Try to keep benchmarks even. */
-       seed.base.attr = TDB_ATTRIBUTE_SEED;
+       seed.base.attr = NTDB_ATTRIBUTE_SEED;
        seed.base.next = NULL;
        seed.seed.seed = 0;
 
-       log.base.attr = TDB_ATTRIBUTE_LOG;
+       log.base.attr = NTDB_ATTRIBUTE_LOG;
        log.base.next = &seed;
-       log.log.fn = tdb_log;
+       log.log.fn = ntdb_log;
 
        if (argv[1] && strcmp(argv[1], "--internal") == 0) {
-               flags = TDB_INTERNAL;
+               flags = NTDB_INTERNAL;
                argc--;
                argv++;
        }
@@ -168,7 +168,7 @@ int main(int argc, char *argv[])
                argv++;
        }
        if (argv[1] && strcmp(argv[1], "--no-sync") == 0) {
-               flags |= TDB_NOSYNC;
+               flags |= NTDB_NOSYNC;
                argc--;
                argv++;
        }
@@ -183,10 +183,10 @@ int main(int argc, char *argv[])
                argv++;
        }
 
-       tdb = tdb_open("/tmp/speed.tdb", flags, O_RDWR|O_CREAT|O_TRUNC,
+       ntdb = ntdb_open("/tmp/speed.ntdb", flags, O_RDWR|O_CREAT|O_TRUNC,
                       0600, &log);
-       if (!tdb)
-               err(1, "Opening /tmp/speed.tdb");
+       if (!ntdb)
+               err(1, "Opening /tmp/speed.ntdb");
 
        key.dptr = (void *)&i;
        key.dsize = sizeof(i);
@@ -206,199 +206,199 @@ int main(int argc, char *argv[])
 
        /* Add 1000 records. */
        printf("Adding %u records: ", num); fflush(stdout);
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (i = 0; i < num; i++)
-               if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
-                       errx(1, "Inserting key %u in tdb: %s",
-                            i, tdb_errorstr(ecode));
+               if ((ecode = ntdb_store(ntdb, key, data, NTDB_INSERT)) != 0)
+                       errx(1, "Inserting key %u in ntdb: %s",
+                            i, ntdb_errorstr(ecode));
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
 
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
        if (do_stats)
-               dump_and_clear_stats(&tdb, flags, &log);
+               dump_and_clear_stats(&ntdb, flags, &log);
 
        if (++stage == stopat)
                exit(0);
 
        /* Finding 1000 records. */
        printf("Finding %u records: ", num); fflush(stdout);
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (i = 0; i < num; i++) {
-               struct tdb_data dbuf;
-               if ((ecode = tdb_fetch(tdb, key, &dbuf)) != TDB_SUCCESS
+               NTDB_DATA dbuf;
+               if ((ecode = ntdb_fetch(ntdb, key, &dbuf)) != NTDB_SUCCESS
                    || *(int *)dbuf.dptr != i) {
-                       errx(1, "Fetching key %u in tdb gave %u",
+                       errx(1, "Fetching key %u in ntdb gave %u",
                             i, ecode ? ecode : *(int *)dbuf.dptr);
                }
        }
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
        if (do_stats)
-               dump_and_clear_stats(&tdb, flags, &log);
+               dump_and_clear_stats(&ntdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
        /* Missing 1000 records. */
        printf("Missing %u records: ", num); fflush(stdout);
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (i = num; i < num*2; i++) {
-               struct tdb_data dbuf;
-               ecode = tdb_fetch(tdb, key, &dbuf);
-               if (ecode != TDB_ERR_NOEXIST)
-                       errx(1, "Fetching key %u in tdb gave %s",
-                            i, tdb_errorstr(ecode));
+               NTDB_DATA dbuf;
+               ecode = ntdb_fetch(ntdb, key, &dbuf);
+               if (ecode != NTDB_ERR_NOEXIST)
+                       errx(1, "Fetching key %u in ntdb gave %s",
+                            i, ntdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
        if (do_stats)
-               dump_and_clear_stats(&tdb, flags, &log);
+               dump_and_clear_stats(&ntdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
        /* Traverse 1000 records. */
        printf("Traversing %u records: ", num); fflush(stdout);
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        i = 0;
        gettimeofday(&start, NULL);
-       if (tdb_traverse(tdb, count_record, &i) != num)
+       if (ntdb_traverse(ntdb, count_record, &i) != num)
                errx(1, "Traverse returned wrong number of records");
        if (i != (num - 1) * (num / 2))
                errx(1, "Traverse tallied to %u", i);
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
        if (do_stats)
-               dump_and_clear_stats(&tdb, flags, &log);
+               dump_and_clear_stats(&ntdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
        /* Delete 1000 records (not in order). */
        printf("Deleting %u records: ", num); fflush(stdout);
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (j = 0; j < num; j++) {
                i = (j + 100003) % num;
-               if ((ecode = tdb_delete(tdb, key)) != TDB_SUCCESS)
-                       errx(1, "Deleting key %u in tdb: %s",
-                            i, tdb_errorstr(ecode));
+               if ((ecode = ntdb_delete(ntdb, key)) != NTDB_SUCCESS)
+                       errx(1, "Deleting key %u in ntdb: %s",
+                            i, ntdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
        if (do_stats)
-               dump_and_clear_stats(&tdb, flags, &log);
+               dump_and_clear_stats(&ntdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
        /* Re-add 1000 records (not in order). */
        printf("Re-adding %u records: ", num); fflush(stdout);
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        gettimeofday(&start, NULL);
        for (j = 0; j < num; j++) {
                i = (j + 100003) % num;
-               if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
-                       errx(1, "Inserting key %u in tdb: %s",
-                            i, tdb_errorstr(ecode));
+               if ((ecode = ntdb_store(ntdb, key, data, NTDB_INSERT)) != 0)
+                       errx(1, "Inserting key %u in ntdb: %s",
+                            i, ntdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
        if (do_stats)
-               dump_and_clear_stats(&tdb, flags, &log);
+               dump_and_clear_stats(&ntdb, flags, &log);
        if (++stage == stopat)
                exit(0);
 
        /* Append 1000 records. */
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        printf("Appending %u records: ", num); fflush(stdout);
        gettimeofday(&start, NULL);
        for (i = 0; i < num; i++)
-               if ((ecode = tdb_append(tdb, key, data)) != TDB_SUCCESS)
-                       errx(1, "Appending key %u in tdb: %s",
-                            i, tdb_errorstr(ecode));
+               if ((ecode = ntdb_append(ntdb, key, data)) != NTDB_SUCCESS)
+                       errx(1, "Appending key %u in ntdb: %s",
+                            i, ntdb_errorstr(ecode));
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
@@ -406,36 +406,36 @@ int main(int argc, char *argv[])
                exit(0);
 
        /* Churn 1000 records: not in order! */
-       if (transaction && (ecode = tdb_transaction_start(tdb)))
-               errx(1, "starting transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_start(ntdb)))
+               errx(1, "starting transaction: %s", ntdb_errorstr(ecode));
        printf("Churning %u records: ", num); fflush(stdout);
        gettimeofday(&start, NULL);
        for (j = 0; j < num; j++) {
                i = (j + 1000019) % num;
-               if ((ecode = tdb_delete(tdb, key)) != TDB_SUCCESS)
-                       errx(1, "Deleting key %u in tdb: %s",
-                            i, tdb_errorstr(ecode));
+               if ((ecode = ntdb_delete(ntdb, key)) != NTDB_SUCCESS)
+                       errx(1, "Deleting key %u in ntdb: %s",
+                            i, ntdb_errorstr(ecode));
                i += num;
-               if ((ecode = tdb_store(tdb, key, data, TDB_INSERT)) != 0)
-                       errx(1, "Inserting key %u in tdb: %s",
-                            i, tdb_errorstr(ecode));
+               if ((ecode = ntdb_store(ntdb, key, data, NTDB_INSERT)) != 0)
+                       errx(1, "Inserting key %u in ntdb: %s",
+                            i, ntdb_errorstr(ecode));
        }
        gettimeofday(&stop, NULL);
-       if (transaction && (ecode = tdb_transaction_commit(tdb)))
-               errx(1, "committing transaction: %s", tdb_errorstr(ecode));
+       if (transaction && (ecode = ntdb_transaction_commit(ntdb)))
+               errx(1, "committing transaction: %s", ntdb_errorstr(ecode));
        printf(" %zu ns (%zu bytes)\n",
               normalize(&start, &stop, num), file_size());
 
-       if (tdb_check(tdb, NULL, NULL))
-               errx(1, "tdb_check failed!");
+       if (ntdb_check(ntdb, NULL, NULL))
+               errx(1, "ntdb_check failed!");
        if (summary) {
                char *sumstr = NULL;
-               tdb_summary(tdb, TDB_SUMMARY_HISTOGRAMS, &sumstr);
+               ntdb_summary(ntdb, NTDB_SUMMARY_HISTOGRAMS, &sumstr);
                printf("%s\n", sumstr);
                free(sumstr);
        }
        if (do_stats)
-               dump_and_clear_stats(&tdb, flags, &log);
+               dump_and_clear_stats(&ntdb, flags, &log);
        if (++stage == stopat)
                exit(0);