TIdHTTP获取MSecs中的往返请求/响应时间

时间:2016-03-06 09:13:56

标签: c++builder indy indy10 idhttp

我正在使用TIdHTTP发出HTTP GET请求来获取维基页面(使用for循环语句执行x> = 9次)。

我正在尝试使用TStopWatch来测量每次GET请求的HTTP往返请求/响应时间(以毫秒为单位)。

我尝试了一些方法,但是,我仍然怀疑它是对还是不对。那么,请告诉我这是什么方法。谢谢。

不过,我的代码是:

//---------------------------------------------------------------------------
  int MSec = 0;
  TTimeSpan TTSpan;
  TDateTime StartTime;
  DWORD AStartTime = 0;
  DWORD CStartTime = 0;
  String ACount, Codes, Milli, Result;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
  IdHTTP->ConnectTimeout = 3048;
  IdHTTP->ReadTimeout = 2048;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdHTTPWork(TObject *ASender, TWorkMode AWorkMode, __int64 AWorkCount)
{
  if (AWorkMode == wmRead)
    ACount = "size: [" + String(AWorkCount) + " bytes]";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WorkClick(TObject *Sender)
{
  static int Number = 1;
  TStopwatch SWatch = TStopwatch::StartNew();

  // CStartTime = GetTickCount();
  // DWORD Start = timeGetTime();
  // AStartTime = GetTickCount();
  // CStartTime = TTSpan.Milliseconds;

  Work->Enabled = false;
  ListBox->Items->Clear();
  ListBox->Items->BeginUpdate();

  for (int i = 0; i < 9; i++)
  {
    try
    {
      SWatch.Start();
      try
      {
        IdHTTP->Get("http://en.wikipedia.org/");
        Sleep(1000);
      }
      catch (const Exception &E)
      {
        Label1->Caption = E.Message;
      }

      SWatch.Stop();
      TTSpan = SWatch.Elapsed;
      MSec = TTSpan.Milliseconds;
      // MSec = TTSpan.Milliseconds / 10;
      // MSec = SWatch.ElapsedMilliseconds / 10;
      // TTSpan = SWatch.Elapsed;
      // MSec = TTSpan.Milliseconds - CStartTime;

      Milli = " time: [" + String(MSec) + " ms]";
      Codes = " code: [" + IdHTTP->Response->ResponseText.LowerCase() + "]";
      ListBox->Items->Add("(" + String(Number++) + "). " + ACount + Codes + Milli);

      // AStartTime = GetTickCount();
      // ListBox->Items->Add("(" + String(Number++) + "). " + ACount + Codes + Result.sprintf(L" time: [%i ms]",(aStartTime-bStartTime)));
    }
    __finally
    {
      Work->Enabled = true;
      IdHTTP->Disconnect(true);
      ListBox->Items->EndUpdate();
    }
  }
}

0 个答案:

没有答案