summaryrefslogtreecommitdiff
path: root/IvyPerf/IvyPerf.cs
blob: 794ffd84ca1df26468cff3cf1e3ee1d82555753e (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
using System;
using IvyBus;
using System.Threading;
using System.Globalization;
using System.Collections.Specialized;

namespace IvyPerf
{
	/// <summary>
    /// Description résumée de IvyPerf.
    /// mesure des perfo de round trip entre deux applis
	/// </summary>
	class IvyPerf
	{
		static Ivy bus;
        static double origin = 0;
        static int nbMsgReceive = 0;
        static int nbMsgEmit = 0;
        static int nbMsg = 10;


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

		static double currentTime() // en ms
		{
			double time;
			time = (double)(DateTime.Now.Ticks) / (double)(TimeSpan.TicksPerMillisecond);
			//time = Environment.TickCount;
			return time;
		}
        [IvyBinding("^ping ts=(.*)")]
		static void Reply(object sender, IvyMessageEventArgs args)
		{
			bus.SendMsg("pong ts={0} tr={1}", args[0], currentTime() - origin );
		}
        [IvyBinding("^pong ts=(.*) tr=(.*)")]
        static void Pong(object sender, IvyMessageEventArgs args)
		{
            nbMsgReceive++;

			double current = currentTime() - origin;
			double ts = double.Parse(args[0], bus.Culture );
            double tr = double.Parse(args[1], bus.Culture );
            double roundtrip1 = tr - ts;
            double roundtrip2 = current - tr;
            double roundtrip3 = current - ts;

            if (roundtrip3 > 50)
                Console.WriteLine("slow roundtrip[{0}] min {1} av {2} max {3} ms slow={4}", nbMsgReceive, minRoundTrip, averageRoundTrip, maxRoundTrip, roundtrip3);
            
            if (roundtrip3 > maxRoundTrip) 
            {
                maxRoundTrip = roundtrip3;
            }
            if ( roundtrip3 < minRoundTrip )
            {
                minRoundTrip = roundtrip3;
            }
            averageRoundTrip = (averageRoundTrip * ( nbMsgReceive - 1 ) + roundtrip3) /nbMsgReceive;
            
            if ( nbMsg == nbMsgReceive )
            {
                Console.WriteLine("roundtrip[{0}] min {1} av {2} max {3} ms\n", nbMsgReceive, minRoundTrip, averageRoundTrip, maxRoundTrip);
                    //bus->Stop();
            }
		}
		/// <summary>
		/// Point d'entrée principal de l'application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
            int timeout = 200;
            if (args.Length > 0)
                timeout = int.Parse(args[0]);
            if (args.Length > 1)
                nbMsg = int.Parse(args[1]);

            bus = new Ivy("IvyPerf", "IvyPref ready");
            bus.SentMessageFilter.Add("ping");
            bus.SentMessageFilter.Add("pong");
            bus.SentMessageFilter.Add("IvyPref");
            bus.BindingFilter += new EventHandler<IvyEventArgs>(bus_BindingFilter);
            //TODO auto generation of testtarget ?? how to
            //bus.BindMsg("test", testtarget);
            bus.Start(null);
            origin = currentTime();
            Console.WriteLine("Start Sending {0} messages...", nbMsg);
            while( true )
			{
				Thread.Sleep( timeout );
				int count = bus.SendMsg("ping ts={0}", currentTime() - origin );
				if (count != 0) nbMsgEmit++;
                if (nbMsg == nbMsgEmit)
                {
                    Console.WriteLine("... {0} messages sent ", nbMsg);
                    Thread.Sleep(timeout);
                    break;
                }

			}
		}

        static void bus_BindingFilter(object sender, IvyEventArgs e)
        {
            Console.WriteLine( "The app {0} regexp {1} was Filtred.", e.Client.ApplicationName,e.Argument);
        }
	}
}