When i make the same test but without debug mode every thing works well. So is this a bug or am i doing something wrong i use C++ Builder Berlin 10.1 Update 2.
I tested this with 3 client components TNetHTTPClient - TRESTClient - TIdHTTP
and all 3 gave me the same results.
Here is my client code:
- Code: Select all
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainU.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMain *Main;
//---------------------------------------------------------------------------
__fastcall TMain::TMain(TComponent* Owner)
: TForm(Owner)
{
TestTime->Date = Now();
TestTime->Time = Now();
ResetCounter();
}
//---------------------------------------------------------------------------
void TMain::ResetCounter()
{
ExecutedCount = 0;
SuccessCount = 0;
ErrorsCount = 0;
ExceptionsCount = 0;
Executed->Caption = "Executed: 0";
Success->Caption = "Success: 0";
Errors->Caption = "Errors: 0";
Exceptions->Caption = "Exceptions: 0";
}
//---------------------------------------------------------------------------
void TMain::Execute(String aFramework, String ID)
{
if (aFramework == "Net")
Thread(FuncBind( &TMain::NetExecute, this,ID));
if (aFramework == "REST")
Thread(FuncBind( &TMain::RESTExecute, this,ID));
if (aFramework == "Indy")
Thread(FuncBind( &TMain::IndyExecute, this, ID));
}
//---------------------------------------------------------------------------
void TMain::NetExecute(String ID)
{
if (Headers->Strings->Text.Trim() == "=")
Headers->Strings->Clear();
unique_ptr<TNetHTTPClient> HTTPClient(new TNetHTTPClient(nullptr));
String Response;
try
{
HTTPClient->CustomHeaders["Connection"] = "close";
HTTPClient->HandleRedirects = false;
HTTPClient->ConnectionTimeout = TimeOut->Value;
HTTPClient->ResponseTimeout = TimeOut->Value;
if (Headers->Strings->Count > 0)
{
for (int i = 0; i < Headers->Strings->Count; i++)
HTTPClient->CustomHeaders[Headers->Strings->Names[i]] = Headers->Strings->ValueFromIndex[i];
}
AddCount(1);
TStopwatch Timer = TStopwatch::Create();
Timer.Reset();
Timer.Start();
_di_IHTTPResponse AResponse = HTTPClient->Execute(Method->Text, URL->Text);
Timer.Stop();
String Timing = Timer.ElapsedMilliseconds / 1000.0;
Response = AResponse->ContentAsString();
if (AResponse->StatusCode == 200)
AddCount(2);
else
{
AddCount(3);
ShowError(ID, String(AResponse->StatusCode) + ": " + AResponse->StatusText + " - " + Timing + "\n" + Response);
}
}
catch (const Exception &E)
{
AddCount(4);
ShowError(ID,"Exception " + E.Message + " - " + Response);
}
}
//---------------------------------------------------------------------------
void TMain::RESTExecute(String ID)
{
if (Headers->Strings->Text.Trim() == "=")
Headers->Strings->Clear();
unique_ptr<TRESTClient> RESTClient(new TRESTClient(nullptr));
unique_ptr<TRESTRequest> RESTRequest(new TRESTRequest(nullptr));
unique_ptr<TRESTResponse> RESTResponse(new TRESTResponse(nullptr));
try
{
RESTClient->HandleRedirects = false;
RESTClient->BaseURL = URL->Text;
RESTRequest->Client = RESTClient.get();
RESTRequest->Response = RESTResponse.get();
RESTRequest->Timeout = TimeOut->Value;
RESTRequest->AddParameter("Connection","close", pkHTTPHEADER);
if (Headers->Strings->Count > 0)
{
for (int i = 0; i < Headers->Strings->Count; i++)
{
RESTRequest->AddParameter(Headers->Strings->Names[i],Headers->Strings->ValueFromIndex[i], pkHTTPHEADER);
}
}
RESTRequest->Method = (TRESTRequestMethod)GetEnumValue(__delphirtti(TRESTRequestMethod), "rm" + Method->Text);
AddCount(1);
TStopwatch Timer = TStopwatch::Create();
Timer.Reset();
Timer.Start();
RESTRequest->Execute();
Timer.Stop();
String Timing = Timer.ElapsedMilliseconds / 1000.0;
if (RESTResponse->StatusCode == 200)
AddCount(2);
else
{
AddCount(3);
ShowError(ID, String(RESTResponse->StatusCode) + ": " + RESTResponse->StatusText + " - " + Timing + "\n" + RESTResponse->Content);
}
}
catch (const Exception &E)
{
AddCount(4);
ShowError(ID,"Exception " + E.Message + " - " + RESTResponse->Content);
}
}
//---------------------------------------------------------------------------
void TMain::IndyExecute(String ID)
{
if (Headers->Strings->Text.Trim() == "=")
Headers->Strings->Clear();
unique_ptr<TIdHTTP> IdHTTP(new TIdHTTP(nullptr));
String Response;
try
{
IdHTTP->ConnectTimeout = TimeOut->Value;
IdHTTP->ReadTimeout = TimeOut->Value;
IdHTTP->Request->Connection = "close";
if (Headers->Strings->Count > 0)
{
for (int i = 0; i < Headers->Strings->Count; i++)
{
IdHTTP->Request->RawHeaders->Values[Headers->Strings->Names[i]] = Headers->Strings->ValueFromIndex[i];
}
}
AddCount(1);
TStopwatch Timer = TStopwatch::Create();
Timer.Reset();
Timer.Start();
if (Method->Text == "GET")
Response = IdHTTP->Get(URL->Text);
if (Method->Text == "POST")
Response = IdHTTP->Post(URL->Text,"");
if (Method->Text == "PUT")
Response = IdHTTP->Put(URL->Text, nullptr);
if (Method->Text == "DELETE")
Response = IdHTTP->Delete(URL->Text);
Timer.Stop();
String Timing = Timer.ElapsedMilliseconds / 1000.0;
if (IdHTTP->ResponseCode == 200)
AddCount(2);
else
{
AddCount(3);
ShowError(ID, String(IdHTTP->ResponseCode) + ": " + IdHTTP->ResponseText + " - " + Timing + "\n" + Response);
}
}
catch (const Exception &E)
{
AddCount(4);
ShowError(ID,"Exception " + E.Message + " - " + Response);
}
}
//---------------------------------------------------------------------------
void TMain::DoAddCount(int Index)
{
switch (Index)
{
case 1 : ExecutedCount++; break;
case 2 : SuccessCount++; break;
case 3 : ErrorsCount++; break;
case 4 : ExceptionsCount++; break;
default: break;
}
Executed->Caption = "Executed: " + String(ExecutedCount);
Success->Caption = "Success: " + String(SuccessCount);
Errors->Caption = "Errors: " + String(ErrorsCount);
Exceptions->Caption = "Exceptions: " + String(ExceptionsCount);
}
//---------------------------------------------------------------------------
void TMain::AddCount(int Index)
{
NotifyUI(FuncBind( &TMain::DoAddCount, this, Index));
}
//---------------------------------------------------------------------------
void TMain::DoShowError(String ID, String Text)
{
Results->Lines->Add("----------------------");
Results->Lines->Add(ID + "- " + Text);
Results->Lines->Add("----------------------");
}
//---------------------------------------------------------------------------
void TMain::ShowError(String ID, String Text)
{
NotifyUI(FuncBind( &TMain::DoShowError, this, ID, Text));
}
//---------------------------------------------------------------------------
void __fastcall TMain::SendClick(TObject *Sender)
{
ResetCounter();
Results->Clear();
Execute(Framework->Text, 1);
}
//---------------------------------------------------------------------------
void TMain::DoRunTest()
{
for (int i = 0; i < ThreadsCount->Value; i++)
{
Execute(Framework->Text,i+1);
if (Interval->Value > 0)
Sleep(Interval->Value);
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void TMain::RunTest()
{
StartTest->Enabled = false;
Results->Clear();
ResetCounter();
ThreadWait(FuncBind( &TMain::DoRunTest, this));
StartTest->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TMain::StartTestClick(TObject *Sender)
{
RunTest();
}
//---------------------------------------------------------------------------
void __fastcall TMain::TestTimerTimer(TObject *Sender)
{
Results->Text = Time();
if (Now() >= TestTime->Time)
{
TestTimer->Enabled = false;
RunTest();
}
}
//---------------------------------------------------------------------------
void __fastcall TMain::StartTimedClick(TObject *Sender)
{
TestTimer->Enabled = true;
}
//---------------------------------------------------------------------------
Appreciate any help. Thanks in advance
Regards,
Ahmed Sayed