LCOV - code coverage report
Current view: top level - source4/torture/local - dbspeed.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 104 144 72.2 %
Date: 2024-04-21 15:09:00 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    local test for tdb/ldb speed
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2004
       7             :    
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "system/filesys.h"
      24             : #include <tdb.h>
      25             : #include <ldb.h>
      26             : #include <ldb_errors.h>
      27             : #include "ldb_wrap.h"
      28             : #include "lib/tdb_wrap/tdb_wrap.h"
      29             : #include "torture/smbtorture.h"
      30             : #include "torture/local/proto.h"
      31             : #include "param/param.h"
      32             : 
      33             : float tdb_speed;
      34             : 
      35        2000 : static bool tdb_add_record(struct tdb_wrap *tdbw, const char *p1,
      36             :                            const char *p2, int i)
      37             : {
      38        2000 :         TDB_DATA key, data;
      39        2000 :         int ret;
      40             : 
      41        2000 :         key.dptr = (uint8_t *)talloc_asprintf(tdbw, "%s%u", p1, i);
      42        2000 :         key.dsize = strlen((char *)key.dptr)+1;
      43        2000 :         data.dptr = (uint8_t *)talloc_asprintf(tdbw, "%s%u", p2, i+10000);
      44        2000 :         data.dsize = strlen((char *)data.dptr)+1;
      45             : 
      46        2000 :         ret = tdb_store(tdbw->tdb, key, data, TDB_INSERT);
      47             : 
      48        2000 :         talloc_free(key.dptr);
      49        2000 :         talloc_free(data.dptr);
      50        2000 :         return ret == 0;
      51             : }
      52             : 
      53             : /*
      54             :   test tdb speed
      55             : */
      56           1 : static bool test_tdb_speed(struct torture_context *torture, const void *_data)
      57             : {
      58           1 :         struct timeval tv;
      59           1 :         struct tdb_wrap *tdbw;
      60           1 :         int timelimit = torture_setting_int(torture, "timelimit", 10);
      61           1 :         int i, count;
      62           1 :         TALLOC_CTX *tmp_ctx = talloc_new(torture);
      63             : 
      64           1 :         unlink("test.tdb");
      65             : 
      66           1 :         torture_comment(torture, "Testing tdb speed for sidmap\n");
      67             : 
      68           1 :         tdbw = tdb_wrap_open(tmp_ctx, "test.tdb", 10000,
      69             :                              lpcfg_tdb_flags(torture->lp_ctx, 0),
      70             :                              O_RDWR|O_CREAT|O_TRUNC, 0600);
      71           1 :         if (!tdbw) {
      72           0 :                 torture_result(torture, TORTURE_FAIL, "Failed to open test.tdb");
      73           0 :                 goto failed;
      74             :         }
      75             : 
      76           1 :         torture_comment(torture, "Adding %d SID records\n", torture_entries);
      77             : 
      78        1002 :         for (i=0;i<torture_entries;i++) {
      79        1000 :                 if (!tdb_add_record(tdbw, 
      80             :                                     "S-1-5-21-53173311-3623041448-2049097239-",
      81             :                                     "UID ", i)) {
      82           0 :                         torture_result(torture, TORTURE_FAIL, "Failed to add SID %d!", i);
      83           0 :                         goto failed;
      84             :                 }
      85        1000 :                 if (!tdb_add_record(tdbw, 
      86             :                                     "UID ",
      87             :                                     "S-1-5-21-53173311-3623041448-2049097239-", i)) {
      88           0 :                         torture_result(torture, TORTURE_FAIL, "Failed to add UID %d!", i);
      89           0 :                         goto failed;
      90             :                 }
      91             :         }
      92             : 
      93           1 :         torture_comment(torture, "Testing for %d seconds\n", timelimit);
      94             : 
      95           1 :         tv = timeval_current();
      96             : 
      97      463582 :         for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
      98      463580 :                 TDB_DATA key, data;
      99      463580 :                 i = random() % torture_entries;
     100      463580 :                 key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "S-1-5-21-53173311-3623041448-2049097239-%u", i);
     101      463580 :                 key.dsize = strlen((char *)key.dptr)+1;
     102      463580 :                 data = tdb_fetch(tdbw->tdb, key);
     103      463580 :                 talloc_free(key.dptr);
     104      463580 :                 if (data.dptr == NULL) {
     105           0 :                         torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
     106           0 :                         goto failed;
     107             :                 }
     108      463580 :                 free(data.dptr);
     109      463580 :                 key.dptr = (uint8_t *)talloc_asprintf(tmp_ctx, "UID %u", i);
     110      463580 :                 key.dsize = strlen((char *)key.dptr)+1;
     111      463580 :                 data = tdb_fetch(tdbw->tdb, key);
     112      463580 :                 talloc_free(key.dptr);
     113      463580 :                 if (data.dptr == NULL) {
     114           0 :                         torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
     115           0 :                         goto failed;
     116             :                 }
     117      463580 :                 free(data.dptr);
     118             :         }
     119             : 
     120           1 :         tdb_speed = count/timeval_elapsed(&tv);
     121           1 :         torture_comment(torture, "tdb speed %.2f ops/sec\n", tdb_speed);
     122             :         
     123           1 :         talloc_free(tmp_ctx);
     124           1 :         unlink("test.tdb");
     125           1 :         return true;
     126             : 
     127           0 : failed:
     128           0 :         talloc_free(tmp_ctx);
     129           0 :         unlink("test.tdb");
     130           0 :         return false;
     131             : }
     132             : 
     133             : 
     134        1000 : static bool ldb_add_record(struct ldb_context *ldb, unsigned rid)
     135             : {
     136        1000 :         struct ldb_message *msg;        
     137        1000 :         int ret;
     138             : 
     139        1000 :         msg = ldb_msg_new(ldb);
     140        1000 :         if (msg == NULL) {
     141           0 :                 return false;
     142             :         }
     143             : 
     144        1000 :         msg->dn = ldb_dn_new_fmt(msg, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", rid);
     145        1000 :         if (msg->dn == NULL) {
     146           0 :                 talloc_free(msg);
     147           0 :                 return false;
     148             :         }
     149             : 
     150        1000 :         ret = ldb_msg_add_fmt(msg, "UID", "%u", rid);
     151        1000 :         if (ret != LDB_SUCCESS) {
     152           0 :                 talloc_free(msg);
     153           0 :                 return false;
     154             :         }
     155             : 
     156        1000 :         ret = ldb_add(ldb, msg);
     157             : 
     158        1000 :         talloc_free(msg);
     159             : 
     160        1000 :         return ret == LDB_SUCCESS;
     161             : }
     162             : 
     163             : 
     164             : /*
     165             :   test ldb speed
     166             : */
     167           1 : static bool test_ldb_speed(struct torture_context *torture, const void *_data)
     168             : {
     169           1 :         struct timeval tv;
     170           1 :         struct ldb_context *ldb;
     171           1 :         int timelimit = torture_setting_int(torture, "timelimit", 10);
     172           1 :         int i, count;
     173           1 :         TALLOC_CTX *tmp_ctx = talloc_new(torture);
     174           1 :         struct ldb_ldif *ldif;
     175           1 :         const char *init_ldif = "dn: @INDEXLIST\n" \
     176             :                 "@IDXATTR: UID\n";
     177           1 :         float ldb_speed;
     178             : 
     179           1 :         unlink("./test.ldb");
     180             : 
     181           1 :         torture_comment(torture, "Testing ldb speed for sidmap\n");
     182             : 
     183           1 :         ldb = ldb_wrap_connect(tmp_ctx, torture->ev, torture->lp_ctx, "tdb://test.ldb",
     184             :                                 NULL, NULL, LDB_FLG_NOSYNC);
     185           1 :         if (!ldb) {
     186           0 :                 torture_result(torture, TORTURE_FAIL, "Failed to open test.ldb");
     187           0 :                 goto failed;
     188             :         }
     189             : 
     190             :         /* add an index */
     191           1 :         ldif = ldb_ldif_read_string(ldb, &init_ldif);
     192           1 :         if (ldif == NULL) {
     193           0 :                 torture_result(torture, TORTURE_FAIL, "Didn't get LDIF data!");
     194           0 :                 goto failed;
     195             :         }
     196           1 :         if (ldb_add(ldb, ldif->msg) != LDB_SUCCESS) {
     197           0 :                 torture_result(torture, TORTURE_FAIL, "Couldn't apply LDIF data!");
     198           0 :                 talloc_free(ldif);
     199           0 :                 goto failed;
     200             :         }
     201           1 :         talloc_free(ldif);
     202             : 
     203           1 :         torture_comment(torture, "Adding %d SID records\n", torture_entries);
     204             : 
     205        1002 :         for (i=0;i<torture_entries;i++) {
     206        1000 :                 if (!ldb_add_record(ldb, i)) {
     207           0 :                         torture_result(torture, TORTURE_FAIL, "Failed to add SID %d", i);
     208           0 :                         goto failed;
     209             :                 }
     210             :         }
     211             : 
     212           1 :         if (talloc_total_blocks(tmp_ctx) > 100) {
     213           0 :                 torture_result(torture, TORTURE_FAIL, "memory leak in ldb add");
     214           0 :                 goto failed;
     215             :         }
     216             : 
     217           1 :         torture_comment(torture, "Testing for %d seconds\n", timelimit);
     218             : 
     219           1 :         tv = timeval_current();
     220             : 
     221       53065 :         for (count=0;timeval_elapsed(&tv) < timelimit;count++) {
     222       53063 :                 struct ldb_dn *dn;
     223       53063 :                 struct ldb_result *res;
     224             : 
     225       53063 :                 i = random() % torture_entries;
     226       53063 :                 dn = ldb_dn_new_fmt(tmp_ctx, ldb, "SID=S-1-5-21-53173311-3623041448-2049097239-%u", i);
     227       53063 :                 if (ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL) != LDB_SUCCESS || res->count != 1) {
     228           0 :                         torture_result(torture, TORTURE_FAIL, "Failed to find SID %d!", i);
     229           0 :                         goto failed;
     230             :                 }
     231       53063 :                 talloc_free(res);
     232       53063 :                 talloc_free(dn);
     233       53063 :                 if (ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE, NULL, "(UID=%u)", i) != LDB_SUCCESS || res->count != 1) {
     234           0 :                         torture_result(torture, TORTURE_FAIL, "Failed to find UID %d!", i);
     235           0 :                         goto failed;
     236             :                 }
     237       53063 :                 talloc_free(res);
     238             :         }
     239             : 
     240           1 :         if (talloc_total_blocks(tmp_ctx) > 100) {
     241           0 :                 torture_result(torture, TORTURE_FAIL, "memory leak in ldb search");
     242           0 :                 goto failed;
     243             :         }
     244             : 
     245           1 :         ldb_speed = count/timeval_elapsed(&tv);
     246           1 :         torture_comment(torture, "ldb speed %.2f ops/sec\n", ldb_speed);
     247             : 
     248           1 :         torture_comment(torture, "ldb/tdb speed ratio is %.2f%%\n", (100*ldb_speed/tdb_speed));
     249             :         
     250           1 :         talloc_free(tmp_ctx);
     251           1 :         unlink("./test.ldb");
     252           1 :         return true;
     253             : 
     254           0 : failed:
     255           0 :         talloc_free(tmp_ctx);
     256           0 :         unlink("./test.ldb");
     257           0 :         return false;
     258             : }
     259             : 
     260        2354 : struct torture_suite *torture_local_dbspeed(TALLOC_CTX *mem_ctx)
     261             : {
     262        2354 :         struct torture_suite *s = torture_suite_create(mem_ctx, "dbspeed");
     263        2354 :         torture_suite_add_simple_tcase_const(s, "tdb_speed", test_tdb_speed,
     264             :                         NULL);
     265        2354 :         torture_suite_add_simple_tcase_const(s, "ldb_speed", test_ldb_speed,
     266             :                         NULL);
     267        2354 :         return s;
     268             : }

Generated by: LCOV version 1.14