客户端一次发送一堆邮件时,HttpWebRequest花费很长时间发送 [英] HttpWebRequest takes a long time to send when there are a bunch at once from client

查看:48
本文介绍了客户端一次发送一堆邮件时,HttpWebRequest花费很长时间发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用创建许多HttpWebRequests(1000+)的C#负载测试客户端对Comet-ish服务器进行负载测试.我发现几分钟后,服务器随机花了很长时间来接收一些请求.客户端认为它已成功发送了请求,但实际上需要40秒钟才能到达服务器,这时为时已晚. (由于这是Comet型服务器,因此该服务器最终会丢弃客户端会话,这很糟糕).我尝试从异步调用切换到同步调用,但这没什么不同.

I am attempting to load-test a Comet-ish server using a C# load testing client that creates many HttpWebRequests (1000+). I am finding that after a few minutes, randomly, the server takes a long time to receive some of the requests. The client thinks it sent the request successfully, but it actually takes 40s to arrive at the server, at which point it is too late. (Since this is a Comet-type server, the server ends up dropping the client session, which is bad). I tried switching from asynchronous calls to synchronous calls but it didn't make a difference.

问题必须在客户端.我对Wireshark进行了一些跟踪,结果发现,从客户端软件到网络管道的请求实际上需要40秒钟左右的时间!服务器在管道上接收到请求后立即为其提供服务.

The problem must be at the client end. I did some tracing with Wireshark and it turns out that the request actually does take 40 or so seconds to make it to the network pipe from the client software! The server services the request right away when it receives it on its pipe.

也许C#搞砸了,因为该请求看上去与我之前发出的请求完全相同,并且出于某种奇怪的原因将其缓存了?我在响应中包括"Cache-Control:no-cache",以避免完全缓存.

Maybe C# is messing up because the request looks exactly the same as a request I made earlier and caching it for some weird reason? I am including "Cache-Control:no-cache" in my responses to avoid caching altogether.

推荐答案

当我第一次构建网络爬虫时,我遇到了类似的问题,该爬虫每分钟会发出2,000个以上的请求.原来,问题出在我并不总是及时处理HttpWebResponse对象.当您以这种速度发出请求时,垃圾收集/完成机制将无法跟上.

I ran into a similar issue when I was first building my web crawler, which makes upwards of 2,000 requests every minute. The problem turned out to be that I wasn't always disposing of the HttpWebResponse objects in a timely fashion. The garbage collection / finalization mechanism will not keep up when you're making requests at that rate.

您是在执行同步请求还是异步请求都没有关系.只要确保您始终拨打response.Close().

Whether you're doing synchronous or asynchronous requests doesn't really matter. Just make sure you always call response.Close().

这篇关于客户端一次发送一堆邮件时,HttpWebRequest花费很长时间发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆