2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2006-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by Howard Chu for inclusion
17 * in OpenLDAP Software.
24 #include <ac/stdlib.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
33 #include <ac/signal.h>
36 #include <ldap_pvt_thread.h>
42 /* This program is a simplified version of SLAMD's WeightedAuthRate jobclass.
43 * It doesn't offer as much configurability, but it's a good starting point.
44 * When run without the -R option it will behave as a Standard AuthRate job.
45 * Eventually this will grow into a set of C-based load generators for the SLAMD
46 * framework. This code is anywhere from 2 to 10 times more efficient than the
47 * original Java code, allowing servers to be fully loaded without requiring
48 * anywhere near as much load-generation hardware.
53 fprintf( stderr, "usage: %s -H <uri> -b <baseDN> -w <passwd> -t <seconds> -r lo:hi\n\t[-R %:lo:hi] [-f <filter-template>] [-n <threads>]\n",
58 static char *filter = "(uid=user.%d)";
60 static char hname[1024];
61 static char *uri = "ldap:///";
65 static int tdur, r1per, r1lo, r1hi, r2per, r2lo, r2hi;
66 static int threads = 1;
68 static volatile int *r1binds, *r2binds;
69 static int *r1old, *r2old;
70 static volatile int finish;
73 main( int argc, char **argv )
77 while ( (i = getopt( argc, argv, "b:H:w:f:n:t:r:R:" )) != EOF ) {
79 case 'b': /* base DN of a tree of user DNs */
80 base = strdup( optarg );
83 case 'H': /* the server uri */
84 uri = strdup( optarg );
88 pass = strdup( optarg );
91 case 't': /* the duration to run */
92 if ( lutil_atoi( &tdur, optarg ) != 0 ) {
97 case 'r': /* the uid range */
98 if ( sscanf(optarg, "%d:%d", &r1lo, &r1hi) != 2 ) {
103 case 'R': /* percentage:2nd uid range */
104 if ( sscanf(optarg, "%d:%d:%d", &r2per, &r2lo, &r2hi) != 3 ) {
114 if ( lutil_atoi( &threads, optarg ) != 0 || threads < 1 ) {
125 if ( tdur == 0 || r1hi <= r1lo )
132 r1binds = calloc( threads*4, sizeof( int ));
133 r2binds = r1binds + threads;
134 r1old = (int *)r2binds + threads;
135 r2old = r1old + threads;
139 exit( EXIT_SUCCESS );
143 my_task( void *my_num )
147 LDAPMessage *res, *msg;
148 char *attrs[] = { "1.1", NULL };
149 int rc = LDAP_SUCCESS;
150 int tid = *(int *)my_num;
152 ldap_initialize( &ld, uri );
154 perror( "ldap_initialize" );
159 int version = LDAP_VERSION3;
160 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
163 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
168 char dn[BUFSIZ], *ptr, fstr[256];
179 j = rand() % (r2hi - r2lo + 1 );
183 sprintf(fstr, filter, j);
185 rc = ldap_search_ext( ld, base, LDAP_SCOPE_SUB,
186 fstr, attrs, 0, NULL, NULL, 0, 0, &msgid );
187 if ( rc != LDAP_SUCCESS ) {
188 ldap_perror( ld, "ldap_search_ex" );
192 while (( rc=ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
198 for (msg = ldap_first_message( ld, res ); msg;
199 msg = ldap_next_message( ld, msg )) {
200 switch ( ldap_msgtype( msg )) {
201 case LDAP_RES_SEARCH_ENTRY:
202 rc = ldap_get_dn_ber( ld, msg, &ber, &bv );
203 strcpy(dn, bv.bv_val );
206 case LDAP_RES_SEARCH_RESULT:
217 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
218 if ( rc != LDAP_SUCCESS ) {
219 ldap_perror( ld, "ldap_bind" );
236 time_t now, prevt, start;
239 int dt, dr1, dr2, rr1, rr2;
243 gethostname(hname, sizeof(hname));
244 printf("%s(tid)\tdeltaT\tauth1\tauth2\trate1\trate2\tRate1+2\n", hname);
247 prevt = start = time(0L);
249 for ( i = 0; i<threads; i++ ) {
250 ldap_pvt_thread_t thr;
252 ldap_pvt_thread_create( &thr, 1, my_task, (void *)&r1binds[i] );
259 select(0, NULL, NULL, NULL, &tv);
269 for ( i = 0; i < threads; i++ ) {
273 dr1 = r1new - r1old[i];
274 dr2 = r2new - r2old[i];
278 printf("%s(%d)\t%d\t%d\t%d\t%d\t%d\t%d\n",
279 hname, i, dt, dr1, dr2, rr1, rr2, rr1 + rr2);
291 printf("%s(sum)\t%d\t%d\t%d\t%d\t%d\t%d\n",
292 hname, 0, dr10, dr20, rr1, rr2, rr1 + rr2);
295 if ( now - start >= tdur ) {