コード例 #1
0
ファイル: Program.cs プロジェクト: robertvazan/snappy.net
        static void Main(string[] args)
        {
            var data = Encoding.ASCII.GetBytes("Hello World!");

            Console.WriteLine("SnappyCodec roundtrip: {0}", Encoding.ASCII.GetString(SnappyCodec.Uncompress(SnappyCodec.Compress(data))));

            var buffer = new MemoryStream();
            var stream = new SnappyStream(buffer, CompressionMode.Compress);

            stream.WriteAsync(data, 0, data.Length).Wait();
            stream.Close();
            buffer = new MemoryStream(buffer.ToArray());
            stream = new SnappyStream(buffer, CompressionMode.Decompress);
            var roundtrip = new byte[data.Length];
            int read      = stream.ReadAsync(roundtrip, 0, data.Length).Result;

            if (read != data.Length)
            {
                throw new ApplicationException();
            }
            if (0 != stream.ReadAsync(roundtrip, 0, data.Length).Result)
            {
                throw new ApplicationException();
            }
            Console.WriteLine("SnappyStream async roundtrip: {0}", Encoding.ASCII.GetString(roundtrip));
        }
コード例 #2
0
 public void AsyncCompression(string name)
 {
     Benchmark.Run("Async-compressing", name, benchmark =>
     {
         var stream     = new NullStream();
         var compressor = new SnappyStream(stream, CompressionMode.Compress);
         benchmark.Stopwatch.Start();
         for (int i = 0; i < benchmark.Iterations; ++i)
         {
             compressor.WriteAsync(benchmark.Input, 0, benchmark.Input.Length).Wait();
             compressor.FlushAsync().Wait();
         }
         benchmark.Stopwatch.Stop();
         benchmark.Note = String.Format(" ({0:0.00 %})", stream.Written / (double)benchmark.Input.Length / benchmark.Iterations);
     });
 }
コード例 #3
0
        public void Twister()
        {
            var  testdata  = Directory.GetFiles(Benchmark.DataPath).Select(f => File.ReadAllBytes(f)).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 SnappyStream(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
                                {
#if SNAPPY_ASYNC
                                    if (ReadRandom.Next(2) == 0)
                                    {
                                        ReadAllAsync(decompressor, decompressed, 0, decompressed.Length).Wait();
                                    }
                                    else
#endif
                                    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 SnappyStream(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
                        {
#if SNAPPY_ASYNC
                            if (WriteRandom.Next(2) == 0)
                            {
                                compressor.WriteAsync(file, 0, file.Length).Wait();
                            }
                            else
#endif
                            compressor.Write(file, 0, file.Length);
                        }
                        if (WriteRandom.Next(10) == 0)
                        {
#if SNAPPY_ASYNC
                            if (WriteRandom.Next(2) == 0)
                            {
                                compressor.FlushAsync().Wait();
                            }
                            else
#endif
                            compressor.Flush();
                        }
                    }
#if SNAPPY_ASYNC
                    if (WriteRandom.Next(3) == 0)
                    {
                        compressor.FlushAsync().Wait();
                    }
                    else
#endif
                    {
                        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);
        }