r25554: Convert last instances of BOOL, True and False to the standard types.
[gd/samba-autobuild/.git] / source4 / torture / nbt / query.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT name query testing
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/resolve/resolve.h"
25 #include "torture/torture.h"
26 #include "torture/nbt/proto.h"
27
28 struct result_struct {
29         int num_pass;
30         int num_fail;
31 };
32
33 static void increment_handler(struct nbt_name_request *req)
34 {
35         struct result_struct *v = talloc_get_type(req->async.private, struct result_struct);
36         if (req->state != NBT_REQUEST_DONE) {
37                 v->num_fail++;
38         } else {
39                 v->num_pass++;
40         }
41         talloc_free(req);
42 }
43
44 /*
45   benchmark simple name queries
46 */
47 static bool bench_namequery(struct torture_context *tctx)
48 {
49         struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
50         int num_sent=0;
51         struct result_struct *result;
52         struct nbt_name_query io;
53         struct timeval tv = timeval_current();
54         int timelimit = torture_setting_int(tctx, "timelimit", 5);
55
56         const char *address;
57         struct nbt_name name;
58
59         if (!torture_nbt_get_name(tctx, &name, &address))
60                 return false;
61
62         io.in.name = name;
63         io.in.dest_addr = address;
64         io.in.broadcast = false;
65         io.in.wins_lookup = false;
66         io.in.timeout = 1;
67
68         result = talloc_zero(tctx, struct result_struct);
69
70         torture_comment(tctx, "Running for %d seconds\n", timelimit);
71         while (timeval_elapsed(&tv) < timelimit) {
72                 while (num_sent - (result->num_pass+result->num_fail) < 10) {
73                         struct nbt_name_request *req;
74                         req = nbt_name_query_send(nbtsock, &io);
75                         torture_assert(tctx, req != NULL, "Failed to setup request!");
76                         req->async.fn = increment_handler;
77                         req->async.private = result;
78                         num_sent++;
79                         if (num_sent % 1000 == 0) {
80                                 if (torture_setting_bool(tctx, "progress", true)) {
81                                         torture_comment(tctx, "%.1f queries per second (%d failures)  \r", 
82                                                result->num_pass / timeval_elapsed(&tv),
83                                                result->num_fail);
84                                         fflush(stdout);
85                                 }
86                         }
87                 }
88
89                 event_loop_once(nbtsock->event_ctx);
90         }
91
92         while (num_sent != (result->num_pass + result->num_fail)) {
93                 event_loop_once(nbtsock->event_ctx);
94         }
95
96         torture_comment(tctx, "%.1f queries per second (%d failures)  \n", 
97                result->num_pass / timeval_elapsed(&tv),
98                result->num_fail);
99
100         return true;
101 }
102
103
104 /*
105   benchmark how fast a server can respond to name queries
106 */
107 struct torture_suite *torture_bench_nbt(TALLOC_CTX *mem_ctx)
108 {
109         struct torture_suite *suite = torture_suite_create(mem_ctx, "BENCH");
110         torture_suite_add_simple_test(suite, "namequery", bench_namequery);
111
112         return suite;
113 }