同步对比.NET 中的异步套接字性能 [英] Sync Vs. Async Sockets Performance in .NET

查看:14
本文介绍了同步对比.NET 中的异步套接字性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET 中读到的关于套接字的所有内容都说异步模式提供了更好的性能(特别是使用新的 SocketAsyncEventArgs 可以节省分配).

Everything that I read about sockets in .NET says that the asynchronous pattern gives better performance (especially with the new SocketAsyncEventArgs which saves on the allocation).

我认为,如果我们谈论的服务器有许多客户端连接,而无法为每个连接分配一个线程,我认为这是有道理的.然后我可以看到使用 ThreadPool 线程并在它们上获得异步回调的优势.

I think this makes sense if we're talking about a server with many client connections where its not possible to allocate one thread per connection. Then I can see the advantage of using the ThreadPool threads and getting async callbacks on them.

但在我的应用程序中,我是客户端,我只需要监听一台服务器通过一个 tcp 连接发送市场报价数据.现在,我创建了一个线程,将优先级设置为最高,并用它调用 Socket.Receive().我的线程在此调用上阻塞并在新数据到达时唤醒.

But in my app, I'm the client and I just need to listen to one server sending market tick data over one tcp connection. Right now, I create a single thread, set the priority to Highest, and call Socket.Receive() with it. My thread blocks on this call and wakes up once new data arrives.

如果我将其切换为异步模式,以便在有新数据时收到回调,我会看到两个问题

If I were to switch this to an async pattern so that I get a callback when there's new data, I see two issues

  1. 线程池线程将具有默认优先级,因此它们似乎比我自己的具有最高优先级的线程更糟糕.

  1. The threadpool threads will have default priority so it seems they will be strictly worse than my own thread which has Highest priority.

我仍然需要在某个时候通过单个线程发送所有内容.假设我在 N 个不同的线程池线程上几乎同时收到 N 个回调,通知我有新数据.它们传递的 N 字节数组无法在线程池线程上处理,因为无法保证它们代表 N 个唯一的市场数据消息,因为 TCP 是基于流的.无论如何,我必须锁定并将字节放入数组中,并向其他一些可以处理数组中内容的线程发出信号.所以我不确定拥有 N 个线程池线程会给我带来什么.

I'll still have to send everything through a single thread at some point. Say that I get N callbacks at almost the same time on N different threadpool threads notifying me that there's new data. The N byte arrays that they deliver can't be processed on the threadpool threads because there's no guarantee that they represent N unique market data messages because TCP is stream based. I'll have to lock and put the bytes into an array anyway and signal some other thread that can process what's in the array. So I'm not sure what having N threadpool threads is buying me.

我想错了吗?在我的一个客户端连接到一个服务器的特定情况下,是否有理由使用异步模式?

Am I thinking about this wrong? Is there a reason to use the Async patter in my specific case of one client connected to one server?

更新:

所以我认为我误解了上面(2)中的异步模式.当有数据可用时,我会在一个工作线程上获得回调.然后我会开始另一个异步接收并获得另一个回调,等等.我不会同时收到 N 个回调.

So I think that I was mis-understanding the async pattern in (2) above. I would get a callback on one worker thread when there was data available. Then I would begin another async receive and get another callback, etc. I wouldn't get N callbacks at the same time.

问题还是一样.在我是客户端且仅连接到一台服务器的特定情况下,回调是否有任何理由会更好.

The question still is the same though. Is there any reason that the callbacks would be better in my specific situation where I'm the client and only connected to one server.

推荐答案

应用程序中最慢的部分将是网络通信.通过调整这样的事情,您很可能几乎不会对单线程、一个连接客户端的性能产生任何影响.网络通信本身将使所有其他对处理或上下文切换时间的贡献相形见绌.

The slowest part of your application will be the network communication. It's highly likely that you will make almost no difference to performance for a one thread, one connection client by tweaking things like this. The network communication itself will dwarf all other contributions to processing or context switching time.

说我几乎收到了 N 个回调同时在 N 不同线程池线程通知我有新数据.

Say that I get N callbacks at almost the same time on N different threadpool threads notifying me that there's new data.

为什么会这样?如果您有一个套接字,您可以Begin 对它进行一个操作以接收数据,并且在完成时您会得到一个回调.然后您决定是否进行其他操作.听起来你把它复杂化了,但也许我对你想要做的事情过于简单化了.

Why is that going to happen? If you have one socket, you Begin an operation on it to receive data, and you get exactly one callback when it's done. You then decide whether to do another operation. It sounds like you're overcomplicating it, though maybe I'm oversimplifying it with regard to what you're trying to do.

总而言之,我想说:选择最简单的编程模型,让你得到你想要的;考虑到您的方案中可用的选择,无论您选择哪种选择,它们都不可能对性能产生任何显着差异.使用阻塞模型,您正在浪费"一个可以做一些实际工作的线程,但是嘿...也许您没有任何真正的工作要做.

In summary, I'd say: pick the simplest programming model that gets you what you want; considering choices available in your scenario, they would be unlikely to make any noticeable difference to performance whichever one you go with. With the blocking model, you're "wasting" a thread that could be doing some real work, but hey... maybe you don't have any real work for it to do.

这篇关于同步对比.NET 中的异步套接字性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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