2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2006-2011 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"
54 "[-R %:lo:hi] [-f <filter-template>] [-n <threads>] [-D <bindDN>] [-i <seconds>]\n",
59 static char *filter = "(uid=user.%d)";
61 static char hname[1024];
62 static char *uri = "ldap:///";
67 static int tdur, r1per, r1lo, r1hi, r2per, r2lo, r2hi;
68 static int threads = 1;
70 static int interval = 30;
72 static volatile int *r1binds, *r2binds;
73 static int *r1old, *r2old;
74 static volatile int finish;
77 main( int argc, char **argv )
81 while ( (i = getopt( argc, argv, "b:D:H:w:f:n:i:t:r:R:" )) != EOF ) {
83 case 'b': /* base DN of a tree of user DNs */
84 base = strdup( optarg );
88 binder = strdup( optarg );
91 case 'H': /* the server uri */
92 uri = strdup( optarg );
96 pass = strdup( optarg );
99 case 't': /* the duration to run */
100 if ( lutil_atoi( &tdur, optarg ) != 0 ) {
105 case 'i': /* the time interval */
106 if ( lutil_atoi( &interval, optarg ) != 0 ) {
111 case 'r': /* the uid range */
112 if ( sscanf(optarg, "%d:%d", &r1lo, &r1hi) != 2 ) {
117 case 'R': /* percentage:2nd uid range */
118 if ( sscanf(optarg, "%d:%d:%d", &r2per, &r2lo, &r2hi) != 3 ) {
128 if ( lutil_atoi( &threads, optarg ) != 0 || threads < 1 ) {
139 if ( tdur == 0 || r1hi <= r1lo )
146 r1binds = calloc( threads*4, sizeof( int ));
147 r2binds = r1binds + threads;
148 r1old = (int *)r2binds + threads;
149 r2old = r1old + threads;
153 exit( EXIT_SUCCESS );
157 my_task( void *my_num )
159 LDAP *ld = NULL, *sld = NULL;
161 LDAPMessage *res, *msg;
162 char *attrs[] = { "1.1", NULL };
163 int rc = LDAP_SUCCESS;
164 int tid = *(int *)my_num;
166 ldap_initialize( &ld, uri );
168 perror( "ldap_initialize" );
173 int version = LDAP_VERSION3;
174 (void) ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,
177 (void) ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
179 ldap_initialize( &sld, uri );
181 perror( "ldap_initialize" );
186 int version = LDAP_VERSION3;
187 (void) ldap_set_option( sld, LDAP_OPT_PROTOCOL_VERSION,
190 (void) ldap_set_option( sld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
192 rc = ldap_bind_s( sld, binder, pass, LDAP_AUTH_SIMPLE );
193 if ( rc != LDAP_SUCCESS ) {
194 ldap_perror( sld, "ldap_bind" );
201 char dn[BUFSIZ], *ptr, fstr[256];
212 j = rand() % (r2hi - r2lo + 1 );
216 sprintf(fstr, filter, j);
218 rc = ldap_search_ext( sld, base, LDAP_SCOPE_SUB,
219 fstr, attrs, 0, NULL, NULL, 0, 0, &msgid );
220 if ( rc != LDAP_SUCCESS ) {
221 ldap_perror( sld, "ldap_search_ex" );
225 while (( rc=ldap_result( sld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) >0){
231 for (msg = ldap_first_message( sld, res ); msg;
232 msg = ldap_next_message( sld, msg )) {
233 switch ( ldap_msgtype( msg )) {
234 case LDAP_RES_SEARCH_ENTRY:
235 rc = ldap_get_dn_ber( sld, msg, &ber, &bv );
236 strcpy(dn, bv.bv_val );
239 case LDAP_RES_SEARCH_RESULT:
250 rc = ldap_bind_s( ld, dn, pass, LDAP_AUTH_SIMPLE );
251 if ( rc != LDAP_SUCCESS ) {
252 ldap_perror( ld, "ldap_bind" );
270 time_t now, prevt, start;
273 int dt, dr1, dr2, rr1, rr2;
277 gethostname(hname, sizeof(hname));
278 printf("%s(tid)\tdeltaT\tauth1\tauth2\trate1\trate2\tRate1+2\n", hname);
281 prevt = start = time(0L);
283 for ( i = 0; i<threads; i++ ) {
284 ldap_pvt_thread_t thr;
286 ldap_pvt_thread_create( &thr, 1, my_task, (void *)&r1binds[i] );
290 tv.tv_sec = interval;
293 select(0, NULL, NULL, NULL, &tv);
303 for ( i = 0; i < threads; i++ ) {
307 dr1 = r1new - r1old[i];
308 dr2 = r2new - r2old[i];
312 printf("%s(%d)\t%d\t%d\t%d\t%d\t%d\t%d\n",
313 hname, i, dt, dr1, dr2, rr1, rr2, rr1 + rr2);
325 printf("%s(sum)\t%d\t%d\t%d\t%d\t%d\t%d\n",
326 hname, 0, dr10, dr20, rr1, rr2, rr1 + rr2);
329 if ( now - start >= tdur ) {