summaryrefslogtreecommitdiff
path: root/tools/ivyperf.c
blob: a7845f05e59015186abeeac5bc076111cc72511a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 *	Ivy perf mesure le temp de round trip
 *
 *	Copyright (C) 1997-2004
 *	Centre d'Études de la Navigation Aérienne
 *
 * 	Main and only file
 *
 *	Authors: François-Régis Colin <fcolin@cena.fr>
 * 	         Yannick Jestin <jestin@cena.fr>
 *
 *	Please refer to file version.h for the
 *	copyright notice regarding this software
 */

#include "version.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#include "getopt.h"
#ifdef __MINGW32__
#include <regex.h> 
#include <getopt.h>
#endif
#else
#include <sys/time.h>
#include <unistd.h>
#ifdef __INTERIX
extern char *optarg;
extern int optind;
#endif
#endif


#include "ivysocket.h"
#include "ivy.h"
#include "timer.h"
#include "ivyloop.h"
#define MILLISEC 1000.0

const char *mymessages[] = { "IvyPerf", "ping", "pong" };
static double origin = 0;
int nbMsgReceive=0;
int nbMsgEmit=0;
long nbMsg = 10;


double minRoundTrip=1e12;
double maxRoundTrip=0;
double averageRoundTrip=0;

static double currentTime()
{
        double current;
#ifdef WIN32
        current = GetTickCount();
#else
        struct timeval stamp;
        gettimeofday( &stamp, NULL );
        current = (double)stamp.tv_sec * MILLISEC + (double)(stamp.tv_usec/MILLISEC);
#endif
        return  current;
}
TimerId send_timer;


void Reply (IvyClientPtr app, void *user_data, int argc, char *argv[])
{
	IvySendMsg ("pong ts=%s tr=%f", *argv, currentTime()- origin);
}
void Pong (IvyClientPtr app, void *user_data, int argc, char *argv[])
{
	double current;
	double ts;
	double roundtrip3;
	nbMsgReceive++;
	
	current = currentTime() - origin ;
	ts = atof( *argv++ );
	roundtrip3 = current - ts;
	if ( roundtrip3 > maxRoundTrip ) maxRoundTrip = roundtrip3;
	if ( roundtrip3 < minRoundTrip ) minRoundTrip = roundtrip3;
	averageRoundTrip = (averageRoundTrip * ( nbMsgReceive - 1 ) + roundtrip3) /nbMsgReceive;
	
	if ( nbMsg == nbMsgReceive )
	{
		printf("roundtrip[%d] min %f av %f max %f ms\n", nbMsgReceive, minRoundTrip, averageRoundTrip, maxRoundTrip );
		//IvyStop();
	}

}

void TimerCall(TimerId id, void *user_data, unsigned long delta)
{
	int count = IvySendMsg ("ping ts=%f", currentTime() - origin );
	if ( count ) nbMsgEmit++;
	if ( nbMsg == nbMsgEmit )
		{
		TimerRemove(send_timer);
		//IvyStop();
		}
}

void binCB( IvyClientPtr app, void *user_data, int id, const char* regexp,  IvyBindEvent event ) 
{
	const char *app_name = IvyGetApplicationName( app );
	switch ( event )
	{
	case IvyAddBind:
		printf("Application:%s bind '%s' ADDED\n", app_name, regexp );
		break;
	case IvyRemoveBind:
		printf("Application:%s bind '%s' REMOVED\n", app_name, regexp );
		break;
	case IvyChangeBind:
		printf("Application:%s bind '%s' CHANGED\n", app_name, regexp );
		break;
	case IvyFilterBind:
		printf("Application:%s bind '%s' FILTRED\n", app_name, regexp );
		break;

	}
}
int main(int argc, char *argv[])
{
	long time=200;
	int c;
	const char * bus = NULL;
	while ((c = getopt(argc, argv, "b:")) != EOF)
			switch (c) {
			case 'b':
				bus = optarg;
				break;
	}
	/* Mainloop management */
	if ( optind < argc ) time = atol( argv[optind++] );
	if ( optind < argc ) nbMsg = atol( argv[optind] );

	IvyInit ("IvyPerf", "IvyPerf ready", NULL,NULL,NULL,NULL);
	IvySetFilter( sizeof( mymessages )/ sizeof( char *),mymessages );
	IvySetBindCallback( binCB, 0 ),
	IvyBindMsg (Reply, NULL, "^ping ts=(.*)");
	IvyBindMsg (Pong, NULL, "^pong ts=(.*) tr=(.*)");
	  
	origin = currentTime();
	IvyStart (bus);

	if ( nbMsg )
		send_timer = TimerRepeatAfter (TIMER_LOOP, time, TimerCall, (void*)nbMsg);
	

	IvyMainLoop ();
	return 0;
}