r12608: Remove some unused #include lines.
[kamenim/samba.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 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
26 struct result_struct {
27         int num_pass;
28         int num_fail;
29 };
30
31 static void increment_handler(struct nbt_name_request *req)
32 {
33         struct result_struct *v = talloc_get_type(req->async.private, struct result_struct);
34         if (req->state != NBT_REQUEST_DONE) {
35                 v->num_fail++;
36         } else {
37                 v->num_pass++;
38         }
39         talloc_free(req);
40 }
41
42 /*
43   benchmark simple name queries
44 */
45 static BOOL bench_namequery(TALLOC_CTX *mem_ctx, struct nbt_name *name, const char *address)
46 {
47         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
48         int num_sent=0;
49         struct result_struct *result;
50         struct nbt_name_query io;
51         struct timeval tv = timeval_current();
52         BOOL ret = True;
53         int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
54
55         io.in.name = *name;
56         io.in.dest_addr = address;
57         io.in.broadcast = False;
58         io.in.wins_lookup = False;
59         io.in.timeout = 1;
60
61         result = talloc_zero(mem_ctx, struct result_struct);
62
63         printf("Running for %d seconds\n", timelimit);
64         while (timeval_elapsed(&tv) < timelimit) {
65                 while (num_sent - (result->num_pass+result->num_fail) < 10) {
66                         struct nbt_name_request *req;
67                         req = nbt_name_query_send(nbtsock, &io);
68                         if (req == NULL) {
69                                 printf("Failed to setup request!\n");
70                                 ret = False;
71                                 goto failed;
72                         }
73                         req->async.fn = increment_handler;
74                         req->async.private = result;
75                         num_sent++;
76                         if (num_sent % 1000 == 0) {
77                                 printf("%.1f queries per second (%d failures)  \r", 
78                                        result->num_pass / timeval_elapsed(&tv),
79                                        result->num_fail);
80                         }
81                 }
82
83                 event_loop_once(nbtsock->event_ctx);
84         }
85
86         while (num_sent != (result->num_pass + result->num_fail)) {
87                 event_loop_once(nbtsock->event_ctx);
88         }
89
90         printf("%.1f queries per second (%d failures)  \n", 
91                result->num_pass / timeval_elapsed(&tv),
92                result->num_fail);
93
94 failed:
95         talloc_free(nbtsock);
96         return ret;
97 }
98
99
100 /*
101   benchmark how fast a server can respond to name queries
102 */
103 BOOL torture_bench_nbt(void)
104 {
105         const char *address;
106         struct nbt_name name;
107         TALLOC_CTX *mem_ctx = talloc_new(NULL);
108         NTSTATUS status;
109         BOOL ret = True;
110         
111         make_nbt_name_server(&name, lp_parm_string(-1, "torture", "host"));
112
113         /* do an initial name resolution to find its IP */
114         status = resolve_name(&name, mem_ctx, &address, event_context_find(mem_ctx));
115         if (!NT_STATUS_IS_OK(status)) {
116                 printf("Failed to resolve %s - %s\n",
117                        name.name, nt_errstr(status));
118                 talloc_free(mem_ctx);
119                 return False;
120         }
121
122         ret &= bench_namequery(mem_ctx, &name, address);
123
124         talloc_free(mem_ctx);
125
126         return ret;
127 }