コード例 #1
0
        private static void MqInProcessPerformanceTests(int runs, int loops, MqMessage message, MqConfig config)
        {
            var server = new MqServer <SimpleMqSession, MqConfig>(config);

            server.Start();

            double[] totalValues = { 0, 0, 0 };

            var count        = 0;
            var sw           = new Stopwatch();
            var wait         = new AutoResetEvent(false);
            var completeTest = new AutoResetEvent(false);

            var client = new MqClient <SimpleMqSession, MqConfig>(config);

            Console.WriteLine("|   Build |   Messages | Msg Bytes | Milliseconds |    Msg/sec |     MBps |");
            Console.WriteLine("|---------|------------|-----------|--------------|------------|----------|");


            var messageSize = message.Size;

            server.IncomingMessage += (sender, args2) =>
            {
                count += args2.Messages.Count;


                if (count == runs)
                {
                    sw.Stop();
                    var mode = "Release";

#if DEBUG
                    mode = "Debug";
#endif

                    var messagesPerSecond = (int)((double)runs / sw.ElapsedMilliseconds * 1000);
                    var msgSizeNoHeader   = messageSize - 12;
                    var mbps = runs * (double)(msgSizeNoHeader) / sw.ElapsedMilliseconds / 1000;
                    Console.WriteLine("| {0,7} | {1,10:N0} | {2,9:N0} | {3,12:N0} | {4,10:N0} | {5,8:N2} |", mode, runs,
                                      msgSizeNoHeader, sw.ElapsedMilliseconds, messagesPerSecond, mbps);
                    totalValues[0] += sw.ElapsedMilliseconds;
                    totalValues[1] += messagesPerSecond;
                    totalValues[2] += mbps;


                    wait.Set();
                }
            };


            var send = new Action(() =>
            {
                count = 0;
                sw.Restart();
                for (var i = 0; i < runs; i++)
                {
                    client.Send(message);
                }
                //MqServer sv = server;
                wait.WaitOne();
                wait.Reset();
            });

            client.Connected += (sender, args) =>
            {
                for (var i = 0; i < loops; i++)
                {
                    send();
                }

                Console.WriteLine("|         |            |  AVERAGES | {0,12:N0} | {1,10:N0} | {2,8:N2} |",
                                  totalValues[0] / loops,
                                  totalValues[1] / loops, totalValues[2] / loops);
                Console.WriteLine();

                server.Stop();
                client.Close();
                completeTest.Set();
            };

            client.Connect();

            completeTest.WaitOne();
        }