s4:torture: use a connected CLDAP socket.
[Samba/nascimento.git] / source4 / torture / ldap / cldapbench.c
bloba63c78944b098cbb4d598da563a793db5090e451
1 /*
2 Unix SMB/CIFS implementation.
4 CLDAP benchmark test
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
22 #include "includes.h"
23 #include "libcli/cldap/cldap.h"
24 #include "libcli/resolve/resolve.h"
25 #include "torture/torture.h"
26 #include "param/param.h"
27 #include "../lib/tsocket/tsocket.h"
29 #define CHECK_VAL(v, correct) torture_assert_int_equal(tctx, (v), (correct), "incorrect value");
31 struct bench_state {
32 struct torture_context *tctx;
33 int pass_count, fail_count;
36 static void request_netlogon_handler(struct tevent_req *req)
38 struct cldap_netlogon io;
39 struct bench_state *state = tevent_req_callback_data(req, struct bench_state);
40 NTSTATUS status;
41 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
42 io.in.version = 6;
43 status = cldap_netlogon_recv(req,
44 lp_iconv_convenience(state->tctx->lp_ctx),
45 tmp_ctx, &io);
46 talloc_free(req);
47 if (NT_STATUS_IS_OK(status)) {
48 state->pass_count++;
49 } else {
50 state->fail_count++;
52 talloc_free(tmp_ctx);
56 benchmark cldap netlogon calls
58 static bool bench_cldap_netlogon(struct torture_context *tctx, const char *address)
60 struct cldap_socket *cldap;
61 int num_sent=0;
62 struct timeval tv = timeval_current();
63 int timelimit = torture_setting_int(tctx, "timelimit", 10);
64 struct cldap_netlogon search;
65 struct bench_state *state;
66 NTSTATUS status;
67 struct tsocket_address *dest_addr;
68 int ret;
70 ret = tsocket_address_inet_from_strings(tctx, "ip",
71 address,
72 lp_cldap_port(tctx->lp_ctx),
73 &dest_addr);
74 CHECK_VAL(ret, 0);
76 status = cldap_socket_init(tctx, tctx->ev, NULL, dest_addr, &cldap);
77 torture_assert_ntstatus_ok(tctx, status, "cldap_socket_init");
79 state = talloc_zero(tctx, struct bench_state);
80 state->tctx = tctx;
82 ZERO_STRUCT(search);
83 search.in.dest_address = NULL;
84 search.in.dest_port = 0;
85 search.in.acct_control = -1;
86 search.in.version = 6;
88 printf("Running CLDAP/netlogon for %d seconds\n", timelimit);
89 while (timeval_elapsed(&tv) < timelimit) {
90 while (num_sent - (state->pass_count+state->fail_count) < 10) {
91 struct tevent_req *req;
92 req = cldap_netlogon_send(state, cldap, &search);
94 tevent_req_set_callback(req, request_netlogon_handler, state);
96 num_sent++;
97 if (num_sent % 50 == 0) {
98 if (torture_setting_bool(tctx, "progress", true)) {
99 printf("%.1f queries per second (%d failures) \r",
100 state->pass_count / timeval_elapsed(&tv),
101 state->fail_count);
102 fflush(stdout);
107 tevent_loop_once(tctx->ev);
110 while (num_sent != (state->pass_count + state->fail_count)) {
111 tevent_loop_once(tctx->ev);
114 printf("%.1f queries per second (%d failures) \n",
115 state->pass_count / timeval_elapsed(&tv),
116 state->fail_count);
118 talloc_free(cldap);
119 return true;
122 static void request_rootdse_handler(struct tevent_req *req)
124 struct cldap_search io;
125 struct bench_state *state = tevent_req_callback_data(req, struct bench_state);
126 NTSTATUS status;
127 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
128 status = cldap_search_recv(req, tmp_ctx, &io);
129 talloc_free(req);
130 if (NT_STATUS_IS_OK(status)) {
131 state->pass_count++;
132 } else {
133 state->fail_count++;
135 talloc_free(tmp_ctx);
139 benchmark cldap netlogon calls
141 static bool bench_cldap_rootdse(struct torture_context *tctx, const char *address)
143 struct cldap_socket *cldap;
144 int num_sent=0;
145 struct timeval tv = timeval_current();
146 int timelimit = torture_setting_int(tctx, "timelimit", 10);
147 struct cldap_search search;
148 struct bench_state *state;
149 NTSTATUS status;
150 struct tsocket_address *dest_addr;
151 int ret;
153 ret = tsocket_address_inet_from_strings(tctx, "ip",
154 address,
155 lp_cldap_port(tctx->lp_ctx),
156 &dest_addr);
157 CHECK_VAL(ret, 0);
159 /* cldap_socket_init should now know about the dest. address */
160 status = cldap_socket_init(tctx, tctx->ev, NULL, dest_addr, &cldap);
161 torture_assert_ntstatus_ok(tctx, status, "cldap_socket_init");
163 state = talloc_zero(tctx, struct bench_state);
165 ZERO_STRUCT(search);
166 search.in.dest_address = NULL;
167 search.in.dest_port = 0;
168 search.in.filter = "(objectClass=*)";
169 search.in.timeout = 2;
170 search.in.retries = 1;
172 printf("Running CLDAP/rootdse for %d seconds\n", timelimit);
173 while (timeval_elapsed(&tv) < timelimit) {
174 while (num_sent - (state->pass_count+state->fail_count) < 10) {
175 struct tevent_req *req;
176 req = cldap_search_send(state, cldap, &search);
178 tevent_req_set_callback(req, request_rootdse_handler, state);
180 num_sent++;
181 if (num_sent % 50 == 0) {
182 if (torture_setting_bool(tctx, "progress", true)) {
183 printf("%.1f queries per second (%d failures) \r",
184 state->pass_count / timeval_elapsed(&tv),
185 state->fail_count);
186 fflush(stdout);
191 tevent_loop_once(tctx->ev);
194 while (num_sent != (state->pass_count + state->fail_count)) {
195 tevent_loop_once(tctx->ev);
198 printf("%.1f queries per second (%d failures) \n",
199 state->pass_count / timeval_elapsed(&tv),
200 state->fail_count);
202 talloc_free(cldap);
203 return true;
207 benchmark how fast a CLDAP server can respond to a series of parallel
208 requests
210 bool torture_bench_cldap(struct torture_context *torture)
212 const char *address;
213 struct nbt_name name;
214 NTSTATUS status;
215 bool ret = true;
217 make_nbt_name_server(&name, torture_setting_string(torture, "host", NULL));
219 /* do an initial name resolution to find its IP */
220 status = resolve_name(lp_resolve_context(torture->lp_ctx), &name, torture, &address, torture->ev);
221 if (!NT_STATUS_IS_OK(status)) {
222 printf("Failed to resolve %s - %s\n",
223 name.name, nt_errstr(status));
224 return false;
227 ret &= bench_cldap_netlogon(torture, address);
228 ret &= bench_cldap_rootdse(torture, address);
230 return ret;