tdb_wrap: Remove tdb_wrap_open_ again
[kai/samba-autobuild/.git] / source4 / torture / local / dbspeed.c
index b6c1a43d483eab20e1cdceaa7176419232048344..185faecd96357d241d21187046384878b6c9978d 100644 (file)
 
 #include "includes.h"
 #include "system/filesys.h"
-#include "lib/tdb/include/tdb.h"
-#include "lib/ldb/include/ldb.h"
-#include "lib/ldb/include/ldb_errors.h"
-#include "lib/db_wrap.h"
-#include "torture/torture.h"
+#include "tdb_compat.h"
+#include <ldb.h>
+#include <ldb_errors.h>
+#include "ldb_wrap.h"
+#include "lib/tdb_wrap/tdb_wrap.h"
+#include "torture/smbtorture.h"
 #include "param/param.h"
 
 float tdb_speed;
@@ -35,6 +36,7 @@ static bool tdb_add_record(struct tdb_wrap *tdbw, const char *fmt1,
 {
        TDB_DATA key, data;
        int ret;
+
        key.dptr = (uint8_t *)talloc_asprintf(tdbw, fmt1, i);
        key.dsize = strlen((char *)key.dptr)+1;
        data.dptr = (uint8_t *)talloc_asprintf(tdbw, fmt2, i+10000);
@@ -50,7 +52,7 @@ static bool tdb_add_record(struct tdb_wrap *tdbw, const char *fmt1,
 /*
   test tdb speed
 */
-static BOOL test_tdb_speed(struct torture_context *torture, const void *_data)
+static bool test_tdb_speed(struct torture_context *torture, const void *_data)
 {
        struct timeval tv;
        struct tdb_wrap *tdbw;
@@ -62,12 +64,12 @@ static BOOL test_tdb_speed(struct torture_context *torture, const void *_data)
 
        torture_comment(torture, "Testing tdb speed for sidmap\n");
 
-       tdbw = tdb_wrap_open(tmp_ctx, "test.tdb", 
-                            10000, 0, O_RDWR|O_CREAT|O_TRUNC, 0600);
+       tdbw = tdb_wrap_open(tmp_ctx, "test.tdb", 10000,
+                            lpcfg_tdb_flags(torture->lp_ctx, 0),
+                            O_RDWR|O_CREAT|O_TRUNC, 0600);
        if (!tdbw) {
-               unlink("test.tdb");
-               talloc_free(tmp_ctx);
-               torture_fail(torture, "Failed to open test.tdb");
+               torture_result(torture, TORTURE_FAIL, "Failed to open test.tdb");
+               goto failed;
        }
 
        torture_comment(torture, "Adding %d SID records\n", torture_entries);
@@ -76,13 +78,13 @@ static BOOL test_tdb_speed(struct torture_context *torture, const void *_data)
                if (!tdb_add_record(tdbw, 
                                    "S-1-5-21-53173311-3623041448-2049097239-%u",
                                    "UID %u", i)) {
-                       torture_result(torture, TORTURE_FAIL, "Failed to add SID %d\n", i);
+                       torture_result(torture, TORTURE_FAIL, "Failed to add SID %d!", i);
                        goto failed;
                }
                if (!tdb_add_record(tdbw, 
                                    "UID %u",
                                    "S-1-5-21-53173311-3623041448-2049097239-%u", i)) {
-                       torture_result(torture, TORTURE_FAIL, "Failed to add UID %d\n", i);
+                       torture_result(torture, TORTURE_FAIL, "Failed to add UID %d!", i);
                        goto failed;
                }
        }
@@ -96,19 +98,19 @@ static BOOL test_tdb_speed(struct torture_context *torture, const void *_data)
                i = random() % torture_entries;
                key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "S-1-5-21-53173311-3623041448-2049097239-%u", i);
                key.dsize = strlen((char *)key.dptr)+1;
-               data = tdb_fetch(tdbw->tdb, key);
+               data = tdb_fetch_compat(tdbw->tdb, key);
                talloc_free(key.dptr);
                if (data.dptr == NULL) {
-                       torture_result(torture, TORTURE_FAIL, "Failed to fetch SID %d\n", i);
+                       torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
                        goto failed;
                }
                free(data.dptr);
                key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "UID %u", i);
                key.dsize = strlen((char *)key.dptr)+1;
-               data = tdb_fetch(tdbw->tdb, key);
+               data = tdb_fetch_compat(tdbw->tdb, key);
                talloc_free(key.dptr);
                if (data.dptr == NULL) {
-                       torture_result(torture, TORTURE_FAIL, "Failed to fetch UID %d\n", i);
+                       torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
                        goto failed;
                }
                free(data.dptr);
@@ -117,35 +119,37 @@ static BOOL test_tdb_speed(struct torture_context *torture, const void *_data)
        tdb_speed = count/timeval_elapsed(&tv);
        torture_comment(torture, "tdb speed %.2f ops/sec\n", tdb_speed);
        
-
-       unlink("test.tdb");
        talloc_free(tmp_ctx);
-       return True;
+       unlink("test.tdb");
+       return true;
 
 failed:
-       unlink("test.tdb");
        talloc_free(tmp_ctx);
-       return False;
+       unlink("test.tdb");
+       return false;
 }
 
 
-static BOOL ldb_add_record(struct ldb_context *ldb, unsigned rid)
+static bool ldb_add_record(struct ldb_context *ldb, unsigned rid)
 {
        struct ldb_message *msg;        
        int ret;
 
        msg = ldb_msg_new(ldb);
        if (msg == NULL) {
-               return False;
+               return false;
        }
 
        msg->dn = ldb_dn_new_fmt(msg, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", rid);
        if (msg->dn == NULL) {
-               return False;
+               talloc_free(msg);
+               return false;
        }
 
-       if (ldb_msg_add_fmt(msg, "UID", "%u", rid) != 0) {
-               return False;
+       ret = ldb_msg_add_fmt(msg, "UID", "%u", rid);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(msg);
+               return false;
        }
 
        ret = ldb_add(ldb, msg);
@@ -159,7 +163,7 @@ static BOOL ldb_add_record(struct ldb_context *ldb, unsigned rid)
 /*
   test ldb speed
 */
-static BOOL test_ldb_speed(struct torture_context *torture, const void *_data)
+static bool test_ldb_speed(struct torture_context *torture, const void *_data)
 {
        struct timeval tv;
        struct ldb_context *ldb;
@@ -175,31 +179,37 @@ static BOOL test_ldb_speed(struct torture_context *torture, const void *_data)
 
        torture_comment(torture, "Testing ldb speed for sidmap\n");
 
-       ldb = ldb_wrap_connect(tmp_ctx, global_loadparm, "tdb://test.ldb", 
-                               NULL, NULL, LDB_FLG_NOSYNC, NULL);
+       ldb = ldb_wrap_connect(tmp_ctx, torture->ev, torture->lp_ctx, "tdb://test.ldb",
+                               NULL, NULL, LDB_FLG_NOSYNC);
        if (!ldb) {
-               unlink("./test.ldb");
-               talloc_free(tmp_ctx);
-               torture_fail(torture, "Failed to open test.ldb");
+               torture_result(torture, TORTURE_FAIL, "Failed to open test.ldb");
+               goto failed;
        }
 
        /* add an index */
        ldif = ldb_ldif_read_string(ldb, &init_ldif);
-       if (ldif == NULL) goto failed;
-       if (ldb_add(ldb, ldif->msg) != LDB_SUCCESS) goto failed;
+       if (ldif == NULL) {
+               torture_result(torture, TORTURE_FAIL, "Didn't get LDIF data!");
+               goto failed;
+       }
+       if (ldb_add(ldb, ldif->msg) != LDB_SUCCESS) {
+               torture_result(torture, TORTURE_FAIL, "Couldn't apply LDIF data!");
+               talloc_free(ldif);
+               goto failed;
+       }
        talloc_free(ldif);
 
        torture_comment(torture, "Adding %d SID records\n", torture_entries);
 
        for (i=0;i<torture_entries;i++) {
                if (!ldb_add_record(ldb, i)) {
-                       torture_result(torture, TORTURE_FAIL, "Failed to add SID %d\n", i);
+                       torture_result(torture, TORTURE_FAIL, "Failed to add SID %d", i);
                        goto failed;
                }
        }
 
-       if (talloc_total_blocks(torture) > 100) {
-               torture_result(torture, TORTURE_FAIL, "memory leak in ldb add\n");
+       if (talloc_total_blocks(tmp_ctx) > 100) {
+               torture_result(torture, TORTURE_FAIL, "memory leak in ldb add");
                goto failed;
        }
 
@@ -210,27 +220,25 @@ static BOOL test_ldb_speed(struct torture_context *torture, const void *_data)
        for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
                struct ldb_dn *dn;
                struct ldb_result *res;
-               char *expr;
 
                i = random() % torture_entries;
                dn = ldb_dn_new_fmt(tmp_ctx, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", i);
-               if (ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res) != LDB_SUCCESS || res->count != 1) {
-                       torture_fail(torture, talloc_asprintf(torture, "Failed to find SID %d", i));
+               if (ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL) != LDB_SUCCESS || res->count != 1) {
+                       torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
+                       goto failed;
                }
                talloc_free(res);
                talloc_free(dn);
-               expr = talloc_asprintf(tmp_ctx, "(UID=%u)", i);
-               if (ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE, expr, NULL, &res) != LDB_SUCCESS || res->count != 1) {
-                       torture_fail(torture, talloc_asprintf(torture, "Failed to find UID %d", i));
+               if (ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, NULL, "(UID=%u)", i) != LDB_SUCCESS || res->count != 1) {
+                       torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
+                       goto failed;
                }
                talloc_free(res);
-               talloc_free(expr);
        }
 
-       if (talloc_total_blocks(torture) > 100) {
-               unlink("./test.ldb");
-               talloc_free(tmp_ctx);
-               torture_fail(torture, "memory leak in ldb search");
+       if (talloc_total_blocks(tmp_ctx) > 100) {
+               torture_result(torture, TORTURE_FAIL, "memory leak in ldb search");
+               goto failed;
        }
 
        ldb_speed = count/timeval_elapsed(&tv);
@@ -238,21 +246,22 @@ static BOOL test_ldb_speed(struct torture_context *torture, const void *_data)
 
        torture_comment(torture, "ldb/tdb speed ratio is %.2f%%\n", (100*ldb_speed/tdb_speed));
        
-
-       unlink("./test.ldb");
        talloc_free(tmp_ctx);
-       return True;
+       unlink("./test.ldb");
+       return true;
 
 failed:
-       unlink("./test.ldb");
        talloc_free(tmp_ctx);
-       return False;
+       unlink("./test.ldb");
+       return false;
 }
 
 struct torture_suite *torture_local_dbspeed(TALLOC_CTX *mem_ctx)
 {
-       struct torture_suite *s = torture_suite_create(mem_ctx, "DBSPEED");
-       torture_suite_add_simple_tcase(s, "tdb_speed", test_tdb_speed, NULL);
-       torture_suite_add_simple_tcase(s, "ldb_speed", test_ldb_speed, NULL);
+       struct torture_suite *s = torture_suite_create(mem_ctx, "dbspeed");
+       torture_suite_add_simple_tcase_const(s, "tdb_speed", test_tdb_speed,
+                       NULL);
+       torture_suite_add_simple_tcase_const(s, "ldb_speed", test_ldb_speed,
+                       NULL);
        return s;
 }