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