r20261: merge 20260 from samba_3_0_24
[ddiss/samba.git] / source3 / torture / msgtest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Andrew Tridgell 2000
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /*
21   test code for internal messaging
22  */
23
24 #include "includes.h"
25
26 static int pong_count;
27
28
29 /****************************************************************************
30 a useful function for testing the message system
31 ****************************************************************************/
32 static void pong_message(int msg_type, struct process_id src, void *buf, size_t len)
33 {
34         pong_count++;
35 }
36
37  int main(int argc, char *argv[])
38 {
39         pid_t pid;
40         int i, n;
41         char buf[12];
42
43         load_case_tables();
44
45         setup_logging(argv[0],True);
46         
47         lp_load(dyn_CONFIGFILE,False,False,False,True);
48
49         message_init();
50
51         if (argc != 3) {
52                 fprintf(stderr, "%s: Usage - %s pid count\n", argv[0],
53                         argv[0]);
54                 exit(1);
55         }
56
57         pid = atoi(argv[1]);
58         n = atoi(argv[2]);
59
60         message_register(MSG_PONG, pong_message);
61
62         for (i=0;i<n;i++) {
63                 message_send_pid(pid_to_procid(pid), MSG_PING, NULL, 0, True);
64         }
65
66         while (pong_count < i) {
67                 message_dispatch();
68                 smb_msleep(1);
69         }
70
71         /* Now test that the duplicate filtering code works. */
72         pong_count = 0;
73
74         safe_strcpy(buf, "1234567890", sizeof(buf)-1);
75
76         for (i=0;i<n;i++) {
77                 message_send_pid(pid_to_procid(getpid()), MSG_PING,
78                                  NULL, 0, False);
79                 message_send_pid(pid_to_procid(getpid()), MSG_PING,
80                                  buf, 11, False);
81         }
82
83         for (i=0;i<n;i++) {
84                 message_dispatch();
85                 smb_msleep(1);
86         }
87
88         if (pong_count != 2) {
89                 fprintf(stderr, "Duplicate filter failed (%d).\n", pong_count);
90         }
91
92         /* Speed testing */
93
94         pong_count = 0;
95
96         {
97                 struct timeval tv = timeval_current();
98                 size_t timelimit = n;
99                 size_t ping_count = 0;
100
101                 printf("Sending pings for %d seconds\n", (int)timelimit);
102                 while (timeval_elapsed(&tv) < timelimit) {              
103                         if(message_send_pid(pid_to_procid(pid), MSG_PING,
104                                             buf, 11, False)) ping_count++;
105                         if(message_send_pid(pid_to_procid(pid), MSG_PING,
106                                             NULL, 0, False)) ping_count++;
107
108                         while (ping_count > pong_count + 20) {
109                                 message_dispatch();
110                         }
111                 }
112                 
113                 printf("waiting for %d remaining replies (done %d)\n", 
114                        (int)(ping_count - pong_count), pong_count);
115                 while (timeval_elapsed(&tv) < 30 && pong_count < ping_count) {
116                         message_dispatch();
117                 }
118                 
119                 if (ping_count != pong_count) {
120                         fprintf(stderr, "ping test failed! received %d, sent "
121                                 "%d\n", pong_count, (int)ping_count);
122                 }
123                 
124                 printf("ping rate of %.0f messages/sec\n", 
125                        (ping_count+pong_count)/timeval_elapsed(&tv));
126         }
127
128         return (0);
129 }
130