]> git.sur5r.net Git - openldap/blob - tests/slapd-search.c
More timing for performance testing. Re-introduction of cache.c_mutex.
[openldap] / tests / slapd-search.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/string.h>
6 #include <ac/ctype.h>
7 #include <ac/socket.h>
8 #include <ac/unistd.h>
9 #include <ac/wait.h>
10
11 #include <sys/param.h>
12
13 #include "lber.h"
14 #include "ldap.h"
15
16 #define LOOPS   100
17
18 static void
19 do_search( char *host, int port, char *sbase, char *filter, int maxloop );
20
21 static void
22 usage( char *name )
23 {
24         fprintf( stderr, "usage: %s [-h <host>] -p port -b <searchbase> -f <searchfiter> [-l <loops>]\n",
25                         name );
26         exit( 1 );
27 }
28
29 int
30 main( int argc, char **argv )
31 {
32         int             i, j;
33         char        *host = "localhost";
34         int                     port = -1;
35         char        *sbase = NULL;
36         char            *filter  = NULL;
37         int                     loops = LOOPS;
38
39         while ( (i = getopt( argc, argv, "h:p:b:f:l:" )) != EOF ) {
40                 switch( i ) {
41                         case 'h':               /* the servers host */
42                                 host = strdup( optarg );
43                         break;
44
45                         case 'p':               /* the servers port */
46                                 port = atoi( optarg );
47                                 break;
48
49                         case 'b':               /* file with search base */
50                                 sbase = strdup( optarg );
51                         break;
52
53                         case 'f':               /* the search request */
54                                 filter = strdup( optarg );
55                                 break;
56
57                         case 'l':               /* number of loops */
58                                 loops = atoi( optarg );
59                                 break;
60
61                         default:
62                                 usage( argv[0] );
63                                 break;
64                 }
65         }
66
67         if (( sbase == NULL ) || ( filter == NULL ) || ( port == -1 ))
68                 usage( argv[0] );
69
70         if ( *filter == '\0' ) {
71
72                 fprintf( stderr, "%s: invalid EMPTY search filter.\n",
73                                 argv[0] );
74                 exit( 1 );
75
76         }
77
78         do_search( host, port, sbase, filter, ( 4 * loops ));
79
80         exit( 0 );
81 }
82
83
84 static void
85 do_search( char *host, int port, char *sbase, char *filter, int maxloop )
86 {
87         LDAP    *ld;
88         int     i;
89         char    *attrs[] = { "cn", "sn", NULL };
90         pid_t   pid = getpid();
91
92         if (( ld = ldap_init( host, port )) == NULL ) {
93                 perror( "ldap_init" );
94                 exit( 1 );
95         }
96
97         if ( ldap_bind_s( ld, NULL, NULL, LDAP_AUTH_SIMPLE ) != LDAP_SUCCESS ) {
98                 ldap_perror( ld, "ldap_bind" );
99                  exit( 1 );
100         }
101
102
103         fprintf( stderr, "PID=%ld - Search(%d): base=\"%s\", filter=\"%s\".\n",
104                                 pid, maxloop, sbase, filter );
105
106         for ( i = 0; i < maxloop; i++ ) {
107                 LDAPMessage *res;
108                 int         rc;
109
110                 if (( rc = ldap_search_s( ld, sbase, LDAP_SCOPE_SUBTREE,
111                                 filter, attrs, 0, &res )) != LDAP_SUCCESS ) {
112
113                         ldap_perror( ld, "ldap_search" );
114                         if ( rc != LDAP_NO_SUCH_OBJECT ) break;
115                         continue;
116
117                 }
118
119                 ldap_msgfree( res );
120         }
121
122         fprintf( stderr, " PID=%ld - Search done.\n", pid );
123
124         ldap_unbind( ld );
125 }
126
127