Exemplo n.º 1
0
        public static void Json(int itemCount, TestIpc ipcType, int portNumber)
        {
            SafeFileHandle pipe   = null;
            Socket         socket = null;

            var start = DateTime.Now;

            if (ipcType == TestIpc.NamedPipe)
            {
                pipe = GetPipeClient("d2n-json-pipe");
            }
            else if (ipcType == TestIpc.TcpSocket)
            {
                socket = GetSocket(portNumber);
            }
            var itemsSent = 0;

            var  serializer   = new JsonSerializer();
            var  data         = TestData.Generate(itemCount);
            uint bytesWritten = 0;

            byte[] buffer     = new byte[100 * 1024 * 1024];
            byte[] headBuffer = new byte[4];

            while (true)
            {
                var chunk = data.Take(50000);
                chunk      = chunk.ToList();
                itemsSent += chunk.Count();
                data       = data.Skip(chunk.Count());
                if (!chunk.Any())
                {
                    break;
                }
                var stream     = new MemoryStream(buffer);
                var writer     = new StreamWriter(stream);
                var jsonWriter = new JsonTextWriter(writer);
                serializer.Serialize(jsonWriter, chunk);
                jsonWriter.Flush();
                var bufferSize        = (uint)stream.Position;
                int chunkBytesWritten = 0;
                if (ipcType == TestIpc.NamedPipe)
                {
                    chunkBytesWritten = (int)WriteToPipe(pipe, buffer, bufferSize);
                    bytesWritten     += (uint)chunkBytesWritten;
                }
                else if (ipcType == TestIpc.TcpSocket)
                {
                    WriteToSocket(socket, BitConverter.GetBytes(bufferSize), 4);
                    chunkBytesWritten = WriteToSocket(socket, buffer, (int)bufferSize);
                    bytesWritten     += (uint)chunkBytesWritten;
                }
                Console.WriteLine("JSON: {0} %, {1} bytes", ((float)itemsSent / (float)itemCount) * 100, bufferSize);
            }

            var duration = DateTime.Now.Subtract(start).TotalSeconds;

            Console.WriteLine("Total {0} mbytes, duration {1} secs", bytesWritten / 1024 / 1024, duration);
            if (ipcType == TestIpc.NamedPipe)
            {
                Win32.CloseHandle(pipe);
            }
            else if (ipcType == TestIpc.TcpSocket)
            {
                socket.Dispose();
            }
        }