简体   繁体   English

将套接字服务器从Node.js移植到C#

[英]Porting socket server from Node.js to C#

I've built multiple socket server apps in Node.js for a multi-user artificial intelligence app. 我在Node.js中为多用户人工智能应用程序构建了多个套接字服务器应用程序。 We're looking at 1K to 10K active socket connections per box. 我们正在考虑每盒1K到10K有源插座连接。 However even when idle and with 0 active connections, some of my servers consume 50-100 MB of memory when running on Unix. 然而,即使在空闲和0活动连接时,我的一些服务器在Unix上运行时消耗50-100 MB的内存。 I'm sure with a sensible platform like C# or C++, this should be close to 0 MB. 我确信使用像C#或C ++这样合理的平台,这应该接近0 MB。 So we are considering a port to a "better" platform. 所以我们正在考虑将端口变为“更好”的平台。 Now let my clarify my use case: 现在让我澄清一下我的用例:

  • This is not a "web server". 这不是“网络服务器”。 No files are served. 没有提供文件。
  • We do lots of CPU intensive data processing and certain portions have already been ported to C++ and pulled into node via native modules. 我们进行了大量CPU密集型数据处理,某些部分已经移植到C ++并通过本机模块拉入节点。
  • We don't need to access much I/O (in most cases a few files are accessed, in some cases none, we don't use an RDBMS either) 我们不需要访问很多I / O(在大多数情况下访问一些文件,在某些情况下没有,我们也不使用RDBMS)

We went with node because it was Unix friendly (unlike .NET) and seemed easy to use. 我们选择节点,因为它是Unix友好的(不像.NET),似乎很容易使用。 But with its current memory consumption we need to evaluate other options. 但是由于目前的内存消耗,我们需要评估其他选项。 Many have compared Node.js with ASP.NET but I need to build a socket server in C# or C++. 许多人将Node.js与ASP.NET进行了比较但我需要用C#或C ++构建套接字服务器。

I have significant experience with .NET and C++. 我在.NET和C ++方面有丰富的经验。 There are libs like SuperSocket (used by Redgate and Telerik) that handle all of the low-level stuff in .NET. SuperSocket (由Redgate和Telerik使用)这样的库可以处理.NET中的所有低级内容。 I will have to find a similar socket framework for C++. 我将不得不为C ++找到一个类似的套接字框架。

So putting this all together, what are the advantages of using .NET or C++ over Node.js? 总而言之,使用.NET或C ++而不是Node.js有什么好处? And considering my servers are highly CPU-bound (not I/O bound) would the benefits of using .NET/C++ be significant or should I stick with Node.js? 考虑到我的服务器是高度CPU限制的(不受I / O约束)使用.NET / C ++的好处是显着的还是我应该坚持使用Node.js? Any other comments regarding porting a Node.js app to C# or C++? 关于将Node.js应用程序移植到C#或C ++的任何其他评论?

Bounty: I need advice and a recommended socket server library/implementation/example app in C# and/or C++. 赏金:我需要C#和/或C ++中的建议和推荐的套接字服务器库/实现/示例应用程序。 Must be open source. 必须是开源的。 I need it to be high-performance, async and bug-free. 我需要它是高性能,异步和无错误。 Must support binary data transfer. 必须支持二进制数据传输。 Must run on Windows. 必须在Windows上运行。 Unix is a bonus. Unix是一个奖励。

We're looking at 1K to 10K active socket connections per box 我们正在考虑每盒1K到10K有源插座连接

the bottleneck here is not the programing language or the technology, it's the hardware and OS support. 这里的瓶颈不是编程语言或技术,而是硬件和操作系统支持。 the thing that limits the amount of concurrent sockets count is basically the machine you're running on. 限制并发套接字数量的东西基本上就是你正在运行的机器。 yet, from my experience, the determinisitic object lifetime of C++ can help dramatically for supporting large number of concurrent OS resources. 然而,根据我的经验,C ++的确定对象生存期可以极大地帮助支持大量并发OS资源。

This is not a "web server". 这不是“网络服务器”。 No files are served. 没有提供文件。

I have done some Node.js in my profesional work, I have done some C# but mostly C++. 我在专业工作中做了一些Node.js,我做了一些C#,但主要是C ++。 even with node.js as a web server, most of the client and server code didn't had many much in common besides the language itself. 即使将node.js作为Web服务器,除了语言本身之外,大多数客户端和服务器代码没有太多共同之处。 the web server dealt with buisness logic mostly, while the client dealt with fetching and presenting the data interactivly. Web服务器主要处理商务逻辑,而客户端处理提取和呈现数据的交互。 So, I think the main advantage of node.js as a web server is that it gives purist-JS developers the ability to write server side without using languages/technology they are not familliar with. 因此,我认为node.js作为Web服务器的主要优点在于它为纯粹的JS开发人员提供了编写服务器端的能力,而无需使用他们不熟悉的语言/技术。

We do lots of CPU intensive data processing and certain portions have already been ported to C++ and pulled into node via native modules. 我们进行了大量CPU密集型数据处理,某些部分已经移植到C ++并通过本机模块拉入节点。

yep. 是的。 using strongly typed language can do wonders here. 使用强类型语言可以在这里创造奇迹。 no redunadand runtime-parsing. 没有redunadand运行时解析。

We don't need to access much I/O (in most cases a few files are accessed, in some cases none, we don't use an RDBMS either) 我们不需要访问很多I / O(在大多数情况下访问一些文件,在某些情况下没有,我们也不使用RDBMS)

Well, I feel there's a myth in the air that node.js somehow handles IO better than other technologies. 嗯,我觉得有一个神话,node.js以某种方式比其他技术更好地处理IO。 this is simply wrong. 这完全是错的。 the main feature of Node.js is the fact that by default, the IO is asynchronous. Node.js的主要特性是默认情况下IO是异步的。 but Node.js didn't invent any wheel. 但Node.js没有发明任何轮子。 you have asynchronous IO in Java (aka Java.NIO), C# (async/await) and C++ (as native stuff like epoll/IOCompletionPort, or some higher stuff like Boost.ASIO/ CPP-rest, Proxygen etc.) 你有Java(又名Java.NIO),C#(异步/等待)和C ++的异步IO(像epoll / IOCompletionPort这样的本地东西,或者像Boost.ASIO / CPP-rest,Proxygen等更高级的东西)

We went with node because it was Unix friendly (unlike .NET) 我们选择了节点因为它是Unix友好的(不像.NET)

.Net Core is a relativly new technology where .Net can run on Unix-based systems (like linux) .Net Core是一种相对较新的技术,其中.Net可以在基于Unix的系统上运行(如linux)

I will have to find a similar socket framework for C++. 我将不得不为C ++找到一个类似的套接字框架。

Boost.ASIO, or write something yourself, it's really not that hard.. Boost.ASIO,或自己写点东西,真的不是那么难......

So putting this all together, what are the advantages of using .NET or C++ over Node.js? 总而言之,使用.NET或C ++而不是Node.js有什么好处?

better CPU usage: because C++ and C# are strongly typed languages, and C++ is a statically compiled language, there are huge oppretunities for the compiler to optimize CPU extensive jobs. 更好的CPU使用率:因为C ++和C#是强类型语言,而C ++是一种静态编译语言,所以编译器有很大的优势可以优化CPU广泛的工作。

lower memory footprint: usually because strongly typed languages have smaller objects without the overhead of keeping a lot of meta-data behind the scences. 较低的内存占用:通常是因为强类型语言具有较小的对象而没有在场景后面保留大量元数据的开销。 with C++, having stack allocation and scoped object life-time usually the memory footprint is low. 使用C ++,具有堆栈分配和作用域对象的生命周期通常内存占用率很低。 again, it depends on the quality of the code in any language. 再次,它取决于任何语言的代码质量。

no callback hell: C# has tasks and async await. 没有回调地狱:C#有任务和异步等待。 C++ has futures/promises and some compilers (aka VC++) do supports await as well. C ++有期货/承诺,一些编译器(又名VC ++)确实支持等待。 the asynchronous code simply becomes pure fun to write as oppossed to callbacks. 异步代码变得非常有趣,因为它与回调相反。 yes, I do aware of JS promises and the new async/await stuff, but they are relativly new compared to .Net implementation. 是的,我确实知道JS承诺和新的async / await东西,但与.Net实现相比,它们相对较新。

Compiler checks : since C# and C++ have to be compiled, a lot of silly bugs are caught in compile time. 编译器检查:由于必须编译C#和C ++,因此在编译时会遇到很多愚蠢的错误。 no "undefiend is not a function" or "cannot read property of undefined". 没有“undefiend不是函数”或“不能读取未定义的属性”。

other than that it's pretty much a matter of choice. 除此之外,它几乎是一个选择问题。

NetMQ is native C# port of zeromq. NetMQ是zeromq的本地C#端口。

Zeromq is lightweight messaging library, the zeromq guide is a great if you want to learn about messaging, it also come as a book. Zeromq是轻量级的消息传递库,如果你想了解消息传递,zeromq指南是一个很棒的,它也可以作为一本书。 It applicable both to zeromq and NetMQ. 它适用于zeromq和NetMQ。

If you are using windows and need to handle a lot of connection I don't recommend zeromq as it not using IOCP. 如果您正在使用Windows并需要处理大量连接,我不建议使用zeromq,因为它不使用IOCP。

NetMQ is using IOCP on Windows and works both on windows and linux. NetMQ在Windows上使用IOCP,在Windows和Linux上都可以使用。

Disclosure - I'm author of NetMQ and maintainer on the zeromq (libzmq) project. 披露 - 我是zeromq(libzmq)项目的NetMQ和维护者的作者。

[1] https://github.com/zeromq/netmq [1] https://github.com/zeromq/netmq

[2] http://netmq.readthedocs.io/en/latest/ [2] http://netmq.readthedocs.io/en/latest/

[3] http://zguide.zeromq.org/page:all [3] http://zguide.zeromq.org/page:all

[4] http://www.amazon.com/ZeroMQ-Messaging-Applications-Pieter-Hintjens/dp/1449334067/ref=sr_1_1?ie=UTF8&qid=1462550951&sr=8-1&keywords=zeromq [4] http://www.amazon.com/ZeroMQ-Messaging-Applications-Pieter-Hintjens/dp/1449334067/ref=sr_1_1?ie=UTF8&qid=1462550951&sr=8-1&keywords=zeromq

We do lots of CPU intensive data processing 我们做了很多CPU密集型数据处理

Node.js may have been the wrong choice from the start and it would probably never match performances of a C++ server. Node.js从一开始就可能是错误的选择,它可能永远不会匹配C ++服务器的性能。 However, it can be pretty close, if you are doing things right. 但是,如果你做得对,它可能非常接近。 In addition, writing good C++ and a complete rewrite of a system is difficult and time consuming. 此外,编写好的C ++并完全重写系统既困难又耗时。 So, I want to give some reasons for you to stick to Node.js or at least, completely exhaust all your options before you move. 所以,我想给你一些理由让你坚持使用Node.js,或者至少在移动之前完全耗尽所有选择。

my servers consume 50-100 MB 我的服务器消耗50-100 MB

Are you using Node.js v0.12? 你在使用Node.js v0.12吗? With Node.js v4.2 LTS, idle Node.js server should use around 20 MB of memory. 使用Node.js v4.2 LTS,空闲Node.js服务器应该使用大约20 MB的内存。 (It would probably never be near 0 MB because of V8) Have you checked for memory leaks? (由于V8,它可能永远不会接近0 MB)你检查过内存泄漏吗?

1K to 10K active socket connections per box 每盒1K至10K有源插座连接

This should be easily achievable. 这应该很容易实现。 If you are using the most popular socket.io library, here's some relevant benchmarks. 如果您使用的是最流行的socket.io库,这里有一些相关的基准测试。

on a 3.3 GHz Xeon X5470 using one core, the max messages-sent-per-second rate is around 9,000–10,000 depending on the concurrency level. 在使用一个内核的3.3 GHz Xeon X5470上,根据并发级别,每秒​​最大消息发送速率约为9,000-10,000。

from: http://drewww.github.io/socket.io-benchmarking/ (Since, all these connections are kept alive concurrently, CPU usage matters more) 来自: http//drewww.github.io/socket.io-benchmarking/ (因为,所有这些连接同时保持活动,CPU使用更重要)

If you are already using that and having issues, try replacing socket.io with SocketCluster which is faster and more scalable. 如果您已经在使用它并遇到问题,请尝试使用更快,更具可扩展性的SocketCluster替换socket.io Replacing this should be easier than a complete rewrite. 替换它应该比完全重写更容易。 Here's some benchmarks: 这是一些基准:

8-core Amazon EC2 m3.2xlarge instance running Linux 运行Linux的8核Amazon EC2 m3.2xlarge实例

at 42K, the CPU use of the busiest worker dropped to around 45% 在42K,最繁忙的工作者的CPU使用率下降到45%左右

http://socketcluster.io/#!/performance http://socketcluster.io/#!/performance

Finally, to prove that Node.js can nearly reach C++ performance. 最后,证明Node.js几乎可以达到C ++性能。 Have a look at this: 看看这个:

servers use 12G memory 服务器使用12G内存

It supports 1,200,000 active websocket connections 它支持1,200,000个有效的websocket连接

https://github.com/smallnest/C1000K-Servers https://github.com/smallnest/C1000K-Servers

My point is you have average performance goals that you should be able to reach with Node.js with little effort. 我的观点是,您可以轻松地使用Node.js达到平均性能目标。 Try to benchmark ( https://github.com/machinezone/tcpkali ) and find the issue rather than do a complete rewrite. 尝试基准测试( https://github.com/machinezone/tcpkali )并找到问题而不是完全重写。

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

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