加速执行socket.Beginsend [英] Speed UP execution of socket.Beginsend

查看:458
本文介绍了加速执行socket.Beginsend的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我有多线程TCP IP服务器,必须每秒向其客户发送100条消息。

我正在测试它在500个客户端上,每条消息的大小约为2kb。



我使用SMART Thread Pool将我的服务器开发为多线程应用程序。



多线程工作正常。我正在使用socket .beginsend方法向客户发送消息。



测试时遇到延迟问题:

如果我们说100条消息每秒500个客户端这意味着每秒50000个消息,这需要18秒发送给客户端。



我测试问题是否与我的线程应用程序执行有关线程,但我发现,当我评论beginsend行时,我发现执行不需要超过2秒到达发送方法。



有没有办法加快beginsend的执行速度?

这个行为是否正确?

Hello There .

I have multithreaded TCP IP server which must send 100 messages per second to its clients.
I am testing it on 500 clients and the size of each message around 2kb.

I developed my server to be a multithreaded application using SMART Thread Pool.

The multithreading is working fine. I am using socket .beginsend method to send messages to clients.

While testing I face a delay problem:
If we said 100 message per second to 500 clients this means 50000 message per second this takes 18 seconds to be sent to clients.

I test to see if the problem is with my threading application execution of threads but I found that when I comment the beginsend line I find that the execution does not need more than 2 seconds reaching the sending method.

Is there is a way to speed up the execution of beginsend?
Is this behavior correct ?

推荐答案

这是非常简化的 - 我忽略了数据包开销,acks,响应转换,交换机背板容量和unicode。



2KB = 2096 * 8位。

每秒100条消息= 100 * 2096 * 8.

500个客户= 500 * 100 * 2096 * 8.

=每秒838,400,000位。



您使用的是千兆网络吗?因为如果你不存在瓶颈,就在那里。
This is very simplified - I ignore packet overhead, acks, response turnround, switch backplane capacity, and unicode.

2KB = 2096 * 8 bits.
100 messages per second = 100 * 2096 * 8.
500 clients = 500 * 100 * 2096 * 8.
= 838,400,000 bits per second.

Are you using a gigabit network? Because if you aren't there is your bottleneck, right there.


我相信即使你有足够的带宽来处理你想要发送的数据量,也有太多的开销和发送那么多数据包的延迟。当通过TCP发送时,您需要发送数据包,该数据包在发送到实际网络时会有一个小的延迟(无论实际前往另一台计算机所需的时间),然后您需要等待确认客户端在继续之前收到了数据包,或者如果没有收到确认,请再次发送数据包。



另外,如果使用2kb的数据包大小,您的数据包可能会被分段因为它们实际上并不适合一帧数据,这显然需要更长的时间才能发送。此外,如果您的最大数据包大小很小,那么这些消息实际上会被分成单独的TCP数据包,这将导致更多的开销。



关于MTU的页面 [ ^ ]



您可能遇到的问题更多地与网络的局限性有关,而不是代码的性能。你可能想尝试的是发送相同数量的小得多的消息并看看它是如何应对的。
I believe that even if you have enough bandwidth to cope with the amount of data you want to send, there's too much overhead and latency with sending that many packets. When sending over TCP you need to send the packet which will have a small delay as it gets sent onto the actual network (regardless of the time it takes to actually travel to the other computer) and then you need to wait for an acknowledgement that the client has received the packet before continuing or if no ack is received, send the packet again.

Also with using packet sizes of 2kb you're packets may be being fragmented as they don't actually fit within one frame of data, which will obviously take a little longer to send. Also if you have a small maximum packet size then the messages will actually be split into separate TCP packets which is going to incur quite a lot more overhead.

Page about MTU[^]

The problem your having may be more to do with the limitations of the network rather than the performance of the code. What you might like to try is sending the same amount of much smaller messages and see how it copes.


是的我正在使用千兆网络。我忘了提一下,当我评论发送语句时,当语句取消注释
Yes I am using Gigabit network. I forget to mention that when I comment the sending statement also the CPU usage reduced to half of usage when the statement uncommented


这篇关于加速执行socket.Beginsend的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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