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