942c7fbef4d4730a7e09c78570c371c29f3ef065
[kai/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 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 "events.h"
25 #include "libcli/nbt/libnbt.h"
26 #include "librpc/gen_ndr/ndr_nbt.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(TALLOC_CTX *mem_ctx, struct nbt_name *name, const char *address)
48 {
49         struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
50         int num_sent=0;
51         struct result_struct *result;
52         struct nbt_name_query io;
53         struct timeval tv = timeval_current();
54         BOOL ret = True;
55         int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
56
57         io.in.name = *name;
58         io.in.dest_addr = address;
59         io.in.broadcast = False;
60         io.in.wins_lookup = False;
61         io.in.timeout = 1;
62
63         result = talloc_zero(mem_ctx, struct result_struct);
64
65         printf("Running for %d seconds\n", timelimit);
66         while (timeval_elapsed(&tv) < timelimit) {
67                 while (num_sent - (result->num_pass+result->num_fail) < 10) {
68                         struct nbt_name_request *req;
69                         req = nbt_name_query_send(nbtsock, &io);
70                         if (req == NULL) {
71                                 printf("Failed to setup request!\n");
72                                 ret = False;
73                                 goto failed;
74                         }
75                         req->async.fn = increment_handler;
76                         req->async.private = result;
77                         num_sent++;
78                         if (num_sent % 1000 == 0) {
79                                 printf("%.1f queries per second (%d failures)  \r", 
80                                        result->num_pass / timeval_elapsed(&tv),
81                                        result->num_fail);
82                         }
83                 }
84
85                 event_loop_once(nbtsock->event_ctx);
86         }
87
88         while (num_sent != (result->num_pass + result->num_fail)) {
89                 event_loop_once(nbtsock->event_ctx);
90         }
91
92         printf("%.1f queries per second (%d failures)  \n", 
93                result->num_pass / timeval_elapsed(&tv),
94                result->num_fail);
95
96 failed:
97         talloc_free(nbtsock);
98         return ret;
99 }
100
101
102 /*
103   benchmark how fast a server can respond to name queries
104 */
105 BOOL torture_bench_nbt(void)
106 {
107         const char *address;
108         struct nbt_name name;
109         TALLOC_CTX *mem_ctx = talloc_new(NULL);
110         NTSTATUS status;
111         BOOL ret = True;
112         
113         name.name = lp_parm_string(-1, "torture", "host");
114         name.type = NBT_NAME_SERVER;
115         name.scope = NULL;
116
117         /* do an initial name resolution to find its IP */
118         status = resolve_name(&name, mem_ctx, &address);
119         if (!NT_STATUS_IS_OK(status)) {
120                 printf("Failed to resolve %s - %s\n",
121                        name.name, nt_errstr(status));
122                 talloc_free(mem_ctx);
123                 return False;
124         }
125
126         ret &= bench_namequery(mem_ctx, &name, address);
127
128         talloc_free(mem_ctx);
129
130         return ret;
131 }