From 7c20a942e3be7cb4650374601559c3ec4de511b5 Mon Sep 17 00:00:00 2001 From: ikpil Date: Sun, 3 Dec 2023 13:49:36 +0900 Subject: [PATCH] add snappy --- .../BenchmarkSnappy.cs | 38 +++++++++++++++++++ .../Benchmarks.cs | 2 + .../DotCompressorBenchmark.Tools.csproj | 1 + 3 files changed, 41 insertions(+) create mode 100644 src/DotCompressorBenchmark.Tools/BenchmarkSnappy.cs diff --git a/src/DotCompressorBenchmark.Tools/BenchmarkSnappy.cs b/src/DotCompressorBenchmark.Tools/BenchmarkSnappy.cs new file mode 100644 index 0000000..ff72945 --- /dev/null +++ b/src/DotCompressorBenchmark.Tools/BenchmarkSnappy.cs @@ -0,0 +1,38 @@ +using System.IO; +using System.IO.Compression; +using Snappier; + +namespace DotCompressorBenchmark.Tools; + +public class BenchmarkSnappy : IBenchmark +{ + public string Name { get; } + + public BenchmarkSnappy() + { + Name = $"Snappy"; + } + + public BenchmarkResult Roundtrip(string filename, byte[] srcBytes, byte[] dstBytes) + { + return Benchmarks.Roundtrip(Name, filename, srcBytes, dstBytes, (s, d) => Compress(s, d), Decompress); + } + + public static long Compress(byte[] uncompressedBytes, byte[] compressedBytes) + { + using MemoryStream ms = new MemoryStream(compressedBytes); + using (SnappyStream stream = new SnappyStream(ms, CompressionMode.Compress, true)) + { + stream.Write(uncompressedBytes, 0, uncompressedBytes.Length); + } + + return ms.Position; + } + + public static long Decompress(byte[] compressedBytes, long size, byte[] uncompressedBytes) + { + using MemoryStream ms = new MemoryStream(compressedBytes, 0, (int)size); + using SnappyStream stream = new SnappyStream(ms, CompressionMode.Decompress, true); + return stream.Read(uncompressedBytes); + } +} \ No newline at end of file diff --git a/src/DotCompressorBenchmark.Tools/Benchmarks.cs b/src/DotCompressorBenchmark.Tools/Benchmarks.cs index 85607c6..d7c1c9a 100644 --- a/src/DotCompressorBenchmark.Tools/Benchmarks.cs +++ b/src/DotCompressorBenchmark.Tools/Benchmarks.cs @@ -78,6 +78,8 @@ public static List Benchmark(List files) benchmarks.Add(new BenchmarkZLib(CompressionLevel.Fastest)); //benchmarks.Add(new BenchmarkZLib(CompressionLevel.SmallestSize)); + benchmarks.Add(new BenchmarkSnappy()); + var results = new List(); foreach (var file in files) { diff --git a/src/DotCompressorBenchmark.Tools/DotCompressorBenchmark.Tools.csproj b/src/DotCompressorBenchmark.Tools/DotCompressorBenchmark.Tools.csproj index 356103b..f1bfeba 100644 --- a/src/DotCompressorBenchmark.Tools/DotCompressorBenchmark.Tools.csproj +++ b/src/DotCompressorBenchmark.Tools/DotCompressorBenchmark.Tools.csproj @@ -10,6 +10,7 @@ +