public Task <TestResults> StartTest(HttpTestConfig config, IList <SaveSpansArgs> saveSpansArgs, CancellationToken?ct = null)
        {
            var testResults = new TestResults();

            if (config == null)
            {
                return(Task.FromResult(testResults));
            }

            testResults.FailExpiredMs    = config.FailExpiredMs;
            testResults.Uri              = config.LastTestUri;
            testResults.Data             = config.LastTestData;
            testResults.MaxParallelCount = config.MaxParallelCount;
            testResults.HttpMethod       = config.HttpMethod;

            var theToken             = ct ?? CancellationToken.None;
            var results              = new ConcurrentBag <TestResult>();
            var taskCompletionSource = new TaskCompletionSource <TestResults>();

            var parallelOptions = new ParallelOptions()
            {
                CancellationToken = theToken, MaxDegreeOfParallelism = config.MaxParallelCount
            };

            Parallel.ForEach(saveSpansArgs, parallelOptions, (span) =>
            {
                var testResult = AsyncHelper.RunSync(() =>
                                                     RunTestClientSpan(WebApiHelper, testResults.FailExpiredMs, span, config));
                results.Add(testResult);
            });

            testResults.Items = results.ToList();
            taskCompletionSource.SetResult(testResults);
            return(taskCompletionSource.Task);
        }
예제 #2
0
        public static string GetRequestUri(HttpTestConfig config, string method)
        {
            if (_fixedTraceApiEndPoint == null)
            {
                _fixedTraceApiEndPoint = config.TraceApiEndPoint;
                _fixedTraceApiEndPoint = _fixedTraceApiEndPoint.TrimEnd('/') + "/";
            }

            return(_fixedTraceApiEndPoint + method);
        }
        public IList <SaveSpansArgs> CreateTestClientSpans(HttpTestConfig config)
        {
            var result   = new List <SaveSpansArgs>();
            var now      = DateHelper.Instance.GetDateNow();
            var ticks    = now.Ticks;
            var traceId  = "trace_" + ticks;
            var tracerId = "ConcurrentTest-Tracer-" + now.ToString("yyyyMMddHHmmss");

            for (int i = 0; i < config.ConcurrentCount; i++)
            {
                var spanId         = "span_" + i.ToString("");
                var saveClientSpan = SaveClientSpan.Create(now, now.AddMilliseconds(10), tracerId, traceId, null, spanId, "FooOp");
                var saveSpansArgs  = new SaveSpansArgs();
                saveSpansArgs.Items.Add(saveClientSpan);
                result.Add(saveSpansArgs);
            }
            return(result);
        }
예제 #4
0
        public virtual Task <TestResults> StartTest(HttpTestConfig config, CancellationToken?ct = null)
        {
            var testResults = new TestResults();

            if (config == null)
            {
                return(Task.FromResult(testResults));
            }

            testResults.FailExpiredMs    = config.FailExpiredMs;
            testResults.Uri              = config.LastTestUri;
            testResults.Data             = config.LastTestData;
            testResults.MaxParallelCount = config.MaxParallelCount;
            testResults.HttpMethod       = config.HttpMethod;

            #region not real parallel test

            ////this will not block current thread
            //for (int i = 0; i < config.ConcurrentCount; i++)
            //{
            //    var index = i;
            //    var testResult = AsyncHelper.RunSync(() => RunHttpTest(index, testResults.Uri, testResults.FailExpiredMs));
            //    results.Add(testResult);
            //}
            //_simpleLog.Log("Concurrent Test Completed.");

            #endregion

            #region parallel demos

            //Threads on a non-threaded machine:
            //=> Parallel
            //=>     --  --  --
            //=> /              \
            //=> >---- --  --  --  -- ---->>

            //Threads on a threaded machine:
            //=>     ------
            //=>    /      \
            //=> >-------------->>

            #endregion

            var theToken             = ct ?? CancellationToken.None;
            var results              = new ConcurrentBag <TestResult>();
            var taskCompletionSource = new TaskCompletionSource <TestResults>();

            var parallelOptions = new ParallelOptions()
            {
                CancellationToken = theToken, MaxDegreeOfParallelism = config.MaxParallelCount
            };
            Parallel.For(0, config.ConcurrentCount, parallelOptions, i =>
            {
                var index      = i;
                var testResult = AsyncHelper.RunSync(() => RunHttpTest(index, testResults.Uri, testResults.FailExpiredMs, testResults.HttpMethod, testResults.Data));
                results.Add(testResult);
            });

            testResults.Items = results.ToList();
            taskCompletionSource.SetResult(testResults);
            return(taskCompletionSource.Task);
        }
예제 #5
0
 public void SaveConfig(HttpTestConfig config)
 {
     AsyncHelper.RunSync(() => SimpleConfigFile.SaveFile(config));
 }
        private async Task <TestResult> RunTestClientSpan(IWebApiTester webApiHelper, int failExpiredMs, SaveSpansArgs saveSpansArgs, HttpTestConfig config)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var jsonData = saveSpansArgs.ToJson(false);
            var saveOk   = await webApiHelper.TestHttpPost(config.GetSaveSpansApiUri(), jsonData, failExpiredMs).ConfigureAwait(false);

            stopwatch.Stop();
            var testResult = new TestResult();

            testResult.Success   = saveOk;
            testResult.ElapsedMs = stopwatch.ElapsedMilliseconds;
            var itemsCount     = saveSpansArgs.Items.Count;
            var saveClientSpan = saveSpansArgs.Items.First();

            testResult.Message = string.Format("{0}(1/{1}) => {2} , take {3:0.00} ms",
                                               saveClientSpan.SpanId,
                                               itemsCount,
                                               saveOk ? "Success" : "Fail",
                                               stopwatch.ElapsedMilliseconds);
            SimpleLog.Log(testResult.Message);
            return(testResult);
        }
예제 #7
0
 public static string GetSaveSpansApiUri(this HttpTestConfig config)
 {
     return(GetRequestUri(config, "SaveSpans"));
 }