Exemplo n.º 1
1
        static void Main(string[] args)
        {
            int writeCount = 0;
            int clientWaitCount = 0;
            int readCount = 0;
            int serverWaitCount = 0;
            long lastTick = 0;
            byte[] readData = new byte[1048576];
            int size = sizeof(byte) * 1048576;
            int count = 2; // node count within buffer

            long bytesWritten = 0;
            long bytesRead = 0;
            string name = Guid.NewGuid().ToString();
            var server = new SharedMemory.CircularBuffer(name, count, size);
            
            Stopwatch sw = Stopwatch.StartNew();

            Action clientAction = () =>
            {
                byte[] testData = new byte[size];

                var client = new SharedMemory.CircularBuffer(name);

                Stopwatch clientTime = new Stopwatch();
                clientTime.Start();
                long startTick = 0;
                long stopTick = 0;

                for (; ; )
                {
                    startTick = clientTime.ElapsedTicks;
                    int amount = client.Read(testData, 100);
                    bytesRead += amount;
                    if (amount == 0)
                        Interlocked.Increment(ref clientWaitCount);
                    else
                        Interlocked.Increment(ref readCount);
                    stopTick = clientTime.ElapsedTicks;

                    if (writeCount > 100000 && writeCount - readCount == 0)
                        break;
                }
            };
#if NET40Plus
            Task c1 = Task.Factory.StartNew(clientAction);
            Task c2 = Task.Factory.StartNew(clientAction);
            Task c3 = Task.Factory.StartNew(clientAction);
            Task c4 = Task.Factory.StartNew(clientAction);
            //Task c5 = Task.Factory.StartNew(clientAction);
            //Task c6 = Task.Factory.StartNew(clientAction);
            //Task c7 = Task.Factory.StartNew(clientAction);
#else
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
#endif
            int index = 0;

            Action serverWrite = () =>
            {
                int serverIndex = Interlocked.Increment(ref index);

                var writer = (serverIndex == 1 ? server : new SharedMemory.CircularBuffer(name));

                for (; ; )
                {
                    if (writeCount <= 100000)
                    {
                        int amount = writer.Write(readData, 100);
                        bytesWritten += amount;
                        if (amount == 0)
                            Interlocked.Increment(ref serverWaitCount);
                        else
                            Interlocked.Increment(ref writeCount);
                    }

                    if (serverIndex == 1 && sw.ElapsedTicks - lastTick > 1000000)
                    {
                        lastTick = sw.ElapsedTicks;
                        Console.WriteLine("Write: {0}, Read: {1}, Diff: {5}, Wait(cli/svr): {3}/{2}, {4}MB/s", writeCount, readCount, serverWaitCount, clientWaitCount, (int)((((bytesWritten + bytesRead) / 1048576.0) / sw.ElapsedMilliseconds) * 1000), writeCount - readCount);

                        if (writeCount > 100000 && writeCount - readCount == 0)
                            break;
                    }
                }
            };

#if NET40Plus
            Task s1 = Task.Factory.StartNew(serverWrite);
            //Task s2 = Task.Factory.StartNew(serverWrite);
            //Task s3 = Task.Factory.StartNew(serverWrite);
            //Task s4 = Task.Factory.StartNew(serverWrite);
            //Task s5 = Task.Factory.StartNew(serverWrite);
            //Task s6 = Task.Factory.StartNew(serverWrite);
            //Task s7 = Task.Factory.StartNew(serverWrite);
#else
            ThreadPool.QueueUserWorkItem((o) => { serverWrite(); });
#endif
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            SampleData data = new SampleData()
            {
                Destination = Mode.Client,
                Message     = "From Server"
            };

            byte[] writeBuffer = new byte[1024];
            byte[] readBuffer  = new byte[1024];
            SharedMemory.CircularBuffer sendRing = new SharedMemory.CircularBuffer("Test", 50, writeBuffer.Length);
            SharedMemory.CircularBuffer readRing = new SharedMemory.CircularBuffer("TestReader", 50, writeBuffer.Length);
            long counter = 0;

            do
            {
                {
                    string          message      = string.Format("Test Message {0}", counter++);
                    BinaryFormatter formatter    = new BinaryFormatter();
                    MemoryStream    memoryStream = new MemoryStream();
                    formatter.Serialize(memoryStream, message);
                    writeBuffer = memoryStream.ToArray();
                    sendRing.Write(writeBuffer);
                }

                if (readRing.Read(readBuffer) > 0)
                {
                    BinaryFormatter formatter    = new BinaryFormatter();
                    MemoryStream    memoryStream = new MemoryStream(readBuffer);
                    string          readData     = formatter.Deserialize(memoryStream) as string;
                    Console.WriteLine(string.Format("Found message: {0}", readData));
                }
            } while (true);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            int    size = 900 * 720 * 2;
            string name = "SharedMemoryTest";

            Random r = new Random();

            byte[] data = new byte[size];


            var buf      = new SharedMemory.CircularBuffer(name, 2, size);
            var bufWrite = new SharedMemory.CircularBuffer(name + "W", 2, size);

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    int count = buf.Read(data, 0, -1);
                    if (count != 0)
                    {
                        Console.WriteLine(data[0]);
                    }

                    bufWrite.Write(data);
                }
            });

            Console.ReadKey();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            int  writeCount      = 0;
            int  clientWaitCount = 0;
            int  readCount       = 0;
            int  serverWaitCount = 0;
            long lastTick        = 0;

            byte[] readData = new byte[1048576];
            int    size     = sizeof(byte) * 1048576;
            int    count    = 2; // node count within buffer

            long   bytesWritten = 0;
            long   bytesRead    = 0;
            string name         = Guid.NewGuid().ToString();
            var    server       = new SharedMemory.CircularBuffer(name, count, size);

            Stopwatch sw = Stopwatch.StartNew();

            Action clientAction = () =>
            {
                byte[] testData = new byte[size];

                var client = new SharedMemory.CircularBuffer(name);

                Stopwatch clientTime = new Stopwatch();
                clientTime.Start();
                long startTick = 0;
                long stopTick  = 0;

                for (; ;)
                {
                    startTick = clientTime.ElapsedTicks;
                    int amount = client.Read(testData, 100);
                    bytesRead += amount;
                    if (amount == 0)
                    {
                        Interlocked.Increment(ref clientWaitCount);
                    }
                    else
                    {
                        Interlocked.Increment(ref readCount);
                    }
                    stopTick = clientTime.ElapsedTicks;

                    if (writeCount > 100000 && writeCount - readCount == 0)
                    {
                        break;
                    }
                }
            };

#if NET40Plus
            Task c1 = Task.Factory.StartNew(clientAction);
            Task c2 = Task.Factory.StartNew(clientAction);
            Task c3 = Task.Factory.StartNew(clientAction);
            Task c4 = Task.Factory.StartNew(clientAction);
            //Task c5 = Task.Factory.StartNew(clientAction);
            //Task c6 = Task.Factory.StartNew(clientAction);
            //Task c7 = Task.Factory.StartNew(clientAction);
#else
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
            ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
#endif
            int index = 0;

            Action serverWrite = () =>
            {
                int serverIndex = Interlocked.Increment(ref index);

                var writer = (serverIndex == 1 ? server : new SharedMemory.CircularBuffer(name));

                for (; ;)
                {
                    if (writeCount <= 100000)
                    {
                        int amount = writer.Write(readData, 100);
                        bytesWritten += amount;
                        if (amount == 0)
                        {
                            Interlocked.Increment(ref serverWaitCount);
                        }
                        else
                        {
                            Interlocked.Increment(ref writeCount);
                        }
                    }

                    if (serverIndex == 1 && sw.ElapsedTicks - lastTick > 1000000)
                    {
                        lastTick = sw.ElapsedTicks;
                        Console.WriteLine("Write: {0}, Read: {1}, Diff: {5}, Wait(cli/svr): {3}/{2}, {4}MB/s", writeCount, readCount, serverWaitCount, clientWaitCount, (int)((((bytesWritten + bytesRead) / 1048576.0) / sw.ElapsedMilliseconds) * 1000), writeCount - readCount);

                        if (writeCount > 100000 && writeCount - readCount == 0)
                        {
                            break;
                        }
                    }
                }
            };

#if NET40Plus
            Task s1 = Task.Factory.StartNew(serverWrite);
            //Task s2 = Task.Factory.StartNew(serverWrite);
            //Task s3 = Task.Factory.StartNew(serverWrite);
            //Task s4 = Task.Factory.StartNew(serverWrite);
            //Task s5 = Task.Factory.StartNew(serverWrite);
            //Task s6 = Task.Factory.StartNew(serverWrite);
            //Task s7 = Task.Factory.StartNew(serverWrite);
#else
            ThreadPool.QueueUserWorkItem((o) => { serverWrite(); });
#endif
            Console.ReadLine();
        }
Exemplo n.º 5
0
 void Start()
 {
     mReadBuffer  = new SharedMemory.CircularBuffer("Test");
     mWriteBuffer = new SharedMemory.CircularBuffer("TestReader");
 }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            int bufferSize = 1048576;

            byte[] readData;
            int    size  = sizeof(byte) * bufferSize;
            int    count = 50;

            // Generate data to be written
            byte[][] dataList = new byte[256][];
            for (var j = 0; j < 256; j++)
            {
                var data = new byte[bufferSize];
                for (var i = 0; i < data.Length; i++)
                {
                    data[i] = (byte)((i + j) % 255);
                }
                dataList[j] = data;
            }

            Console.WriteLine("Press <enter> to start Server");
            Console.ReadLine();


            Console.WriteLine("Create shared memory circular buffer");
            using (var theServer = new SharedMemory.CircularBuffer("TEST", count, size))
            {
                Console.WriteLine("Circular buffer producer created.");
                Console.WriteLine("Ready for client...");
                Thread.Sleep(1000);

                int       skipCount   = 0;
                long      iterations  = 0;
                long      totalBytes  = 0;
                long      lastTick    = 0;
                Stopwatch sw          = Stopwatch.StartNew();
                int       threadCount = 0;
                Action    writer      = () =>
                {
                    int  myThreadIndex = Interlocked.Increment(ref threadCount);
                    int  linesOut      = 0;
                    bool finalLine     = false;
                    for (; ;)
                    {
                        readData = dataList[iterations % 255];

                        int amount = theServer.Write(readData, 100);
                        //int amount = theServer.Write<byte>(readData, 100);

                        if (amount == 0)
                        {
                            Interlocked.Increment(ref skipCount);
                        }
                        else
                        {
                            Interlocked.Add(ref totalBytes, amount);
                            Interlocked.Increment(ref iterations);
                        }

                        if (threadCount == 1 && Interlocked.Read(ref iterations) > 500)
                        {
                            finalLine = true;
                        }

                        if (myThreadIndex < 3 && (finalLine || sw.ElapsedTicks - lastTick > 1000000))
                        {
                            lastTick = sw.ElapsedTicks;
                            Console.WriteLine("Write: {0}, Wait: {1}, {2}MB/s", ((double)totalBytes / 1048576.0).ToString("F0"), skipCount, (((totalBytes / 1048576.0) / sw.ElapsedMilliseconds) * 1000).ToString("F2"));
                            linesOut++;
                            if (finalLine || (myThreadIndex > 1 && linesOut > 10))
                            {
                                Console.WriteLine("Completed.");
                                break;
                            }
                        }
                    }
                };

                writer();
                Console.WriteLine("");
                skipCount  = 0;
                iterations = 0;
                totalBytes = 0;
                lastTick   = 0;
                sw.Reset();
                sw.Start();

                Console.WriteLine("Testing throughput...");
#if NET40Plus
                Task s1 = Task.Factory.StartNew(writer);
                //Task s2 = Task.Factory.StartNew(writer);
                //Task s3 = Task.Factory.StartNew(writer);
#else
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    writer();
                });
#endif
                Console.ReadLine();
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            int bufferSize = 1048576;
            byte[] readData;
            int size = sizeof(byte) * bufferSize;
            int count = 50;

            // Generate data to be written
            byte[][] dataList = new byte[256][];
            for (var j = 0; j < 256; j++)
            {
                var data = new byte[bufferSize];
                for (var i = 0; i < data.Length; i++)
                {
                    data[i] = (byte)((i + j) % 255);
                }
                dataList[j] = data;
            }

            Console.WriteLine("Press <enter> to start Server");
            Console.ReadLine();


            Console.WriteLine("Create shared memory circular buffer");
            using (var theServer = new SharedMemory.CircularBuffer("TEST", count, size))
            {
                Console.WriteLine("Circular buffer producer created.");
                Console.WriteLine("Ready for client...");
                Thread.Sleep(1000);

                int skipCount = 0;
                long iterations = 0;
                long totalBytes = 0;
                long lastTick = 0;
                Stopwatch sw = Stopwatch.StartNew();                
                int threadCount = 0;
                Action writer = () =>
                {
                    int myThreadIndex = Interlocked.Increment(ref threadCount);
                    int linesOut = 0;
                    bool finalLine = false;
                    for (; ; )
                    {
                        readData = dataList[iterations % 255];

                        int amount = theServer.Write(readData, 100);
                        //int amount = theServer.Write<byte>(readData, 100);

                        if (amount == 0)
                        {
                            Interlocked.Increment(ref skipCount);
                        }
                        else
                        {
                            Interlocked.Add(ref totalBytes, amount);
                            Interlocked.Increment(ref iterations);
                        }

                        if (threadCount == 1 && Interlocked.Read(ref iterations) > 500)
                            finalLine = true;

                        if (myThreadIndex < 3 && (finalLine || sw.ElapsedTicks - lastTick > 1000000))
                        {
                            lastTick = sw.ElapsedTicks;
                            Console.WriteLine("Write: {0}, Wait: {1}, {2}MB/s", ((double)totalBytes / 1048576.0).ToString("F0"), skipCount, (((totalBytes / 1048576.0) / sw.ElapsedMilliseconds) * 1000).ToString("F2"));
                            linesOut++;
                            if (finalLine || (myThreadIndex > 1 && linesOut > 10))
                            {
                                Console.WriteLine("Completed.");
                                break;
                            }
                        }
                    }
                };

                writer();
                Console.WriteLine("");
                skipCount = 0;
                iterations = 0;
                totalBytes = 0;
                lastTick = 0;
                sw.Reset();
                sw.Start();

                Console.WriteLine("Testing throughput...");
#if NET40Plus
                Task s1 = Task.Factory.StartNew(writer);
                //Task s2 = Task.Factory.StartNew(writer);
                //Task s3 = Task.Factory.StartNew(writer);
#else
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    writer();
                });
#endif
                Console.ReadLine();
            }
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            int  elements        = 100000;
            int  writeCount      = 0;
            int  clientWaitCount = 0;
            int  readCount       = 0;
            int  serverWaitCount = 0;
            long lastTick        = 0;
            int  bufferSize      = 1048576;
            int  size            = sizeof(byte) * bufferSize;
            int  count           = 50; // node count within buffer

            int serverCount = 0;
            int clientCount = 0;

            // Process command line
            AppArguments parsedArgs = new AppArguments();
            var          validArgs  = Parser.ParseArgumentsWithUsage(args, parsedArgs);

            if (!validArgs)
            {
                return;
            }
            else
            {
                elements    = parsedArgs.elements;
                bufferSize  = parsedArgs.bufferSize;
                serverCount = parsedArgs.writers;
                clientCount = parsedArgs.readers;
                size        = sizeof(byte) * bufferSize;
                count       = parsedArgs.nodeCount;
            }


            Console.WriteLine("Node buffer size: {0}, count: {1}, writers: {2}, readers {3}, elements: {4}", size, count, serverCount, clientCount, elements);

            int dataListCount = 256;
            // Generate random data to be written
            Random random = new Random();

            byte[][] dataList = new byte[dataListCount][];
            for (var j = 0; j < dataListCount; j++)
            {
                var data = new byte[size];
                random.NextBytes(data);
                dataList[j] = data;
            }

            long   bytesWritten = 0;
            long   bytesRead    = 0;
            string name         = Guid.NewGuid().ToString();
            var    server       = new SharedMemory.CircularBuffer(name, count, size);

            Stopwatch sw = Stopwatch.StartNew();

            Action clientAction = () =>
            {
                byte[] testData = new byte[size];

                var client = new SharedMemory.CircularBuffer(name);

                Stopwatch clientTime = new Stopwatch();
                clientTime.Start();
                long startTick = 0;
                long stopTick  = 0;

                for (; ;)
                {
                    startTick = clientTime.ElapsedTicks;
                    int amount = client.Read(testData, 100);
                    bytesRead += amount;
                    if (amount == 0)
                    {
                        Interlocked.Increment(ref clientWaitCount);
                    }
                    else
                    {
                        Interlocked.Increment(ref readCount);
                    }
                    stopTick = clientTime.ElapsedTicks;

                    if (writeCount > elements && writeCount - readCount == 0)
                    {
                        break;
                    }
                }
            };

            for (int c = 0; c < clientCount; c++)
            {
#if NET40Plus
                Task c1 = Task.Factory.StartNew(clientAction);
#else
                ThreadPool.QueueUserWorkItem((o) => { clientAction(); });
#endif
            }
            bool   wait        = true;
            int    index       = 0;
            Action serverWrite = () =>
            {
                int serverIndex = Interlocked.Increment(ref index);

                var      writer   = (serverIndex == 1 ? server : new SharedMemory.CircularBuffer(name));
                bool     done     = false;
                TimeSpan doneTime = TimeSpan.MinValue;
                for (; ;)
                {
                    if (writeCount <= elements)
                    {
                        int amount = writer.Write(dataList[random.Next(0, dataListCount)], 100);
                        bytesWritten += amount;
                        if (amount == 0)
                        {
                            Interlocked.Increment(ref serverWaitCount);
                        }
                        else
                        {
                            Interlocked.Increment(ref writeCount);
                        }
                    }
                    else
                    {
                        if (!done && serverIndex == 1)
                        {
                            doneTime = sw.Elapsed;
                            done     = true;
                        }
                    }

                    if (serverIndex == 1 && sw.ElapsedTicks - lastTick > 1000000)
                    {
                        Console.WriteLine("Write: {0}, Read: {1}, Diff: {5}, Wait(cli/svr): {3}/{2}, {4}MB/s", writeCount, readCount, serverWaitCount, clientWaitCount, (int)((((bytesWritten + bytesRead) / 1048576.0) / sw.ElapsedMilliseconds) * 1000), writeCount - readCount);
                        lastTick = sw.ElapsedTicks;
                        if (writeCount > elements && writeCount - readCount == 0)
                        {
                            Console.WriteLine("Total Time: " + doneTime);
                            wait = false;
                            break;
                        }
                    }
                }
            };

            for (int s = 0; s < serverCount; s++)
            {
#if NET40Plus
                Task s1 = Task.Factory.StartNew(serverWrite);
#else
                ThreadPool.QueueUserWorkItem((o) => { serverWrite(); });
#endif
            }
            while (wait)
            {
                Thread.Sleep(100);
            }
        }