c9aef1b01df7485f7cbafe1dc532e60d7dbf60ff
[kamenim/samba.git] / source4 / torture / local / messaging.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local test for messaging code
5
6    Copyright (C) Andrew Tridgell 2004
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 "system/filesys.h"
25 #include "lib/events/events.h"
26 #include "lib/messaging/irpc.h"
27
28 enum {MY_PING=1000, MY_PONG, MY_EXIT};
29
30 static void ping_message(struct messaging_context *msg, void *private, 
31                          uint32_t msg_type, uint32_t src, DATA_BLOB *data)
32 {
33         NTSTATUS status;
34         status = messaging_send(msg, src, MY_PONG, data);
35         if (!NT_STATUS_IS_OK(status)) {
36                 printf("pong failed - %s\n", nt_errstr(status));
37         }
38 }
39
40 static void pong_message(struct messaging_context *msg, void *private, 
41                          uint32_t msg_type, uint32_t src, DATA_BLOB *data)
42 {
43         int *count = private;
44         (*count)++;
45 }
46
47 static void exit_message(struct messaging_context *msg, void *private, 
48                          uint32_t msg_type, uint32_t src, DATA_BLOB *data)
49 {
50         talloc_free(private);
51         exit(0);
52 }
53
54 /*
55   test ping speed
56 */
57 static BOOL test_ping_speed(TALLOC_CTX *mem_ctx)
58 {
59         struct event_context *ev;
60         struct messaging_context *msg_ctx;
61         struct messaging_context *msg_ctx2;
62         int ping_count = 0;
63         int pong_count = 0;
64         BOOL ret = True;
65         struct timeval tv;
66         int timelimit = lp_parm_int(-1, "torture", "timelimit", 10);
67
68         lp_set_cmdline("lock dir", "lockdir.tmp");
69
70         ev = event_context_init(mem_ctx);
71
72         msg_ctx2 = messaging_init(mem_ctx, 1, ev);
73         
74         if (!msg_ctx2) {
75                 exit(1);
76         }
77                 
78         messaging_register(msg_ctx2, NULL, MY_PING, ping_message);
79         messaging_register(msg_ctx2, mem_ctx, MY_EXIT, exit_message);
80
81         msg_ctx = messaging_init(mem_ctx, 2, ev);
82
83         if (!msg_ctx) {
84                 printf("messaging_init() failed\n");
85                 return False;
86         }
87
88         messaging_register(msg_ctx, &pong_count, MY_PONG, pong_message);
89
90         tv = timeval_current();
91
92         printf("Sending pings for %d seconds\n", timelimit);
93         while (timeval_elapsed(&tv) < timelimit) {
94                 DATA_BLOB data;
95                 NTSTATUS status1, status2;
96
97                 data.data = discard_const_p(uint8_t, "testing");
98                 data.length = strlen((const char *)data.data);
99
100                 status1 = messaging_send(msg_ctx, 1, MY_PING, &data);
101                 status2 = messaging_send(msg_ctx, 1, MY_PING, NULL);
102
103                 if (!NT_STATUS_IS_OK(status1)) {
104                         printf("msg1 failed - %s\n", nt_errstr(status1));
105                 } else {
106                         ping_count++;
107                 }
108
109                 if (!NT_STATUS_IS_OK(status2)) {
110                         printf("msg2 failed - %s\n", nt_errstr(status2));
111                 } else {
112                         ping_count++;
113                 }
114
115                 while (ping_count > pong_count + 20) {
116                         event_loop_once(ev);
117                 }
118         }
119
120         printf("waiting for %d remaining replies (done %d)\n", 
121                ping_count - pong_count, pong_count);
122         while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
123                 event_loop_once(ev);
124         }
125
126         printf("sending exit\n");
127         messaging_send(msg_ctx, 1, MY_EXIT, NULL);
128
129         if (ping_count != pong_count) {
130                 printf("ping test failed! received %d, sent %d\n", 
131                        pong_count, ping_count);
132                 ret = False;
133         }
134
135         printf("ping rate of %.0f messages/sec\n", 
136                (ping_count+pong_count)/timeval_elapsed(&tv));
137
138         talloc_free(msg_ctx);
139
140         talloc_free(ev);
141
142         return ret;
143 }
144
145 BOOL torture_local_messaging(void) 
146 {
147         TALLOC_CTX *mem_ctx = talloc_init("torture_local_messaging");
148         BOOL ret = True;
149
150         ret &= test_ping_speed(mem_ctx);
151
152         talloc_free(mem_ctx);
153
154         return ret;
155 }