r19806: merge time.c changes
[Samba.git] / source / torture / msgtest.c
blobe8db20f4a846de05a49a04d21cc9bfa4698039a8
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Andrew Tridgell 2000
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.
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.
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.
21 test code for internal messaging
24 #include "includes.h"
26 static int pong_count;
29 /****************************************************************************
30 a useful function for testing the message system
31 ****************************************************************************/
32 void pong_message(int msg_type, struct process_id src, void *buf, size_t len)
34 pong_count++;
37 int main(int argc, char *argv[])
39 pid_t pid;
40 int i, n;
41 char buf[12];
43 load_case_tables();
45 setup_logging(argv[0],True);
47 lp_load(dyn_CONFIGFILE,False,False,False,True);
49 message_init();
51 if (argc != 3) {
52 fprintf(stderr, "%s: Usage - %s pid count\n", argv[0],
53 argv[0]);
54 exit(1);
57 pid = atoi(argv[1]);
58 n = atoi(argv[2]);
60 message_register(MSG_PONG, pong_message);
62 for (i=0;i<n;i++) {
63 message_send_pid(pid_to_procid(pid), MSG_PING, NULL, 0, True);
66 while (pong_count < i) {
67 message_dispatch();
68 smb_msleep(1);
71 /* Now test that the duplicate filtering code works. */
72 pong_count = 0;
74 safe_strcpy(buf, "1234567890", sizeof(buf)-1);
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);
83 for (i=0;i<n;i++) {
84 message_dispatch();
85 smb_msleep(1);
88 if (pong_count != 2) {
89 fprintf(stderr, "Duplicate filter failed (%d).\n", pong_count);
92 /* Speed testing */
94 pong_count = 0;
97 struct timeval tv = timeval_current();
98 size_t timelimit = n;
99 size_t ping_count = 0;
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++;
108 while (ping_count > pong_count + 20) {
109 message_dispatch();
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();
119 if (ping_count != pong_count) {
120 fprintf(stderr, "ping test failed! received %d, sent "
121 "%d\n", pong_count, (int)ping_count);
124 printf("ping rate of %.0f messages/sec\n",
125 (ping_count+pong_count)/timeval_elapsed(&tv));
128 return (0);