2006-10-16 00:53:20 +04:00
/*
Unix SMB / CIFS implementation .
local test for tdb / ldb speed
Copyright ( C ) Andrew Tridgell 2004
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-10-16 00:53:20 +04:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-10-16 00:53:20 +04:00
*/
# include "includes.h"
# include "system/filesys.h"
2015-03-12 17:40:16 +03:00
# include <tdb.h>
2011-02-10 06:12:51 +03:00
# include <ldb.h>
# include <ldb_errors.h>
2010-06-16 15:43:38 +04:00
# include "ldb_wrap.h"
2012-03-11 00:33:11 +04:00
# include "lib/tdb_wrap/tdb_wrap.h"
2008-04-27 17:02:46 +04:00
# include "torture/smbtorture.h"
2014-02-27 12:08:17 +04:00
# include "torture/local/proto.h"
2007-10-01 22:52:55 +04:00
# include "param/param.h"
2006-10-16 00:53:20 +04:00
2007-02-18 05:12:50 +03:00
float tdb_speed ;
2006-10-16 00:53:20 +04:00
2014-02-27 12:08:17 +04:00
static bool tdb_add_record ( struct tdb_wrap * tdbw , const char * p1 ,
const char * p2 , int i )
2006-10-16 00:53:20 +04:00
{
TDB_DATA key , data ;
int ret ;
2010-04-11 23:44:20 +04:00
2014-02-27 12:08:17 +04:00
key . dptr = ( uint8_t * ) talloc_asprintf ( tdbw , " %s%u " , p1 , i ) ;
2006-10-16 00:53:20 +04:00
key . dsize = strlen ( ( char * ) key . dptr ) + 1 ;
2014-02-27 12:08:17 +04:00
data . dptr = ( uint8_t * ) talloc_asprintf ( tdbw , " %s%u " , p2 , i + 10000 ) ;
2006-10-16 00:53:20 +04:00
data . dsize = strlen ( ( char * ) data . dptr ) + 1 ;
ret = tdb_store ( tdbw - > tdb , key , data , TDB_INSERT ) ;
talloc_free ( key . dptr ) ;
talloc_free ( data . dptr ) ;
return ret = = 0 ;
}
/*
test tdb speed
*/
2007-10-07 02:28:14 +04:00
static bool test_tdb_speed ( struct torture_context * torture , const void * _data )
2006-10-16 00:53:20 +04:00
{
struct timeval tv ;
struct tdb_wrap * tdbw ;
2006-10-18 18:23:19 +04:00
int timelimit = torture_setting_int ( torture , " timelimit " , 10 ) ;
2006-10-16 00:53:20 +04:00
int i , count ;
TALLOC_CTX * tmp_ctx = talloc_new ( torture ) ;
unlink ( " test.tdb " ) ;
2006-11-03 01:02:37 +03:00
torture_comment ( torture , " Testing tdb speed for sidmap \n " ) ;
2006-10-16 00:53:20 +04:00
2014-03-26 18:41:03 +04:00
tdbw = tdb_wrap_open ( tmp_ctx , " test.tdb " , 10000 ,
lpcfg_tdb_flags ( torture - > lp_ctx , 0 ) ,
O_RDWR | O_CREAT | O_TRUNC , 0600 ) ;
2006-10-16 00:53:20 +04:00
if ( ! tdbw ) {
2010-04-11 23:44:20 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to open test.tdb " ) ;
goto failed ;
2006-10-16 00:53:20 +04:00
}
2006-11-03 01:02:37 +03:00
torture_comment ( torture , " Adding %d SID records \n " , torture_entries ) ;
2006-10-16 00:53:20 +04:00
for ( i = 0 ; i < torture_entries ; i + + ) {
if ( ! tdb_add_record ( tdbw ,
2014-02-27 12:08:17 +04:00
" S-1-5-21-53173311-3623041448-2049097239- " ,
" UID " , i ) ) {
2010-04-11 23:44:20 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to add SID %d! " , i ) ;
2006-10-16 00:53:20 +04:00
goto failed ;
}
if ( ! tdb_add_record ( tdbw ,
2014-02-27 12:08:17 +04:00
" UID " ,
" S-1-5-21-53173311-3623041448-2049097239- " , i ) ) {
2010-04-11 23:44:20 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to add UID %d! " , i ) ;
2006-10-16 00:53:20 +04:00
goto failed ;
}
}
2006-11-03 01:02:37 +03:00
torture_comment ( torture , " Testing for %d seconds \n " , timelimit ) ;
2006-10-16 00:53:20 +04:00
tv = timeval_current ( ) ;
for ( count = 0 ; timeval_elapsed ( & tv ) < timelimit ; count + + ) {
TDB_DATA key , 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 ;
2015-03-12 17:23:17 +03:00
data = tdb_fetch ( tdbw - > tdb , key ) ;
2007-02-18 14:11:57 +03:00
talloc_free ( key . dptr ) ;
2006-10-16 00:53:20 +04:00
if ( data . dptr = = NULL ) {
2010-04-11 23:44:20 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to find SID %d! " , i ) ;
2006-10-16 00:53:20 +04:00
goto failed ;
}
free ( data . dptr ) ;
key . dptr = ( uint8_t * ) talloc_asprintf ( tmp_ctx , " UID %u " , i ) ;
key . dsize = strlen ( ( char * ) key . dptr ) + 1 ;
2015-03-12 17:23:17 +03:00
data = tdb_fetch ( tdbw - > tdb , key ) ;
2007-02-18 14:11:57 +03:00
talloc_free ( key . dptr ) ;
2006-10-16 00:53:20 +04:00
if ( data . dptr = = NULL ) {
2010-04-11 23:44:20 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to find UID %d! " , i ) ;
2006-10-16 00:53:20 +04:00
goto failed ;
}
free ( data . dptr ) ;
}
2007-02-18 05:12:50 +03:00
tdb_speed = count / timeval_elapsed ( & tv ) ;
torture_comment ( torture , " tdb speed %.2f ops/sec \n " , tdb_speed ) ;
2006-10-16 00:53:20 +04:00
talloc_free ( tmp_ctx ) ;
2010-04-11 23:44:20 +04:00
unlink ( " test.tdb " ) ;
2007-10-07 02:28:14 +04:00
return true ;
2006-10-16 00:53:20 +04:00
failed :
talloc_free ( tmp_ctx ) ;
2010-04-11 23:44:20 +04:00
unlink ( " test.tdb " ) ;
2007-10-07 02:28:14 +04:00
return false ;
2006-10-16 00:53:20 +04:00
}
2007-10-07 02:28:14 +04:00
static bool ldb_add_record ( struct ldb_context * ldb , unsigned rid )
2006-10-16 00:53:20 +04:00
{
struct ldb_message * msg ;
int ret ;
msg = ldb_msg_new ( ldb ) ;
if ( msg = = NULL ) {
2007-10-07 02:28:14 +04:00
return false ;
2006-10-16 00:53:20 +04:00
}
2006-11-22 03:59:34 +03:00
msg - > dn = ldb_dn_new_fmt ( msg , ldb , " SID=S-1-5-21-53173311-3623041448-2049097239-%u " , rid ) ;
2006-10-16 00:53:20 +04:00
if ( msg - > dn = = NULL ) {
2010-04-11 13:13:31 +04:00
talloc_free ( msg ) ;
2007-10-07 02:28:14 +04:00
return false ;
2006-10-16 00:53:20 +04:00
}
2010-10-15 22:18:22 +04:00
ret = ldb_msg_add_fmt ( msg , " UID " , " %u " , rid ) ;
if ( ret ! = LDB_SUCCESS ) {
2010-04-11 13:13:31 +04:00
talloc_free ( msg ) ;
2007-10-07 02:28:14 +04:00
return false ;
2006-10-16 00:53:20 +04:00
}
ret = ldb_add ( ldb , msg ) ;
talloc_free ( msg ) ;
return ret = = LDB_SUCCESS ;
}
/*
test ldb speed
*/
2007-10-07 02:28:14 +04:00
static bool test_ldb_speed ( struct torture_context * torture , const void * _data )
2006-10-16 00:53:20 +04:00
{
struct timeval tv ;
struct ldb_context * ldb ;
2006-10-18 18:23:19 +04:00
int timelimit = torture_setting_int ( torture , " timelimit " , 10 ) ;
2006-10-16 00:53:20 +04:00
int i , count ;
TALLOC_CTX * tmp_ctx = talloc_new ( torture ) ;
struct ldb_ldif * ldif ;
const char * init_ldif = " dn: @INDEXLIST \n " \
" @IDXATTR: UID \n " ;
2007-02-18 05:12:50 +03:00
float ldb_speed ;
2006-10-16 00:53:20 +04:00
unlink ( " ./test.ldb " ) ;
2006-11-03 01:02:37 +03:00
torture_comment ( torture , " Testing ldb speed for sidmap \n " ) ;
2006-10-16 00:53:20 +04:00
2010-07-16 08:32:42 +04:00
ldb = ldb_wrap_connect ( tmp_ctx , torture - > ev , torture - > lp_ctx , " tdb://test.ldb " ,
2009-10-23 07:27:00 +04:00
NULL , NULL , LDB_FLG_NOSYNC ) ;
2006-10-16 00:53:20 +04:00
if ( ! ldb ) {
2010-04-11 13:21:19 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to open test.ldb " ) ;
goto failed ;
2006-10-16 00:53:20 +04:00
}
/* add an index */
ldif = ldb_ldif_read_string ( ldb , & init_ldif ) ;
2010-04-11 13:21:19 +04:00
if ( ldif = = NULL ) {
2010-04-11 23:37:23 +04:00
torture_result ( torture , TORTURE_FAIL , " Didn't get LDIF data! " ) ;
2010-04-11 13:21:19 +04:00
goto failed ;
}
if ( ldb_add ( ldb , ldif - > msg ) ! = LDB_SUCCESS ) {
2010-04-11 23:37:23 +04:00
torture_result ( torture , TORTURE_FAIL , " Couldn't apply LDIF data! " ) ;
2010-04-11 13:21:19 +04:00
talloc_free ( ldif ) ;
goto failed ;
}
2006-10-16 00:53:20 +04:00
talloc_free ( ldif ) ;
2006-11-03 01:02:37 +03:00
torture_comment ( torture , " Adding %d SID records \n " , torture_entries ) ;
2006-10-16 00:53:20 +04:00
for ( i = 0 ; i < torture_entries ; i + + ) {
if ( ! ldb_add_record ( ldb , i ) ) {
2010-04-11 23:37:23 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to add SID %d " , i ) ;
2006-10-16 00:53:20 +04:00
goto failed ;
}
}
2010-04-11 13:22:41 +04:00
if ( talloc_total_blocks ( tmp_ctx ) > 100 ) {
2010-04-11 23:37:23 +04:00
torture_result ( torture , TORTURE_FAIL , " memory leak in ldb add " ) ;
2006-10-16 00:53:20 +04:00
goto failed ;
}
2006-11-03 01:02:37 +03:00
torture_comment ( torture , " Testing for %d seconds \n " , timelimit ) ;
2006-10-16 00:53:20 +04:00
tv = timeval_current ( ) ;
for ( count = 0 ; timeval_elapsed ( & tv ) < timelimit ; count + + ) {
struct ldb_dn * dn ;
struct ldb_result * res ;
i = random ( ) % torture_entries ;
2006-11-22 03:59:34 +03:00
dn = ldb_dn_new_fmt ( tmp_ctx , ldb , " SID=S-1-5-21-53173311-3623041448-2049097239-%u " , i ) ;
2008-09-23 22:30:06 +04:00
if ( ldb_search ( ldb , tmp_ctx , & res , dn , LDB_SCOPE_BASE , NULL , NULL ) ! = LDB_SUCCESS | | res - > count ! = 1 ) {
2010-04-11 23:37:23 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to find SID %d! " , i ) ;
2010-04-11 13:21:19 +04:00
goto failed ;
2006-10-16 00:53:20 +04:00
}
talloc_free ( res ) ;
talloc_free ( dn ) ;
2008-09-23 22:30:06 +04:00
if ( ldb_search ( ldb , tmp_ctx , & res , NULL , LDB_SCOPE_SUBTREE , NULL , " (UID=%u) " , i ) ! = LDB_SUCCESS | | res - > count ! = 1 ) {
2010-04-11 23:37:23 +04:00
torture_result ( torture , TORTURE_FAIL , " Failed to find UID %d! " , i ) ;
2010-04-11 13:21:19 +04:00
goto failed ;
2006-10-16 00:53:20 +04:00
}
talloc_free ( res ) ;
}
2006-11-22 03:59:34 +03:00
2010-04-11 13:22:41 +04:00
if ( talloc_total_blocks ( tmp_ctx ) > 100 ) {
2010-04-11 23:37:23 +04:00
torture_result ( torture , TORTURE_FAIL , " memory leak in ldb search " ) ;
2010-04-11 13:21:19 +04:00
goto failed ;
2006-10-16 00:53:20 +04:00
}
2007-02-18 05:12:50 +03:00
ldb_speed = count / timeval_elapsed ( & tv ) ;
torture_comment ( torture , " ldb speed %.2f ops/sec \n " , ldb_speed ) ;
torture_comment ( torture , " ldb/tdb speed ratio is %.2f%% \n " , ( 100 * ldb_speed / tdb_speed ) ) ;
2006-10-16 00:53:20 +04:00
talloc_free ( tmp_ctx ) ;
2010-04-11 13:13:31 +04:00
unlink ( " ./test.ldb " ) ;
2007-10-07 02:28:14 +04:00
return true ;
2006-10-16 00:53:20 +04:00
failed :
talloc_free ( tmp_ctx ) ;
2010-04-11 13:13:31 +04:00
unlink ( " ./test.ldb " ) ;
2007-10-07 02:28:14 +04:00
return false ;
2006-10-16 00:53:20 +04:00
}
struct torture_suite * torture_local_dbspeed ( TALLOC_CTX * mem_ctx )
{
2010-12-11 05:26:31 +03:00
struct torture_suite * s = torture_suite_create ( mem_ctx , " dbspeed " ) ;
2007-12-24 22:04:56 +03:00
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 ) ;
2006-10-16 00:53:20 +04:00
return s ;
}