Delphi:为什么 IdHTTP.ConnectTimeout 使请求变慢?
我发现当为 TIdHTTP 组件设置 ConnectTimeoout 属性时,它会使请求(GET 和 POST)变慢约 120 毫秒?
为什么会这样,我可以以某种方式避免/绕过它吗?
Env:D2010 附带 Indy 组件,所有更新均针对 D2010 安装。操作系统是 WinXP(32 位)SP3,带有大多数补丁...
我的计时例程是:
Procedure DoGet;
Var
Freq,T1,T2 : Int64;
Cli : TIdHTTP;
S : String;
begin
QueryPerformanceFrequency(Freq);
Try
QueryPerformanceCounter(T1);
Cli := TIdHTTP.Create( NIL );
Cli.ConnectTimeout := 1000; // without this we get < 15ms!!
S := Cli.Get('http://127.0.0.1/empty_page.php');
Finally
FreeAndNil(Cli);
QueryPerformanceCounter(T2);
End;
Memo1.Lines.Add('Time = '+FormatFloat('0.000',(T2-T1)/Freq) );
End;
通过在代码中设置 ConnectTimeout,我得到平均。 130-140ms的时间,没有的话大约5-15ms......
I discovered that when setting the ConnectTimeoout property for a TIdHTTP component, it makes the requests (GET and POST) become about 120ms slower?
Why is this, and can I avoid/bypass this somehow?
Env: D2010 with shipped Indy components, all updates installed for D2010. OS is WinXP (32bit) SP3 with most patches...
My timing routine is:
Procedure DoGet;
Var
Freq,T1,T2 : Int64;
Cli : TIdHTTP;
S : String;
begin
QueryPerformanceFrequency(Freq);
Try
QueryPerformanceCounter(T1);
Cli := TIdHTTP.Create( NIL );
Cli.ConnectTimeout := 1000; // without this we get < 15ms!!
S := Cli.Get('http://127.0.0.1/empty_page.php');
Finally
FreeAndNil(Cli);
QueryPerformanceCounter(T2);
End;
Memo1.Lines.Add('Time = '+FormatFloat('0.000',(T2-T1)/Freq) );
End;
With the ConnectTimeout set in code I get avg. times of 130-140ms, without it's about 5-15ms ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
ConnectTimeout
为零(且TIdAntifreeze
未生效)时,Indy 只会进行连接。否则,TIdIOHandlerStack.ConnectClient
调用DoConnectTimeout
,它创建一个新线程来在调用线程休眠时进行连接并处理TIdAntifreeze
操作,等待建立连接。如果超时后仍未建立连接,则会引发异常。线程不是空闲的,调用线程在检查连接线程是否完成其任务之前将始终处于睡眠状态。默认睡眠持续时间为125 毫秒。 (要使用其他功能,请激活
TIdAntifreeze
并将其IdleTimeout
属性设置为低于 125。)When
ConnectTimeout
is zero (andTIdAntifreeze
is not in effect), Indy simply connects. Otherwise,TIdIOHandlerStack.ConnectClient
callsDoConnectTimeout
, which creates a new thread to do the connecting while the calling thread sleeps and processesTIdAntifreeze
operations, waiting for the connection to be established. If there's not connection by the time the timeout elapses, it throws an exception.Threads aren't free, and the calling thread will always sleep before checking whether the connection thread has accomplished its task. The default sleep duration is 125 ms. (To use something else, activate
TIdAntifreeze
and set itsIdleTimeout
property lower than 125.)