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_SMALL = 100; private const int ROUNDS_BIG = 1000; private const int DELAY = 0; static void Main(string[] args) { PerformanceTestStandard(); Console.WriteLine(); PerformanceTestSession(); Console.WriteLine(); PerformanceTestProcedure(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Completed."); Console.ReadLine(); } private static void PerformanceTestStandard() { List durations = new List(); Stopwatch watch = new Stopwatch(); for (int i = 0; i < ROUNDS_SMALL; i++) { watch.Restart(); Process process = new Process(); process.StartInfo.FileName = "tangostub.exe"; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.Arguments = "calculate 10 15"; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.UseShellExecute = false; process.Start(); process.WaitForExit(); String response = process.StandardOutput.ReadToEnd(); if (!response.StartsWith("Status: OK")) { OnError(response); return; } if (DELAY > 0) { Thread.Sleep(DELAY); } durations.Add(watch.ElapsedMilliseconds); Console.SetCursorPosition(0, 0); Console.Write($"Performance Test (Standard): {i + 1}/{ROUNDS_SMALL}, Avg: {Math.Round(durations.Average(), 2)} ms "); } } private static void PerformanceTestSession() { List durations = new List(); 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.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_BIG; 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: OK")) { OnError(response); return; } if (DELAY > 0) { Thread.Sleep(DELAY); } durations.Add(watch.ElapsedMilliseconds); Console.SetCursorPosition(0, 1); Console.Write($"Performance Test (Session) : {i + 1}/{ROUNDS_BIG}, Avg: {Math.Round(durations.Average(), 2)} ms "); } } private static void PerformanceTestProcedure() { List durations = new List(); Process process = new Process(); process.StartInfo.FileName = "tangostub_p.exe"; process.StartInfo.Arguments = $"procedure_test.pproj {ROUNDS_BIG} 10 15"; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.UseShellExecute = false; process.Start(); process.PriorityClass = ProcessPriorityClass.RealTime; Stopwatch watch = new Stopwatch(); Stopwatch realWatch = new Stopwatch(); realWatch.Stop(); int count = 0; do { watch.Restart(); String line = process.StandardOutput.ReadLine(); if (count == 0) { realWatch.Start(); } if (line == null) { break; } if (line.StartsWith("Status: Error")) { OnError(process.StandardOutput.ReadToEnd()); return; } else if (line.StartsWith("Status: Completed")) { break; } if (count > 0) { durations.Add(watch.ElapsedMilliseconds); Console.SetCursorPosition(0, 2); Console.Write($"Performance Test (Procedure) : {count + 1}/{ROUNDS_BIG}, Avg: {Math.Round(durations.Average(), 2)} ms "); } count++; } while (!process.HasExited); Console.SetCursorPosition(0, 2); Console.Write($"Performance Test (Procedure) : {ROUNDS_BIG}/{ROUNDS_BIG}, Avg: {Math.Round(realWatch.ElapsedMilliseconds / (double)ROUNDS_BIG, 2)} ms "); } private static void OnError(String error) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(error); Console.ForegroundColor = ConsoleColor.Gray; Console.ReadLine(); } } }