Esempio n. 1
0
        //monitor response time, cpu, and memory //WebSockets: 1kb msg => 5000 request/second on Corei5 4GB RAM
        //monitor response time, cpu, and memory //TCP: 1kb msg => 8000 request/second on Corei5 4GB RAM
        static async Task Profile(SocketType socketType)
        {
            var httpAddress = $"http://localhost/test";
            var tcpAddress  = "tcp://localhost:9988";

            var hostBuilder = new ServiceHostBuilder <Service>(InstanceMode.PerCall)
                              .WithCallback <ICallback>()
                              .WithMessagePackSerializer() //it is the default
                              .WithConsoleLogger();        //default is empty

            ConnectionBuilder <IService> clientBuilder = null;

            if (socketType == SocketType.WebSocket)
            {
                hostBuilder.AddWebSocketServer(httpAddress, options =>
                {
                    //if you set this on WebSocket,
                    // it must be less or equal to the client's SendBufferSize, default 4KB
                    options.ReceiveBufferSize = 1024 * 2;
                });

                clientBuilder = new ConnectionBuilder <IService>()
                                .WithWebSocketTransport(httpAddress, options =>
                {
                    //if you set this on WebSocket,
                    // it must be less or equal to the server's ReceiveBufferSize, default 4KB
                    options.SendBufferSize = 1024 * 2;
                });
            }
            else if (socketType == SocketType.TCP)
            {
                hostBuilder.AddTcpServer(tcpAddress, options =>
                {
                    //if you set this on WebSocket,
                    // it must be less or equal to the client's SendBufferSize, default 4KB
                    options.ReceiveBufferSize = 1024 * 2;
                });

                clientBuilder = new ConnectionBuilder <IService>()
                                .WithTcpTransport(tcpAddress, options =>
                {
                    //if you set this on WebSocket,
                    // it must be less or equal to the server's ReceiveBufferSize, default 4KB
                    options.SendBufferSize = 1024 * 2;
                });
            }

            var host = hostBuilder.CreateHost();

            await host.Open();

            Console.WriteLine("Host is open");

            var client = await clientBuilder.CreateConnection();

            //1 KB, the actual message on the wire will be more than 1 KB (depends on method type, name, and payload)
            var msg = new string('*', 1000);

            for (int j = 0; j < 100; j++)
            {
                await Task.Delay(1000);

                var sw = Stopwatch.StartNew();

                for (int i = 0; i < 50000; i++)
                {
                    var resp = await client.Echo(msg);
                }

                sw.Stop();
                Console.WriteLine($">> {sw.ElapsedMilliseconds}");
            }
        }
Esempio n. 2
0
        static async Task Profile(int count, SocketType socketType, bool useSsl,
                                  int serverSendBufferSize,
                                  int serverReciveBufferSize,
                                  int clientSendBufferSize,
                                  int clientReceiveBufferSize,
                                  FramingProtocol framingProtocol)
        {
            X509Certificate2 x509Cert = null;
            string           certName = null;

            if (useSsl)
            {
                x509Cert = GetXeenyTestCertificate(out certName);
            }

            var httpAddress = $"http://localhost/test";
            var tcpAddress  = "tcp://localhost:9988";

            var hostBuilder = new ServiceHostBuilder <Service>(InstanceMode.PerCall)
                              .WithCallback <ICallback>()
                              .WithMessagePackSerializer() //it is the default
                              .WithConsoleLogger();        //default is empty

            ConnectionBuilder <IService> clientBuilder = null;

            if (socketType == SocketType.WebSocket)
            {
                hostBuilder.AddWebSocketServer(httpAddress, options =>
                {
                    options.ReceiveBufferSize = serverReciveBufferSize;
                    options.SendBufferSize    = serverReciveBufferSize;
                    if (useSsl)
                    {
                        throw new NotImplementedException();
                    }
                });

                clientBuilder = new ConnectionBuilder <IService>()
                                .WithWebSocketTransport(httpAddress, options =>
                {
                    options.SendBufferSize    = clientSendBufferSize;
                    options.ReceiveBufferSize = serverReciveBufferSize;
                    if (useSsl)
                    {
                        throw new NotImplementedException();
                    }
                });
            }
            else if (socketType == SocketType.TCP)
            {
                hostBuilder.AddTcpServer(tcpAddress, options =>
                {
                    options.ReceiveBufferSize = serverReciveBufferSize;
                    options.SendBufferSize    = serverSendBufferSize;
                    options.FramingProtocol   = framingProtocol;
                    if (useSsl)
                    {
                        options.SecuritySettings = SecuritySettings.CreateForServer(x509Cert);
                    }
                });

                clientBuilder = new ConnectionBuilder <IService>()
                                .WithTcpTransport(tcpAddress, options =>
                {
                    options.SendBufferSize    = clientSendBufferSize;
                    options.ReceiveBufferSize = clientReceiveBufferSize;
                    options.FramingProtocol   = framingProtocol;
                    if (useSsl)
                    {
                        options.SecuritySettings = SecuritySettings.CreateForClient(certName);
                    }
                });
            }

            var host = hostBuilder.CreateHost();

            await host.Open();

            Console.WriteLine("Host is open");

            var client = await clientBuilder.CreateConnection();

            //1 KB, the actual message on the wire will be more than 1 KB (depends on method type, name, and parameters)
            var msg = new string('*', 1000);

            for (int j = 0; j < 100; j++)
            {
                await Task.Delay(1000);

                var sw = Stopwatch.StartNew();

                for (int i = 0; i < count; i++)
                {
                    var resp = await client.Echo(msg);

                    //Task.Run(async () =>
                    //{
                    //    var resp = await client.Echo(msg);
                    //    Console.WriteLine(resp.Length);
                    //});
                }

                sw.Stop();
                Console.WriteLine($">> {sw.ElapsedMilliseconds}");
            }
        }