byte[] GetCompressedFile(byte[] uncompressed) { var compressed = new MemoryStream(); using (var compressor = new SnappyDOTNETStream(compressed, CompressionMode.Compress, true)) compressor.Write(uncompressed, 0, uncompressed.Length); compressed.Close(); return(compressed.ToArray()); }
public void Compression() { Benchmark.Run("Compressing", 1024 * 1024 * 2, benchmark => { var stream = new NullStream(); var compressor = new SnappyDOTNETStream(stream, CompressionMode.Compress); benchmark.Stopwatch.Start(); for (int i = 0; i < benchmark.Iterations; ++i) { compressor.Write(benchmark.Input, 0, benchmark.Input.Length); compressor.Flush(); } benchmark.Stopwatch.Stop(); benchmark.Note = String.Format(" ({0:0.00 %})", stream.Written / (double)benchmark.Input.Length / benchmark.Iterations); }); }
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); }