.NET Core初览-网络IO篇

梦想游戏人
目录:
C#

初览的应用场景为游戏服务器开发。

网络IO:

NetCore:60%CPU QPS 56W

C++:31%CPU QPS 58W

C++:68%CPU QPS 124W

性能上的差异可能还有socket操作方式有关这个得去netcore源代码找答案

C# 网络IO测试代码

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.Collections.Generic;


namespace ConsoleApp2
{
    class Session
    {
        public TcpClient client = null;
        public void Go()
        {
            client.GetStream().BeginRead(bytes, pos, 32 - pos, AsyncReadCallback, this);
        }
        void AsyncReadCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                var sess = ar.AsyncState as Session;

                sess.client.GetStream().BeginWrite(sess.bytes, sess.pos, 32, AsyncWriteCallback, sess);
            }
        }
        void AsyncWriteCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                var sess = ar.AsyncState as Session;
                sess.client.GetStream().BeginRead(sess.bytes, sess.pos, 32, AsyncReadCallback, sess);
            }
        }
        int pos = 0;
        byte[] bytes = new byte[32];
    }
    class Program
    {
        static void worker(object obj)
        {
         while(true)
            {
                Thread.Sleep(1);

                lock(obj)
                {
                    var que = obj as Queue<Session>;
                    if(que.Count>0)
                    {
                        que.Dequeue().Go();
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            var ss = new TcpListener(12125);
            ss.Start();
            var bytes = new byte[32];

            var ths = new Thread[50];
            var que = new Queue<Session>[50];

            int i = 0;
            foreach(var p in ths)
            {
                que[i] = new Queue<Session>();

                ths[i] = new Thread(   (worker));

                ths[i].Start(que[i]);
                ++i;
            }

            int balance = 0;
            while (true)
            {
                balance = ++balance %  que.Length;
                var sess = new Session();

                var task = ss.AcceptTcpClientAsync();
                while (task.IsCompleted == false)
                {
                    var cli = task.Result;
                    sess.client = cli;
                    lock (que[balance])
                    {
                        que[balance].Enqueue(sess);
                    }    
                }


            }

            ss.Stop();

            Console.WriteLine("Hello World!");
        }
    }
}

Scroll Up