简体   繁体   English

HttpWebRequest和HttpWebResponse是理想的异步缓冲区大小

[英]HttpWebRequest and HttpWebResponse ideal async buffer sizes

I'm trying to use HttpWebRequest and HttpWebResponse in .NET 3.5, running them in asynchronously: BeginGetRequestStream, EndGetRequestStream, BeginWrite, EndWrite, BeginGetResponse, EndGetResponse, BeginRead, EndRead - all parts of handling a request are asynchronous. 我正在尝试在.NET 3.5中使用HttpWebRequest和HttpWebResponse,以异步方式运行它们:BeginGetRequestStream,EndGetRequestStream,BeginWrite,EndWrite,BeginGetResponse,EndGetResponse,BeginRead,EndRead - 处理请求的所有部分都是异步的。

I have a few threads that send a large number of concurrent requests. 我有几个线程发送大量并发请求。 EndRead and EndWrite are both blocking operations - they block the current thread while the actual read/write against the stream is done, I'm trying to come up with an ideal input/output buffer size for these operations. EndRead和EndWrite都是阻塞操作 - 它们在对流完成实际读/写操作时阻塞当前线程,我正在尝试为这些操作提供理想的输入/输出缓冲区大小。

My reasoning is this: as I have multiple requests active at a time, they'll keep firing callbacks to let the thread know there's some data available or data was sent. 我的理由是这样的:因为我一次有多个活动请求,他们将继续触发回调,让线程知道有一些数据可用或数据已发送。 If my buffers are large, reading/writing the data through the wire will take longer, so EndRead/EndWrite will block longer. 如果我的缓冲区很大,通过线路读取/写入数据将花费更长时间,因此EndRead / EndWrite将阻塞更长时间。 This would force the other requests on the same thread to wait a bit longer, since their notifications will have to wait until the thread is unblocked. 这会强制同一线程上的其他请求等待一段时间,因为它们的通知必须等到线程被解除阻塞。

So, my question is, what would be a good read / write buffer size in this situation. 所以,我的问题是,在这种情况下,什么是良好的读/写缓冲区大小。 I was thinking 2048 bytes each, but some sample code I saw in various blogs show wildly different values. 我在考虑每个2048字节,但我在各种博客中看到的一些示例代码显示了截然不同的值。

Thanks in advance for any ideas. 提前感谢任何想法。

There's no definitive rule as to the actual values you should set, beyond avoiding the obvious extremes. 除了避免明显的极端情况之外,对于您应该设置的实际值没有确定的规则。 It really depends on the type of data you're transfering, and how much of it there is. 这实际上取决于您要传输的数据类型,以及有多少数据。 You probably want to set your write buffer quite high, but leave your read buffer lower. 您可能希望将写入缓冲区设置得相当高,但要将读取缓冲区保持在较低水平。 This is because writes are (usually) more expensive than reads when it comes to this kind of thing. 这是因为当涉及到这种事情时,写入(通常)比读取更昂贵。

The best thing to do in this situation is try a few values and see how well they scale. 在这种情况下,最好的办法是尝试一些值,看看它们的扩展程度。 You can always change them later if necessary. 如有必要,您可以随时更改它们。

I think a better solution would be not to worry about the buffer sizes too much, but don't block the threads. 我认为更好的解决方案是不要过多担心缓冲区大小,但不要阻塞线程。 If you pass a delegate to the callback parameter of the Begin* methods, that callback is executed when the operation completes and you can call End* from there, which will (almost) immediately return. 如果将委托传递给Begin*方法的callback参数,则在操作完成时执行该回调,并且可以从那里调用End* ,这将(几乎)立即返回。 No blocking necessary. 无需阻止。

And regarding the buffer sizes, if they really matter to you, you should profile and find out what works best in your specific situation. 关于缓冲区大小,如果它们对您真正重要,您应该剖析并找出在您的特定情况下最有效的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM