Esempio n. 1
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplSocketClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Wait until the connection is actually made
            client.WaitForConnection();

            // Create a byte array to send
            var messageToSend = new byte[1000];

            // Send it
            Console.WriteLine("Client will send message and wait for reply");
            var receivedMessage = client.SendReceive(messageToSend);

            if (receivedMessage == null)
            {
                Console.WriteLine("Client received no answer");
            }
            else
            {
                Console.WriteLine($"Client received answer of {receivedMessage.Length} bytes");

                // Return message to pool
                receivedMessage.Return();
            }
        }
Esempio n. 2
0
        private void SendFromPoolAndReceiveCheck(SimplSocketClient client)
        {
            Log("*** SendFromPoolAndReceiveCheck ***");
            var rnd = new Random();

            var randomData = PooledMessage.Rent(1000 * 512);

            rnd.NextBytes(randomData.Content);

            if (!client.IsConnected())
            {
                client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
            }

            var outputData = client.SendReceive(randomData);
            var same       = true;

            for (int i = 0; i < outputData.Length; i++)
            {
                if (outputData.Content[i] != randomData.Content[i])
                {
                    same = false;
                    break;
                }
            }
            randomData.Return();
            outputData.Return();
            Log(same ? "data is same" : "Data is not the same");
        }
Esempio n. 3
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplSocketClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Wait until the connection is actually made
            client.WaitForConnection();

            // Create a byte array to send
            var arrayToSend1 = new byte[1000];

            // Send it
            client.Send(arrayToSend1);

            // Get a byte array from the memory pool
            var arrayToSend2 = PooledMessage.Rent(1000);

            client.Send(arrayToSend2);

            // We will not dispose the message directly, since it may still need to be send
            // Instead we use the ReturnAfterSend, which will return the message to the pool after sending
            arrayToSend2.ReturnAfterSend();
        }
Esempio n. 4
0
        private void SendBenchmark(SimplSocketClient client)
        {
            Log("*** SendBenchmark ***");
            var rnd         = new Random();
            var bufferSizes = new[] { 1, 10, 100, 1000 };
            var length      = 0;

            for (var test = 0; test < 4; test++)
            {
                byte[] randomData = new byte[bufferSizes[test] * 512];
                length += randomData.Length;
                rnd.NextBytes(randomData);

                var countPerIteration = 100;
                var watch             = Stopwatch.StartNew();
                for (var i = 0; i < countPerIteration; i++)
                {
                    randomData = new byte[bufferSizes[test] * 512];

                    if (!client.IsConnected())
                    {
                        client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
                    }

                    client.Send(randomData);
                }

                watch.Stop();
                var speed       = (countPerIteration * length) / (watch.ElapsedMilliseconds / 1000.0);
                var scaledSpeed = ScaledSpeed(speed);
                Log($"{countPerIteration}x{length}: {watch.ElapsedMilliseconds}ms = {scaledSpeed} ");
            }
        }
Esempio n. 5
0
        private void SendAndReceiveCheck(SimplSocketClient client)
        {
            Log("*** SendAndReceiveCheck ***");
            var rnd = new Random();

            byte[] randomData = new byte[1000 * 512];
            rnd.NextBytes(randomData);

            if (!client.IsConnected())
            {
                client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
            }

            var outputData = client.SendReceive(randomData);

            if (outputData == null)
            {
                Log("No answer received"); return;
            }

            var same = true;

            for (int i = 0; i < outputData.Length; i++)
            {
                if (outputData.Content[i] != randomData[i])
                {
                    same = false;
                    break;
                }
            }
            // We need to return the output data to pool
            outputData.Return();
            Log(same?"data is same":"Data is not the same");
        }
Esempio n. 6
0
 private static async Task RunBenchmarkViaSockets()
 {
     using (var client = new SimplSocketClient(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)))
     {
         var work = new SocketsWorkUnit(client);
         await BenchmarkClient(work);
     }
 }
Esempio n. 7
0
        void RunSocketBenchmarks()
        {
            // Fill randomData array

            using (var client = new SimplSocketClient(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            {
                NoDelay = true
            }))
            {
                // subscribe to broadcasts
                // client.MessageReceived += async (s, e) => await WriteLineAsync('*', e.ReceivedMessage.PooledMessage);
                client.MessageReceived += (s, e) => { e.ReceivedMessage.Dispose(); };


                client.Connected += (s, e) =>
                {
                    Console.WriteLine("The client has connected!");
                };
                client.Disconnected += (s, e) =>
                {
                    Console.WriteLine("The client has disconnected!");
                };


                SendAndReceiveCheck(client);
                WaitUntilRentedMessagesReturn();

                SendFromPoolAndReceiveCheck(client);
                WaitUntilRentedMessagesReturn();

                SendBenchmark(client);
                WaitUntilRentedMessagesReturn();

                SendFromPoolBenchmark(client);
                WaitUntilRentedMessagesReturn();
                SendFromPoolAndReceiveBenchmark(client);
                WaitUntilRentedMessagesReturn();
                SendAndReceiveBenchmark(client);
                WaitUntilRentedMessagesReturn();
                SendBenchmark(client);
                WaitUntilRentedMessagesReturn();
                SendFromPoolBenchmark(client);
                WaitUntilRentedMessagesReturn();
                SendFromPoolAndReceiveBenchmark(client);
                WaitUntilRentedMessagesReturn();
                SendAndReceiveBenchmark(client);
                WaitUntilRentedMessagesReturn();

                Console.ReadLine();
            }
        }
Esempio n. 8
0
        //[Benchmark(OperationsPerInvoke = Ops)]
        public long c1_s1()
        {
            long x = 0;

            using (var client = new SimplSocketClient(CreateSocket))
            {
                client.Connect(s1);
                for (int i = 0; i < Ops; i++)
                {
                    var response = client.SendReceive(_data);
                    x += response.Length;
                }
            }
            return(AssertResult(x));
        }
Esempio n. 9
0
        void CreateClient()
        {
            // Create the client
            var client = new SimplSocketClient();

            // Make the client connect automatically
            client.AutoConnect();

            // Create a callback for received data
            client.MessageReceived += (s, e) =>
            {
                // Get the message
                var receivedMessage = e.ReceivedMessage;
                Console.WriteLine($"Client received message of {receivedMessage.Length} bytes");

                // Return the message to the pool
                receivedMessage.Return();
            };
        }
Esempio n. 10
0
        private void SendFromPoolAndReceiveBenchmark(SimplSocketClient client)
        {
            Log("*** SendFromPoolAndReceiveBenchmark ***");
            var rnd         = new Random();
            var bufferSizes = new[] { 1, 10, 100, 1000 };
            var length      = 0;

            for (var test = 0; test < 4; test++)
            {
                var randomData = PooledMessage.Rent(bufferSizes[test] * 512);
                length += randomData.Length;
                rnd.NextBytes(randomData.Content);

                var countPerIteration = 100;
                var watch             = Stopwatch.StartNew();
                for (var i = 0; i < countPerIteration; i++)
                {
                    if (!client.IsConnected())
                    {
                        client.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
                    }

                    var response = client.SendReceive(randomData);
                    if (response == null)
                    {
                        Log("No response ");
                    }
                    else
                    {
                        response.Return();
                    }
                }

                watch.Stop();
                var speed       = (countPerIteration * length) / (watch.ElapsedMilliseconds / 1000.0);
                var scaledSpeed = ScaledSpeed(speed);
                Log($"{countPerIteration}x{length}: {watch.ElapsedMilliseconds}ms = {scaledSpeed} ");
                randomData.Return();
            }
        }
Esempio n. 11
0
 /// <summary>
 /// The constructor.
 /// </summary>
 /// <param name="socketFunc">The function that creates a new socket. Use this to specify your socket constructor and initialize settings.</param>
 /// <param name="keepAlive">Whether or not to send keep-alive packets and detect if alive</param>
 /// <param name="messageBufferSize">The message buffer size to use for send/receive.</param>
 /// <param name="communicationTimeout">The communication timeout, in milliseconds.</param>
 /// <param name="maxMessageSize">The maximum message size.</param>
 /// <param name="useNagleAlgorithm">Whether or not to use the Nagle algorithm.</param>
 public SimplMessageClient(Func <Socket> socketFunc, bool keepAlive = true, int messageBufferSize = 65536,
                           int communicationTimeout = 10000, int maxMessageSize = 10 * 1024 * 1024, bool useNagleAlgorithm = false)
 {
     _simplSocketClient = new SimplSocketClient(socketFunc, keepAlive, messageBufferSize, communicationTimeout, maxMessageSize, useNagleAlgorithm);
     Initialize();
 }
Esempio n. 12
0
 public SocketsWorkUnit(SimplSocketClient client)
 => Client = client;