public void RunPerfTest() { CancellationTokens.ResetPerformanceCancellationToken(); CancellationTokens.GetPerformanceCancellationToken(); //run program on server Task.Factory.StartNew(Controller.RunTest); Controller.ShowRunningTestViewDialog(); }
/// <summary> /// Runs performance test /// </summary> public static void RunTest() { WCFLoad.Test.MethodLevelResults = new ConcurrentDictionary <string, MethodLogs>(); WCFLoad.Test.MethodLevelReqStatus = new ConcurrentDictionary <string, MethodLogs>(); WCFLoad.Test.ClearCache(); //Reset total clients ApplicationData.TotalClientsStarted = 0; IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; FbHelper.SetInitialValues(ipAddress.ToString(), true, Environment.CurrentDirectory + "\\firebird_filesmydb.fdb"); FbHelper.CreateDataBase(); FbServiceController.StartService(); CreateClients(); ServiceClient.ServerIpAddress = "localhost"; ServiceClient.ServerPort = "9090"; WCFLoad.Test.PerformanceRunToken = CancellationTokens.GetPerformanceCancellationToken(); if (!string.IsNullOrEmpty(WCFLoad.Test.TestPackage.ResultFileName)) { if (File.Exists(WCFLoad.Test.TestPackage.ResultFileName)) { File.Delete(WCFLoad.Test.TestPackage.ResultFileName); } } IsTestCompleted = false; int scenariosCount = WCFLoad.Test.TestPackage.Scenarios.Count; int duration = WCFLoad.Test.TestPackage.Duration; int clients = WCFLoad.Test.TestPackage.Clients; if (WCFLoad.Test.TestPackage.Nodes.NodeList.Count > 0) { clients = WCFLoad.Test.TestPackage.Clients / WCFLoad.Test.TestPackage.Nodes.NodeList.Count; } Stopwatch testDurationTimer = new Stopwatch(); testDurationTimer.Start(); int requestSent = 0; int delayRangeStart = WCFLoad.Test.TestPackage.DelayRangeStart; int delayRangeEnd = WCFLoad.Test.TestPackage.DelayRangeEnd; int clientsSpawned = 0; Task[] clientTasks = new Task[clients]; while (clientsSpawned < clients) { int clientIndex = clientsSpawned; clientTasks[clientIndex] = new Task(() => { int currentScenario = -1; int currentScenarioOrder = -1; while (testDurationTimer.ElapsedMilliseconds <= (duration * 60 * 1000) && !CancellationTokens.GetPerformanceCancellationToken().IsCancellationRequested) { if (scenariosCount == 0) { Random r = new Random(); bool executeWcf = r.Next(0, 1000) % 2 == 0; if (WCFLoad.Test.TestPackage.Suites.Count == 0) { executeWcf = false; } if (executeWcf || WCFLoad.Test.TestPackage.RestMethods.Count == 0) { int suiteNumberToExecute = r.Next(0, WCFLoad.Test.TestPackage.Suites.Count - 1); int testNumberToExecute = r.Next(0, WCFLoad.Test.TestPackage.Suites[suiteNumberToExecute].Tests.Count - 1); var testToExecute = WCFLoad.Test.TestPackage.Suites[suiteNumberToExecute].Tests[testNumberToExecute]; WCFLoad.Test.InvokeTest(testToExecute, WCFLoad.Test.TestPackage.Suites[suiteNumberToExecute].Guid); requestSent++; } else { int suiteNumberToExecute = r.Next(0, WCFLoad.Test.TestPackage.RestMethods.Count - 1); Task.Factory.StartNew(() => { WCFLoad.Test.InvokeRestApi( WCFLoad.Test.TestPackage.RestMethods[suiteNumberToExecute], true); }); } requestSent++; Thread.Sleep(r.Next(delayRangeStart, delayRangeEnd)); } else { if (currentScenario == -1) { Random r = new Random(); currentScenario = r.Next(0, scenariosCount - 1); } var scen = WCFLoad.Test.TestPackage.Scenarios[currentScenario]; if (currentScenarioOrder == -1) { currentScenarioOrder = 0; } var order = currentScenarioOrder; var ss = (from s in scen.ScenarioOrder where s.Order == order select s).First(); int totalMethodsInScenario = scen.ScenarioOrder.Count; if (!ss.IsRest) { var methodName = ss.MethodName; var tts = from suite in WCFLoad.Test.TestPackage.Suites from tt in suite.Tests where tt.Service.MethodName == methodName && ss.AssemblyGuid == suite.Guid select tt; Test testToExecute = tts.ElementAt(0); var valueToUse = (from v in testToExecute.Service.Values.ValueList where v.Guid == ss.MethodGuid select v).First(); WCFLoad.Test.InvokeTest(testToExecute, ss.AssemblyGuid, valueToUse); } else { Task.Factory.StartNew(() => { WCFLoad.Test.InvokeRestApi( WCFLoad.Test.TestPackage.RestMethods.Find(r => r.Guid == ss.MethodGuid), true); }); } requestSent++; Random r1 = new Random(); Thread.Sleep(r1.Next(delayRangeStart, delayRangeEnd)); currentScenarioOrder = currentScenarioOrder + 1; if (totalMethodsInScenario == currentScenarioOrder) { currentScenarioOrder = -1; currentScenario = -1; Thread.Sleep(WCFLoad.Test.TestPackage.IntervalBetweenScenarios); } } } }, CancellationTokens.GetPerformanceCancellationToken()); clientTasks[clientIndex].Start(); clientsSpawned++; } try { //Wait for all client threads to complete Task.WaitAll(clientTasks); SpinWait.SpinUntil(() => (requestSent == WCFLoad.Test.TotalResponsesRecieved), 60000); } catch (AggregateException ae) { Logger.LogMessage("Aggregaate Exception Occured : " + ae.Message); foreach (Exception ex in ae.InnerExceptions) { Logger.LogMessage(string.Format("Aggregate exception : {0} {1} {2}", ex.Message, Environment.NewLine, ex.StackTrace)); } } IsTestCompleted = true; WCFLoad.Test.CallRunResultUpdatedEvent(); FbServiceController.StopService(); }
public static void RunTest() { CancellationTokens.ResetPerformanceCancellationToken(); CancellationTokens.GetPerformanceCancellationToken(); Test.MethodLevelResults = new ConcurrentDictionary <string, MethodLogs>(); Test.MethodLevelReqStatus = new ConcurrentDictionary <string, MethodLogs>(); Test.ClearCache(); Test.PerformanceRunToken = CancellationTokens.GetPerformanceCancellationToken(); if (!string.IsNullOrEmpty(Test.TestPackage.ResultFileName)) { if (File.Exists(Test.TestPackage.ResultFileName)) { File.Delete(Test.TestPackage.ResultFileName); } } int scenariosCount = Test.TestPackage.Scenarios.Count; int duration = Test.TestPackage.Duration; int clients = Test.TestPackage.Clients; Stopwatch testDurationTimer = new Stopwatch(); testDurationTimer.Start(); int requestSent = 0; int delayRangeStart = Test.TestPackage.DelayRangeStart; int delayRangeEnd = Test.TestPackage.DelayRangeEnd; int clientsSpawned = 0; Task[] clientTasks = new Task[clients]; while (clientsSpawned < clients) { int clientIndex = clientsSpawned; clientTasks[clientIndex] = new Task(() => { int currentScenario = -1; int currentScenarioOrder = -1; while (testDurationTimer.ElapsedMilliseconds <= (duration * 60 * 1000) && !CancellationTokens.GetPerformanceCancellationToken().IsCancellationRequested) { if (scenariosCount == 0) { Random r = new Random(); bool executeWcf = r.Next(0, 1000) % 2 == 0; if (Test.TestPackage.Suites.Count == 0) { executeWcf = false; } if (executeWcf || Test.TestPackage.RestMethods.Count == 0) { int suiteNumberToExecute = r.Next(0, Test.TestPackage.Suites.Count - 1); int testNumberToExecute = r.Next(0, Test.TestPackage.Suites[suiteNumberToExecute].Tests.Count - 1); Common.Test testToExecute = Test.TestPackage.Suites[suiteNumberToExecute].Tests[testNumberToExecute]; Test.InvokeTest(testToExecute, Test.TestPackage.Suites[suiteNumberToExecute].Guid); requestSent++; } else { int suiteNumberToExecute = r.Next(0, Test.TestPackage.RestMethods.Count - 1); Task.Factory.StartNew(() => { Test.InvokeRestApi( Test.TestPackage.RestMethods[suiteNumberToExecute], true); }); requestSent++; } Thread.Sleep(r.Next(delayRangeStart, delayRangeEnd)); } else { if (currentScenario == -1) { Random r = new Random(); currentScenario = r.Next(0, scenariosCount - 1); } var scen = Test.TestPackage.Scenarios[currentScenario]; if (currentScenarioOrder == -1) { currentScenarioOrder = 0; } var order = currentScenarioOrder; var ss = (from s in scen.ScenarioOrder where s.Order == order select s).First(); int totalMethodsInScenario = scen.ScenarioOrder.Count; if (!ss.IsRest) { var methodName = ss.MethodName; var tts = from suite in Test.TestPackage.Suites from tt in suite.Tests where tt.Service.MethodName == methodName && ss.AssemblyGuid == suite.Guid select tt; Common.Test testToExecute = tts.ElementAt(0); var valueToUse = (from v in testToExecute.Service.Values.ValueList where v.Guid == ss.MethodGuid select v).First(); Test.InvokeTest(testToExecute, ss.AssemblyGuid, valueToUse); } else { Task.Factory.StartNew(() => { Test.InvokeRestApi( Test.TestPackage.RestMethods.Find(r => r.Guid == ss.MethodGuid), true); }); } requestSent++; Random r1 = new Random(); Thread.Sleep(r1.Next(delayRangeStart, delayRangeEnd)); currentScenarioOrder = currentScenarioOrder + 1; if (totalMethodsInScenario == currentScenarioOrder) { currentScenarioOrder = -1; currentScenario = -1; Thread.Sleep(Test.TestPackage.IntervalBetweenScenarios); } } } }, CancellationTokens.GetPerformanceCancellationToken()); clientTasks[clientIndex].Start(); clientsSpawned++; } try { Task.WaitAll(clientTasks); SpinWait.SpinUntil(() => (requestSent == Test.TotalResponsesRecieved), 60000); } catch (AggregateException ae) { Logger.LogMessage("Aggregaate Exception Occured : " + ae.Message); foreach (Exception ex in ae.InnerExceptions) { Logger.LogMessage(string.Format("Aggregate exception : {0} {1} {2}", ex.Message, Environment.NewLine, ex.StackTrace)); } } }