public void Twister()
        {
            var testdata = new int[]{1,2,3,4,5,6,7,8,9,10}.Select(x=>_dataCreator.GenerateTestData(1024*512*x)).ToArray();

            long totalData = 0;
            var stopwatch = new Stopwatch();
            stopwatch.Start();
            while (stopwatch.Elapsed < TimeSpan.FromSeconds(3))
            {
                int count = Random.Next(0, 21);
                var sequence = Enumerable.Range(0, count).Select(n => testdata[Random.Next(testdata.Length)]).ToArray();
                totalData += sequence.Sum(f => f.Length);
                var stream = new RandomChunkStream();
                ManualResetEvent doneReading = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(ctx =>
                {
                    try
                    {
                        using (var decompressor = new SnappyDOTNETStream(stream, CompressionMode.Decompress))
                        {
                            foreach (var file in sequence)
                            {
                                var decompressed = new byte[file.Length];
                                if (decompressed.Length < 500)
                                {
                                    for (int i = 0; i < decompressed.Length; ++i)
                                        decompressed[i] = checked((byte)decompressor.ReadByte());
                                }
                                else
                                {
                                    ReadAll(decompressor, decompressed, 0, decompressed.Length);
                                }
                                CheckBuffers(file, decompressed);
                            }
                            Assert.AreEqual(-1, decompressor.ReadByte());
                        }
                        doneReading.Set();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Read thread failed: {0}", e);
                        throw;
                    }
                });
                using (var compressor = new SnappyDOTNETStream(stream, CompressionMode.Compress))
                {
                    foreach (var file in sequence)
                    {
                        if (file.Length < 500)
                        {
                            for (int i = 0; i < file.Length; ++i)
                                compressor.WriteByte(file[i]);
                        }
                        else
                        {
                            compressor.Write(file, 0, file.Length);
                        }
                        if (WriteRandom.Next(10) == 0)
                        {

                            compressor.Flush();
                        }
                    }

                    {
                        if (WriteRandom.Next(2) == 0)
                            compressor.Flush();
                    }
                }
                doneReading.WaitOne();
            }
            stopwatch.Stop();
            Console.WriteLine("Ran {0} MB through the stream, that's {1:0.0} MB/s", totalData / 1024 / 1024, totalData / stopwatch.Elapsed.TotalSeconds / 1024 / 1024);
        }
Beispiel #2
0
        public void Twister()
        {
            var testdata = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.Select(x => _dataCreator.GenerateTestData(1024 * 512 * x)).ToArray();


            long totalData = 0;
            var  stopwatch = new Stopwatch();

            stopwatch.Start();
            while (stopwatch.Elapsed < TimeSpan.FromSeconds(3))
            {
                int count    = Random.Next(0, 21);
                var sequence = Enumerable.Range(0, count).Select(n => testdata[Random.Next(testdata.Length)]).ToArray();
                totalData += sequence.Sum(f => f.Length);
                var stream = new RandomChunkStream();
                ManualResetEvent doneReading = new ManualResetEvent(false);
                ThreadPool.QueueUserWorkItem(ctx =>
                {
                    try
                    {
                        using (var decompressor = new SnappyDOTNETStream(stream, CompressionMode.Decompress))
                        {
                            foreach (var file in sequence)
                            {
                                var decompressed = new byte[file.Length];
                                if (decompressed.Length < 500)
                                {
                                    for (int i = 0; i < decompressed.Length; ++i)
                                    {
                                        decompressed[i] = checked ((byte)decompressor.ReadByte());
                                    }
                                }
                                else
                                {
                                    ReadAll(decompressor, decompressed, 0, decompressed.Length);
                                }
                                CheckBuffers(file, decompressed);
                            }
                            Assert.AreEqual(-1, decompressor.ReadByte());
                        }
                        doneReading.Set();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Read thread failed: {0}", e);
                        throw;
                    }
                });
                using (var compressor = new SnappyDOTNETStream(stream, CompressionMode.Compress))
                {
                    foreach (var file in sequence)
                    {
                        if (file.Length < 500)
                        {
                            for (int i = 0; i < file.Length; ++i)
                            {
                                compressor.WriteByte(file[i]);
                            }
                        }
                        else
                        {
                            compressor.Write(file, 0, file.Length);
                        }
                        if (WriteRandom.Next(10) == 0)
                        {
                            compressor.Flush();
                        }
                    }

                    {
                        if (WriteRandom.Next(2) == 0)
                        {
                            compressor.Flush();
                        }
                    }
                }
                doneReading.WaitOne();
            }
            stopwatch.Stop();
            Console.WriteLine("Ran {0} MB through the stream, that's {1:0.0} MB/s", totalData / 1024 / 1024, totalData / stopwatch.Elapsed.TotalSeconds / 1024 / 1024);
        }