tdb:tdbtool: add the "speed" command to the help text.
[amitay/samba.git] / lib / tdb / tools / tdbtool.c
index d104ccd7c44994e9d0f8ba138cbf4584343983dc..52c901f4e89e43bfbf45604bd64fb9deb97ed39d 100644 (file)
@@ -57,6 +57,7 @@ enum commands {
        CMD_FIRST,
        CMD_NEXT,
        CMD_SYSTEM,
+       CMD_CHECK,
        CMD_QUIT,
        CMD_HELP
 };
@@ -87,6 +88,7 @@ COMMAND_TABLE cmd_table[] = {
        {"1",           CMD_FIRST},
        {"next",        CMD_NEXT},
        {"n",           CMD_NEXT},
+       {"check",       CMD_CHECK},
        {"quit",        CMD_QUIT},
        {"q",           CMD_QUIT},
        {"!",           CMD_SYSTEM},
@@ -179,7 +181,9 @@ static void help(void)
 "  delete    key        : delete a record by key\n"
 "  list                 : print the database hash table and freelist\n"
 "  free                 : print the database freelist\n"
-"  ! command            : execute system command\n"             
+"  check                : check the integrity of an opened database\n"
+"  speed                : perform speed tests on the database\n"
+"  ! command            : execute system command\n"
 "  1 | first            : print the first record\n"
 "  n | next             : print the next record\n"
 "  q | quit             : terminate\n"
@@ -389,15 +393,68 @@ static void speed_tdb(const char *tlimit)
 {
        unsigned timelimit = tlimit?atoi(tlimit):0;
        double t;
-       int ops=0;
-       if (timelimit == 0) timelimit = 10;
+       int ops;
+       if (timelimit == 0) timelimit = 5;
+
+       ops = 0;
+       printf("Testing store speed for %u seconds\n", timelimit);
+       _start_timer();
+       do {
+               long int r = random();
+               TDB_DATA key, dbuf;
+               key.dptr = (unsigned char *)"store test";
+               key.dsize = strlen((char *)key.dptr);
+               dbuf.dptr = (unsigned char *)&r;
+               dbuf.dsize = sizeof(r);
+               tdb_store(tdb, key, dbuf, TDB_REPLACE);
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
+
+       ops = 0;
+       printf("Testing fetch speed for %u seconds\n", timelimit);
+       _start_timer();
+       do {
+               long int r = random();
+               TDB_DATA key, dbuf;
+               key.dptr = (unsigned char *)"store test";
+               key.dsize = strlen((char *)key.dptr);
+               dbuf.dptr = (unsigned char *)&r;
+               dbuf.dsize = sizeof(r);
+               tdb_fetch(tdb, key);
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
+
+       ops = 0;
+       printf("Testing transaction speed for %u seconds\n", timelimit);
+       _start_timer();
+       do {
+               long int r = random();
+               TDB_DATA key, dbuf;
+               key.dptr = (unsigned char *)"transaction test";
+               key.dsize = strlen((char *)key.dptr);
+               dbuf.dptr = (unsigned char *)&r;
+               dbuf.dsize = sizeof(r);
+               tdb_transaction_start(tdb);
+               tdb_store(tdb, key, dbuf, TDB_REPLACE);
+               tdb_transaction_commit(tdb);
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
+
+       ops = 0;
        printf("Testing traverse speed for %u seconds\n", timelimit);
        _start_timer();
-       while ((t=_end_timer()) < timelimit) {
+       do {
                tdb_traverse(tdb, traverse_fn, NULL);
-               printf("%10.3f ops/sec\r", (++ops)/t);
-       }
-       printf("\n");
+               t = _end_timer();
+               ops++;
+       } while (t < timelimit);
+       printf("%10.3f ops/sec\n", ops/t);
 }
 
 static void toggle_mmap(void)
@@ -452,6 +509,27 @@ static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
                print_rec(the_tdb, *pkey, dbuf, NULL);
 }
 
+static int test_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
+{
+       return 0;
+}
+
+static void check_db(TDB_CONTEXT *the_tdb)
+{
+       int tdbcount=-1;
+       if (the_tdb) {
+               tdbcount = tdb_traverse(the_tdb, test_fn, NULL);
+       } else {
+               printf("Error: No database opened!\n");
+       }
+
+       if (tdbcount<0) {
+               printf("Integrity check for the opened database failed.\n");
+       } else {
+               printf("Database integrity is OK and has %d records.\n", tdbcount);
+       }
+}
+
 static int do_command(void)
 {
        COMMAND_TABLE *ctp = cmd_table;
@@ -482,7 +560,9 @@ static int do_command(void)
             return 0;
        case CMD_SYSTEM:
            /* Shell command */
-           system(arg1);
+           if (system(arg1) == -1) {
+               terror("system() call failed\n");
+           }
            return 0;
        case CMD_QUIT:
            return 1;
@@ -552,6 +632,9 @@ static int do_command(void)
               if (bIterate)
                  next_record(tdb, &iterate_kbuf);
                return 0;
+           case CMD_CHECK:
+               check_db(tdb);
+               return 0;
            case CMD_HELP:
                help();
                return 0;