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

查看:187
本文介绍了同步比。在.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).

我觉得这是有道理的,如果我们谈论的许多客户端连接的地方它不可能每个连接分配一个线程的服务器。然后,我可以看到使用的线程池线程,并让他们异步回调的优势。

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个字节数组不能在线程池线程处理,因为谁也不能保证他们重新present 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.

这是为什么会发生?如果你有一个插座,您开始上的操作来接收数据,你会得到只有一个回调时,它的完成。然后决定是否做其他操作。这听起来像你过分复杂的,虽然也许我对于你想要做的事情过于简单化了。

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天全站免登陆