Esempio n. 1
0
 public Task RunInConsole(bool saveToFile = true)
 {
     _cts = ScenarioBuilder.ScenarioToRun.DurationLimit.HasValue ? new CancellationTokenSource(ScenarioBuilder.ScenarioToRun.DurationLimit.Value) : new CancellationTokenSource();
     return(Task <ScenarioRunResult> .Factory.StartNew(() =>
     {
         try
         {
             var options = new ProgressBarOptions
             {
                 ForegroundColor = ConsoleColor.Yellow,
                 BackgroundColor = ConsoleColor.DarkYellow,
                 ProgressCharacter = '─'
             };
             using (var pbar = new ProgressBar(1, "Load test progress", options))
             {
                 var result = ScenarioBuilder.BuildAndRun(_cts.Token, pbar);
                 return result;
             }
         }
         catch (Exception e)
         {
             return ScenarioRunResult.Fail(e);
         }
     }).ContinueWith((task) =>
     {
         var result = task.Result;
         var stats = CalculateStatistics(result);
         PrintToConsole(stats);
         if (saveToFile)
         {
             PrintToFile(stats);
         }
     }));
 }
Esempio n. 2
0
        internal static ScenarioRunResult Run(int id, Scenario scenario, CancellationToken token, IProgress <int> progress, int iteration)
        {
            var context = new StepContext();

            context.Id = id;
            var result = new ScenarioRunResult();
            var i      = 0;

            foreach (var step in scenario.Steps)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }

                context.Connection = scenario.ConnectionPool?.OpenConnection();

                var watch    = Stopwatch.StartNew();
                var response = step.Executor(context);
                watch.Stop();

                if (response.Exception != null || !string.IsNullOrEmpty(response.FailReason))
                {
                    result.Response = response;
                    var msg = response.Exception != null ? response.Exception.Message : response.FailReason;
                    progress?.Report(iteration * scenario.Steps.Count);
                    break;
                }
                else
                {
                    context.Data = response.Data;
                    if (result.Stats.ContainsKey(step.Name))
                    {
                        result.Stats[step.Name].Add(watch.ElapsedMilliseconds);
                    }
                    else
                    {
                        result.Stats.Add(step.Name, new List <long> {
                            watch.ElapsedMilliseconds
                        });
                    }
                    i++;
                    progress?.Report(iteration * i);
                }

                if (context.Connection != null)
                {
                    scenario.ConnectionPool.CloseConnection(context.Connection);
                }
            }

            return(result);
        }
Esempio n. 3
0
        public ScenarioRunResult BuildAndRun(CancellationToken ct, ProgressBar pbar)
        {
            var tasks = new List <Task <ScenarioRunResult> >();

            Func <object, ScenarioRunResult> action = (object index) =>
            {
                var resultList   = new List <ScenarioRunResult>();
                var childOptions = new ProgressBarOptions
                {
                    ForegroundColor   = ConsoleColor.Green,
                    BackgroundColor   = ConsoleColor.DarkGreen,
                    ProgressCharacter = '─'
                };
                var pbarChild = pbar?.Spawn(ScenarioToRun.Repeat * ScenarioToRun.Steps.Count, $"concurent run ({index})", childOptions);
                var progress  = pbarChild?.AsProgress <int>();
                for (int ind = (int)index, i = 0; i < ScenarioToRun.Repeat; i++, ind++)
                {
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }

                    resultList.Add(Scenario.Run(ind, ScenarioToRun, ct, progress, i));
                }
                if (pbarChild != null)
                {
                    pbarChild.Dispose();
                }
                var result = ScenarioRunResult.Merge(resultList);
                resultBug.Add(result);
                return(result);
            };

            for (int i = 0; i < (ScenarioToRun.ConcurrentCopies * ScenarioToRun.Repeat); i += ScenarioToRun.Repeat)
            {
                int index = i;
                tasks.Add(Task <ScenarioRunResult> .Factory.StartNew(action, index, ct));
            }

            try
            {
                Task.WaitAll(tasks.ToArray());
                pbar?.AsProgress <float>().Report(1);
                return(ScenarioRunResult.Merge(resultBug.ToArray()));
            }
            catch (AggregateException e)
            {
                return(ScenarioRunResult.Fail(e));
            }
        }
Esempio n. 4
0
        private List <Statistics> CalculateStatistics(ScenarioRunResult scenarioResult)
        {
            var listResult = new List <Statistics>();

            foreach (var result in scenarioResult.Stats)
            {
                listResult.Add(new Statistics
                {
                    Name             = result.Key,
                    ExpectCalls      = ScenarioBuilder.ScenarioToRun.ConcurrentCopies * ScenarioBuilder.ScenarioToRun.Repeat,
                    SuccessfullCalss = result.Value.Count,
                    Mean             = result.Value.Mean(),
                    Max   = result.Value.Max(),
                    Min   = result.Value.Min(),
                    Stdev = result.Value.StandardDeviation()
                });
            }

            return(listResult);
        }
Esempio n. 5
0
        public static ScenarioRunResult Merge(IEnumerable <ScenarioRunResult> list)
        {
            var result = new ScenarioRunResult();

            foreach (var item in list)
            {
                foreach (var stats in item.Stats)
                {
                    if (result.Stats.ContainsKey(stats.Key))
                    {
                        result.Stats[stats.Key].AddRange(stats.Value);
                    }
                    else
                    {
                        result.Stats.Add(stats.Key, stats.Value);
                    }
                }
            }

            return(result);
        }