0a021dacceed380c3162c86dd09834dc88c13633
[samba.git] / source / torture / ldap / cldapbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CLDAP benchmark test
5
6    Copyright (C) Andrew Tridgell 2005
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "libcli/cldap/cldap.h"
26
27 struct bench_state {
28         int pass_count, fail_count;
29 };
30
31 static void request_handler(struct cldap_request *req)
32 {
33         struct cldap_netlogon io;
34         struct bench_state *state = talloc_get_type(req->async.private, struct bench_state);
35         NTSTATUS status;
36         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
37         io.in.version = 6;
38         status = cldap_netlogon_recv(req, tmp_ctx, &io);
39         if (NT_STATUS_IS_OK(status)) {
40                 state->pass_count++;
41         } else {
42                 state->fail_count++;
43         }
44         talloc_free(tmp_ctx);
45 }
46
47 /*
48   benchmark cldap calls
49 */
50 static BOOL bench_cldap(TALLOC_CTX *mem_ctx, const char *address)
51 {
52         struct cldap_socket *cldap = cldap_socket_init(mem_ctx, NULL);
53         int num_sent=0;
54         struct timeval tv = timeval_current();
55         BOOL ret = True;
56         int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
57         struct cldap_netlogon search;
58         struct bench_state *state;
59
60         state = talloc_zero(mem_ctx, struct bench_state);
61
62         ZERO_STRUCT(search);
63         search.in.dest_address = address;
64         search.in.acct_control = -1;
65         search.in.version = 6;
66
67         printf("Running for %d seconds\n", timelimit);
68         while (timeval_elapsed(&tv) < timelimit) {
69                 while (num_sent - (state->pass_count+state->fail_count) < 10) {
70                         struct cldap_request *req;
71                         req = cldap_netlogon_send(cldap, &search);
72
73                         req->async.private = state;
74                         req->async.fn = request_handler;
75                         num_sent++;
76                         if (num_sent % 50 == 0) {
77                                 printf("%.1f queries per second (%d failures)  \r", 
78                                        state->pass_count / timeval_elapsed(&tv),
79                                        state->fail_count);
80                         }
81                 }
82
83                 event_loop_once(cldap->event_ctx);
84         }
85
86         while (num_sent != (state->pass_count + state->fail_count)) {
87                 event_loop_once(cldap->event_ctx);
88         }
89
90         printf("%.1f queries per second (%d failures)  \n", 
91                state->pass_count / timeval_elapsed(&tv),
92                state->fail_count);
93
94         talloc_free(cldap);
95         return ret;
96 }
97
98
99 /*
100   benchmark how fast a CLDAP server can respond to a series of parallel
101   requests 
102 */
103 BOOL torture_bench_cldap(void)
104 {
105         const char *address;
106         struct nbt_name name;
107         TALLOC_CTX *mem_ctx = talloc_new(NULL);
108         NTSTATUS status;
109         BOOL ret = True;
110         
111         make_nbt_name_server(&name, lp_parm_string(-1, "torture", "host"));
112
113         /* do an initial name resolution to find its IP */
114         status = resolve_name(&name, mem_ctx, &address);
115         if (!NT_STATUS_IS_OK(status)) {
116                 printf("Failed to resolve %s - %s\n",
117                        name.name, nt_errstr(status));
118                 talloc_free(mem_ctx);
119                 return False;
120         }
121
122         ret &= bench_cldap(mem_ctx, address);
123
124         talloc_free(mem_ctx);
125
126         return ret;
127 }