コード例 #1
0
        /// <summary>
        /// Функция основной работы теста, передаваемая на поток
        /// </summary>
        public void Callback()
        {
            try
            {
                LoadRequest loader = new LoadRequest(1, LoadRequest.Controllers.acc);
                Random      rand   = new Random(DateTime.Now.Millisecond);
                double      number = rand.NextDouble();
                for (int i = 0; i < _entries; i++)
                {
                    if (number > 1)
                    {
                        number -= 1.0f;
                    }
                    else
                    {
                        number *= 1.073;
                    }
                    loader.AddData(DateTime.Now.AddSeconds(-i), number);
                    if (i % 100 == 0 && _verbose)
                    {
                        VerboseMessage(string.Format("datagen: RunId={2} TestId={1} Generated={0} Total={3}", i, TestId, RunId, _entries));
                    }
                }
                DateTime    startTime = DateTime.Now;
                HttpRequest http      = new HttpRequest(_host, _port, _chunked)
                {
                    Debug     = _debug,
                    KeepAlive = _keepAlive
                };

                http.Post("/api/v1/load", loader);
                http.SendHeader();
                while (true)
                {
                    bool flag = false;
                    if (_chunked)
                    {
                        int count = rand.Next(_minSendingData, _maxSendingData);
                        if (_verbose)
                        {
                            DebugMessage(string.Format("send: RunId={3} TestId={4} Sent={2} Current={0} Total={1}",
                                                       http.ChunkedPosition,
                                                       http.ContentLength,
                                                       count < http.ContentLength ? count : http.ContentLength,
                                                       RunId,
                                                       TestId));
                        }

                        flag = http.Send(count);
                    }
                    else
                    {
                        flag = http.Send();
                    }
                    if (flag)
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(rand.Next(_minSleepTime, _maxSleepTime));
                }

                LoadResponse loadResponse = http.Response <LoadResponse>();
                TimeSpan     endTime      = DateTime.Now - startTime;

                ResultMessage(string.Format("result: RunId={5} TestId={3} Success={0} StatusCode={1} Status={2} Time={4}ms",
                                            loadResponse.Success,
                                            (int)http.StatusCode,
                                            http.StatusCode,
                                            TestId,
                                            endTime.TotalMilliseconds,
                                            RunId));
            }
            catch (Exception e)
            {
                ExceptionMessage(e);
            }
        }