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