1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
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., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 *======================================================================*/
22 #include <sys/types.h>
25 #include "lwes_emitter.h"
26 #include "lwes_time_functions.h"
28 static const char help
[] =
29 "lwes-event-testing-emitter [options]" "\n"
31 " where options are:" "\n"
33 " -m [one argument]" "\n"
34 " The multicast ip address to listen on." "\n"
35 " (default: 224.1.1.11)" "\n"
37 " -p [one argument]" "\n"
38 " The ip port to listen on." "\n"
39 " (default: 12345)" "\n"
41 " -i [one argument]" "\n"
42 " The interface to listen on." "\n"
43 " (default: 0.0.0.0)" "\n"
45 " -n [one argument]" "\n"
46 " The number of events per second to emit." "\n"
49 " -s [one argument]" "\n"
50 " The number of seconds to emit for." "\n"
53 " -x [one argument]" "\n"
54 " The number of extra pad bytes to add to each event." "\n"
57 " -b [one argument]" "\n"
58 " The amount of seconds to break for between event bursts." "\n"
62 " Attempt to emit events evenly throughout each second." "\n"
65 " show this message" "\n"
67 " arguments are specified as -option value or -optionvalue" "\n"
73 const char *mcast_ip
= "224.1.1.11";
74 const char *mcast_iface
= NULL
;
75 int mcast_port
= 12345;
82 struct lwes_emitter
* emitter
;
83 struct lwes_event
*event
;
86 /* turn off error messages, I'll handle them */
90 char c
= getopt (argc
, argv
, "m:p:i:n:s:x:b:eh");
100 pause
= atoi(optarg
);
110 mcast_port
= atoi(optarg
);
115 number
= atoi(optarg
);
120 seconds
= atoi(optarg
);
128 fprintf (stderr
, "%s", help
);
133 mcast_iface
= optarg
;
143 "error: unrecognized command line option -%c\n",
153 pad_string
= malloc(pad
+1);
154 if (pad_string
== NULL
)
157 "Unable to allocate %d bytes for pad string\n",
163 memset(pad_string
, 'X', pad
);
164 pad_string
[pad
] = '\0';
168 emitter
= lwes_emitter_create ( (LWES_SHORT_STRING
) mcast_ip
,
169 (LWES_SHORT_STRING
) mcast_iface
,
170 (LWES_U_INT_32
) mcast_port
,
174 assert (emitter
!= NULL
);
178 LWES_INT_64 start
= 0LL;
179 LWES_INT_64 stop
= 0LL;
181 for (s
= 0, n
= 0; s
< seconds
; ++s
)
183 start
= currentTimeMillisLongLong ();
185 for (i
= 0; i
< number
; ++i
, ++n
)
187 event
= lwes_event_create (NULL
, "MyEvent");
188 assert (event
!= NULL
);
191 (lwes_event_set_STRING (event
, "field", "hello world") == 1 );
193 (lwes_event_set_INT_32 (event
, "count", i
) == 2);
195 (lwes_event_set_INT_32 (event
, "run", s
) == 3);
197 (lwes_event_set_STRING (event
, "prog_id","12345") == 4);
199 (lwes_event_set_INT_16 (event
,"num", 2) == 5);
201 (lwes_event_set_STRING (event
,"k0", "a-key.count") == 6);
203 (lwes_event_set_INT_16 (event
,"v0", 1) == 7);
205 (lwes_event_set_STRING (event
,"k1", "b-key.count") == 8);
207 (lwes_event_set_INT_16 (event
,"v1", 2) == 9);
209 (lwes_event_set_U_INT_64(event
, "global_count", n
) == 10);
211 (lwes_event_set_INT_64 (event
,"SentTime", stop
) == 11);
213 assert(lwes_event_set_STRING (event
,"pad",pad_string
) == 12);
216 assert (lwes_emitter_emit (emitter
, event
) == 0);
218 lwes_event_destroy(event
);
219 stop
= currentTimeMillisLongLong ();
221 /* if we are going over our total time, bail out */
222 if ((stop
- start
) >= 1000)
224 printf ("Was only able to emit %7d in 1 sec\n",i
);
228 /* if we are attempting to send events evenly throughout
229 the second, consider pausing briefly to get back on
233 const int delay_ms
= (i
*1000/number
) - (stop
-start
);
236 usleep (delay_ms
*1000);
241 const int delay_ms
= (1000 - (stop
- start
))*1000;
242 if (delay_ms
> 0) usleep (delay_ms
);
246 /* HH/MM/SS DD/MM/YYYY */
247 /* 12345678901234567890 */
248 time_t start_time
= time (NULL
);
250 strftime (timebuff
, 20, "%H:%M:%S %d/%m/%Y", localtime (&start_time
));
252 printf ("%s : %7d\n", timebuff
, i
);
257 lwes_emitter_destroy(emitter
);
258 if (pad_string
!=NULL
) free(pad_string
);