r22579: disable progress printing in the build-farm
[kai/samba.git] / source4 / torture / nbt / winsbench.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    WINS 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 "lib/socket/socket.h"
26 #include "libcli/resolve/resolve.h"
27 #include "system/network.h"
28 #include "lib/socket/netif.h"
29 #include "torture/torture.h"
30 #include "torture/nbt/proto.h"
31
32 struct wins_state {
33         int num_names;
34         bool *registered;
35         int pass_count;
36         int fail_count;
37         const char *wins_server;
38         const char *my_ip;
39         uint32_t ttl;
40 };
41
42 struct idx_state {
43         int idx;
44         struct wins_state *state;
45 };
46
47 struct nbt_name generate_name(TALLOC_CTX *tctx, int idx)
48 {
49         struct nbt_name name;
50         name.name       = talloc_asprintf(tctx, "WINSBench%6u", idx);
51         name.type       = 0x4;
52         name.scope      = NULL;
53         return name;
54 }
55
56 static void register_handler(struct nbt_name_request *req)
57 {
58         struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
59         struct wins_state *state = istate->state;
60         struct nbt_name_register io;
61         NTSTATUS status;
62
63         status = nbt_name_register_recv(req, istate, &io);
64         if (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK) {
65                 state->fail_count++;
66         } else {
67                 state->pass_count++;
68                 state->registered[istate->idx] = true;
69         }
70         talloc_free(istate);    
71 }
72
73 /*
74   generate a registration
75 */
76 static void generate_register(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
77 {
78         struct nbt_name_register io;
79         TALLOC_CTX *tmp_ctx = talloc_new(state);
80         struct nbt_name_request *req;
81         struct idx_state *istate;
82
83         istate = talloc(nbtsock, struct idx_state);
84         istate->idx = idx;
85         istate->state = state;
86
87         io.in.name            = generate_name(tmp_ctx, idx);
88         io.in.dest_addr       = state->wins_server;
89         io.in.address         = state->my_ip;
90         io.in.nb_flags        = NBT_NODE_H;
91         io.in.register_demand = false;
92         io.in.broadcast       = false;
93         io.in.multi_homed     = false;
94         io.in.ttl             = state->ttl;
95         io.in.timeout         = 2;
96         io.in.retries         = 1;
97
98         req = nbt_name_register_send(nbtsock, &io);
99
100         req->async.fn = register_handler;
101         req->async.private = istate;
102
103         talloc_free(tmp_ctx);
104 }
105
106
107 static void release_handler(struct nbt_name_request *req)
108 {
109         struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
110         struct wins_state *state = istate->state;
111         struct nbt_name_release io;
112         NTSTATUS status;
113
114         status = nbt_name_release_recv(req, istate, &io);
115         if (state->registered[istate->idx] && 
116             (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK)) {
117                 state->fail_count++;
118         } else {
119                 state->pass_count++;
120                 state->registered[istate->idx] = false;
121         }
122         talloc_free(istate);    
123 }
124
125 /*
126   generate a name release
127 */
128 static void generate_release(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
129 {
130         struct nbt_name_release io;
131         TALLOC_CTX *tmp_ctx = talloc_new(state);
132         struct nbt_name_request *req;
133         struct idx_state *istate;
134
135         istate = talloc(nbtsock, struct idx_state);
136         istate->idx = idx;
137         istate->state = state;
138
139         io.in.name            = generate_name(tmp_ctx, idx);
140         io.in.dest_addr       = state->wins_server;
141         io.in.address         = state->my_ip;
142         io.in.nb_flags        = NBT_NODE_H;
143         io.in.broadcast       = false;
144         io.in.timeout         = 2;
145         io.in.retries         = 1;
146
147         req = nbt_name_release_send(nbtsock, &io);
148
149         req->async.fn = release_handler;
150         req->async.private = istate;
151
152         talloc_free(tmp_ctx);
153 }
154
155
156 static void query_handler(struct nbt_name_request *req)
157 {
158         struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
159         struct wins_state *state = istate->state;
160         struct nbt_name_query io;
161         NTSTATUS status;
162
163         status = nbt_name_query_recv(req, istate, &io);
164         if (!NT_STATUS_IS_OK(status) && state->registered[istate->idx]) {
165                 state->fail_count++;
166         } else {
167                 state->pass_count++;
168         }
169         talloc_free(istate);    
170 }
171
172 /*
173   generate a name query
174 */
175 static void generate_query(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
176 {
177         struct nbt_name_query io;
178         TALLOC_CTX *tmp_ctx = talloc_new(state);
179         struct nbt_name_request *req;
180         struct idx_state *istate;
181
182         istate = talloc(nbtsock, struct idx_state);
183         istate->idx = idx;
184         istate->state = state;
185
186         io.in.name            = generate_name(tmp_ctx, idx);
187         io.in.dest_addr       = state->wins_server;
188         io.in.broadcast       = false;
189         io.in.wins_lookup     = true;
190         io.in.timeout         = 2;
191         io.in.retries         = 1;
192
193         req = nbt_name_query_send(nbtsock, &io);
194
195         req->async.fn = query_handler;
196         req->async.private = istate;
197
198         talloc_free(tmp_ctx);
199 }
200
201 /*
202   generate one WINS request
203 */
204 static void generate_request(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
205 {
206         if (random() % 5 == 0) {
207                 generate_register(nbtsock, state, idx);
208                 return;
209         }
210
211         if (random() % 20 == 0) {
212                 generate_release(nbtsock, state, idx);
213                 return;
214         }
215
216         generate_query(nbtsock, state, idx);
217 }
218
219 /*
220   benchmark simple name queries
221 */
222 static bool bench_wins(struct torture_context *tctx)
223 {
224         struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
225         int num_sent=0;
226         struct timeval tv = timeval_current();
227         bool ret = true;
228         int timelimit = torture_setting_int(tctx, "timelimit", 5);
229         struct wins_state *state;
230         extern int torture_entries;
231         struct socket_address *my_ip;
232         struct nbt_name name;
233         const char *address;
234
235         if (!torture_nbt_get_name(tctx, &name, &address))
236                 return false;
237
238         state = talloc_zero(nbtsock, struct wins_state);
239
240         state->num_names = torture_entries;
241         state->registered = talloc_zero_array(state, bool, state->num_names);
242         state->wins_server = address;
243         state->my_ip = talloc_strdup(tctx, iface_best_ip(address));
244         state->ttl = timelimit;
245
246         my_ip = socket_address_from_strings(nbtsock, nbtsock->sock->backend_name, 
247                                             state->my_ip, 0);
248
249         socket_listen(nbtsock->sock, my_ip, 0, 0);
250
251         torture_comment(tctx, "Running for %d seconds\n", timelimit);
252         while (timeval_elapsed(&tv) < timelimit) {
253                 while (num_sent - (state->pass_count+state->fail_count) < 10) {
254                         generate_request(nbtsock, state, num_sent % state->num_names);
255                         num_sent++;
256                         if (num_sent % 50 == 0) {
257                                 if (torture_setting_bool(tctx, "progress", true)) {
258                                         torture_comment(tctx, "%.1f queries per second (%d failures)  \r", 
259                                                state->pass_count / timeval_elapsed(&tv),
260                                                state->fail_count);
261                                         fflush(stdout);
262                                 }
263                         }
264                 }
265
266                 event_loop_once(nbtsock->event_ctx);
267         }
268
269         while (num_sent != (state->pass_count + state->fail_count)) {
270                 event_loop_once(nbtsock->event_ctx);
271         }
272
273         torture_comment(tctx, "%.1f queries per second (%d failures)  \n", 
274                state->pass_count / timeval_elapsed(&tv),
275                state->fail_count);
276
277         talloc_free(nbtsock);
278         return ret;
279 }
280
281
282 /*
283   benchmark how fast a WINS server can respond to a mixture of
284   registration/refresh/release and name query requests
285 */
286 struct torture_suite *torture_bench_wins(void)
287 {
288         struct torture_suite *suite = torture_suite_create(
289                                                                                 talloc_autofree_context(),
290                                                                                 "BENCH-WINS");
291
292         torture_suite_add_simple_test(suite, "wins", bench_wins);
293
294         return suite;
295 }