Experiences in realizing high-performance and stable socket TCP Communication

Source: Internet
Author: User

In fact. net socket write high performance and stability of the information is really less, a substantial test data results will be less. the socket communication capability after listen 2.0 SP1 is very strong, and we can suspend 6 w I/O at the same time (we can simply think that sending + receive within one second can reach 6 W or higher ), however, it seems difficult to test the data. but in a Ms Performance Test on WCF, TCP-based Deployment performance can be achieved in a 4-way server (http://msdn.microsoft.com/en-us/library/bb310550.aspx ). therefore, it is okay for WCF to achieve C # socket-based Io processing. some time ago, even if a core e4300 PC is fully qualified for processing 6 w I/O per second, it also includes data receiving, protocol analysis, and writing to image serialization.

The following is a summary of some experiences:

Buffer Pool

Needless to say, this usage is also introduced on msdn. But how to allocate the buffer size? We usually use a buffer for a message, which is difficult to allocate at this time. We have to define the buffer length according to the maximum message length. In this way, it is extremely unreasonable to allocate memory. however, you don't need to worry about the memory too much now (maybe ). however, there is still a problem that the buffer is generally used for sending. When a message is small, I/O operations must be performed based on the number of messages, if the flexibility of the buffer is better, for example, a small message can be written into one buffer multiple times, and a large message can be written into several buffers. in this way, the memory usage is reasonable. it can also control the least I/O processing information to achieve better performance.

Is the socketasynceventargs pool required?

This usage is also introduced on msdn, but there is a problem of how to allocate, allocate when the connection is generated, and recycle when the connection is disconnected? If how the saea allocates the buffer size (do not use the setbuffer (byte [], Int, INT) method easily), you have to deal with the above buffer problems. therefore, SEAE and buffer pool are the same size to be used in combination to allocate the pool. When used, they are used up and recycled to the pool. in fact, it is best to integrate SEAE with buffer. The advantage of doing so is that it can save a lot of byte [] copy in high concurrency.

Queue usage

The use of the queue is to better control the thread to process data. Since a thread can better complete the work, it is necessary to use more threads. remember to do the most with the least threads.

Can you minimize the memory allocation during the runtime?

Can you make some getbyes methods not generate new byte? Let's take a good look at the msdn-related methods to see how to achieve this effect.

Less self-confidence, more analysis tools

Maybe you have many years of experience and have used some timers to measure it.CodeExecution line performance. however, sometimes it is hard to find that the original performance does not exist in your test. do not focus only on the CPU execution time of the Code. net has a giant GC. when using performance analysis tools to analyze the code execution time, do not forget to analyze the Code's memory allocation in some situations. vs2010 provides these convenient analysis tools.

The following are the test results optimized during this period:

Test result 1:

1 K connection gets a pair and a list pair respectively

Single Object Information

 
Class getresponse: iMessage {public user; Public void save (bufferwriter writer)} class user: iMessage {public string name; Public String email; Public String city; Public String counrty; public void save (bufferwriter writer) {writer. write (name); writer. write (email); writer. write (city); writer. write (counrty);} public void load (bufferreader reader) {name = reader. readstring (); email = reader. readstring (); City = reader. readstring (); counrty = reader. readstring ();}}

List object information (5)

Class response: iMessage {private ilist <order> morders = new list <order> (); Public ilist <order> orders {get {return morders ;}} class order: iMessage {public int orderid; Public String customerid; Public int employeeid; public long orderdate; public long requireddate; Public String shipname; Public String shipaddress; Public String shipcity; Public String shipregion; public String shippostalcode; Public String shipcountry; Public void save (bufferwriter writer) {writer. write (orderid); writer. write (customerid); writer. write (employeeid); writer. write (orderdate); writer. write (requireddate); writer. write (shipname); writer. write (shipaddress); writer. write (shipcity); writer. write (shipregion); writer. write (shippostalcode); writer. write (shipcountry);} public void load (bufferreader reader) {orderid = reader. readint32 (); customerid = reader. readstring (); employeeid = reader. readint32 (); orderdate = reader. readint64 (); requireddate = reader. readint64 (); shipname = reader. readstring (); shipaddress = reader. readstring (); shipcity = reader. readstring (); shipregion = reader. readstring (); shippostalcode = reader. readstring (); shipcountry = reader. readstring ();}}

Test Results

Test result 2:

Due to lan bandwidth restrictions, you can only obtain a single object under 2 K and 3 K connections.

In addition to the above test results, the broadcast of 500 object state change in the same scenario is also carried out, and the core e4300 is fully competent to forward 50 million messages per second. the message delay of each client is within Ms.

Broadcast test for objects in the same scenarioProgram: Http://www.henryfan.net/file.axd? File000022%2f3%2fbroadcasttest.rar (to test the effect, run it on separate computers. If your network is 500 mb, you can run up to five clients. If the server's CPU is less than core e4300, it may not support of the same screen)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.