aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/StubsUtils/Tango.StubsUtils.PerformanceTest.CLI/Program.cs
blob: 16492a27691ae808befc783372abf81a9e9afe48 (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Tango.StubsUtils.PerformanceTest.CLI
{
    class Program
    {
        private const int ROUNDS = 10000;

        static void Main(string[] args)
        {
            List<double> durations = new List<double>();

            Process process = new Process();
            process.StartInfo.FileName = "tangostub_s.exe";
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            process.PriorityClass = ProcessPriorityClass.RealTime;

            process.StandardOutput.ReadLine(); //Read the first welcome line...

            Stopwatch watch = new Stopwatch();

            for (int i = 0; i < ROUNDS; i++)
            {
                watch.Restart();

                process.StandardInput.WriteLine("calculate 10 15");
                StringBuilder builder = new StringBuilder();

                char[] buffer = new char[1];

                while (process.StandardOutput.Read(buffer, 0, 1) > 0)
                {
                    char c = buffer[0];

                    if (c == '\r')
                    {
                        process.StandardOutput.Read();
                        break;
                    }
                    else if (c == '>')
                    {
                        process.StandardOutput.Read();
                    }
                    else
                    {
                        builder.Append(buffer[0]);
                    }
                }

                String response = builder.ToString();

                if (response.StartsWith("Status: Error"))
                {
                    OnError(response);
                    return;
                }

                durations.Add(watch.ElapsedMilliseconds);

                Console.SetCursorPosition(0, 0);
                Console.Write($"Performance test: {i + 1}/{ROUNDS}, Avg: {Math.Round(durations.Average(), 2)} ms            ");
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Completed.");
            Console.ReadLine();
        }

        private static void OnError(String error)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(error);
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.ReadLine();
        }
    }
}