diff --git a/src/DotCompressorBenchmark.Tools/BenchmarkDeflate.cs b/src/DotCompressorBenchmark.Tools/BenchmarkDeflate.cs new file mode 100644 index 0000000..811672d --- /dev/null +++ b/src/DotCompressorBenchmark.Tools/BenchmarkDeflate.cs @@ -0,0 +1,39 @@ +using System.IO; +using System.IO.Compression; + +namespace DotCompressorBenchmark.Tools; + +public class BenchmarkDeflate : IBenchmark +{ + public string Name { get; } + private readonly CompressionLevel _level; + + public BenchmarkDeflate(CompressionLevel level) + { + _level = level; + Name = $"Deflate {_level.ToString()}"; + } + + public BenchmarkResult Roundtrip(string filename, byte[] srcBytes, byte[] dstBytes) + { + return Benchmarks.Roundtrip(Name, filename, srcBytes, dstBytes, (s, d) => CompressGZip(s, d, _level), DecompressGZip); + } + + public static long CompressGZip(byte[] uncompressedBytes, byte[] compressedBytes, CompressionLevel level) + { + using MemoryStream ms = new MemoryStream(compressedBytes); + using (DeflateStream gzipStream = new DeflateStream(ms, level, true)) + { + gzipStream.Write(uncompressedBytes, 0, uncompressedBytes.Length); + } + + return ms.Position; + } + + public static long DecompressGZip(byte[] compressedBytes, long size, byte[] uncompressedBytes) + { + using MemoryStream ms = new MemoryStream(compressedBytes, 0, (int)size); + using DeflateStream gzipStream = new DeflateStream(ms, CompressionMode.Decompress); + return gzipStream.Read(uncompressedBytes); + } +} \ No newline at end of file diff --git a/src/DotCompressorBenchmark.Tools/BenchmarkGZip.cs b/src/DotCompressorBenchmark.Tools/BenchmarkGZip.cs index e168045..0c64c5b 100644 --- a/src/DotCompressorBenchmark.Tools/BenchmarkGZip.cs +++ b/src/DotCompressorBenchmark.Tools/BenchmarkGZip.cs @@ -5,21 +5,25 @@ namespace DotCompressorBenchmark.Tools; public class BenchmarkGZip : IBenchmark { - public string Name { get; } = "GZip"; + public string Name { get; } - public BenchmarkGZip() + private readonly CompressionLevel _level; + + public BenchmarkGZip(CompressionLevel level) { + _level = level; + Name = $"GZip {_level.ToString()}"; } public BenchmarkResult Roundtrip(string filename, byte[] srcBytes, byte[] dstBytes) { - return Benchmarks.Roundtrip(Name, filename, srcBytes, dstBytes, (s, d) => CompressGZip(s, d), DecompressGZip); + return Benchmarks.Roundtrip(Name, filename, srcBytes, dstBytes, (s, d) => CompressGZip(s, d, _level), DecompressGZip); } - private static long CompressGZip(byte[] uncompressedBytes, byte[] compressedBytes) + public static long CompressGZip(byte[] uncompressedBytes, byte[] compressedBytes, CompressionLevel level) { using MemoryStream ms = new MemoryStream(compressedBytes); - using (GZipStream gzipStream = new GZipStream(ms, CompressionMode.Compress, true)) + using (GZipStream gzipStream = new GZipStream(ms, level, true)) { gzipStream.Write(uncompressedBytes, 0, uncompressedBytes.Length); } @@ -27,7 +31,7 @@ private static long CompressGZip(byte[] uncompressedBytes, byte[] compressedByte return ms.Position; } - private static long DecompressGZip(byte[] compressedBytes, long size, byte[] uncompressedBytes) + public static long DecompressGZip(byte[] compressedBytes, long size, byte[] uncompressedBytes) { using MemoryStream ms = new MemoryStream(compressedBytes, 0, (int)size); using (GZipStream gzipStream = new GZipStream(ms, CompressionMode.Decompress)) diff --git a/src/DotCompressorBenchmark.Tools/BenchmarkZip.cs b/src/DotCompressorBenchmark.Tools/BenchmarkZip.cs index ee2ad6e..e6cdd2b 100644 --- a/src/DotCompressorBenchmark.Tools/BenchmarkZip.cs +++ b/src/DotCompressorBenchmark.Tools/BenchmarkZip.cs @@ -37,7 +37,7 @@ public class BenchmarkZip : IBenchmark public BenchmarkZip(CompressionLevel level) { _level = level; - Name = $"System.Io.Zip {_level.ToString()}"; + Name = $"Zip {_level.ToString()}"; } public BenchmarkResult Roundtrip(string filename, byte[] srcBytes, byte[] dstBytes) diff --git a/src/DotCompressorBenchmark.Tools/Benchmarks.cs b/src/DotCompressorBenchmark.Tools/Benchmarks.cs index b869564..b8443bb 100644 --- a/src/DotCompressorBenchmark.Tools/Benchmarks.cs +++ b/src/DotCompressorBenchmark.Tools/Benchmarks.cs @@ -43,24 +43,31 @@ public static List Benchmark(List files) // MemCopy benchmarks.Add(new BenchmarkMemCopy()); - // FastLZ - benchmarks.Add(new BenchmarkDotFastLZ(1)); - benchmarks.Add(new BenchmarkDotFastLZ(2)); + // // FastLZ + // benchmarks.Add(new BenchmarkDotFastLZ(1)); + // benchmarks.Add(new BenchmarkDotFastLZ(2)); + // + // // LZ4 + // foreach (LZ4Level value in Enum.GetValues(typeof(LZ4Level))) + // { + // benchmarks.Add(new BenchmarkK4osLZ4(value)); + // } + // + // // Zip + // benchmarks.Add(new BenchmarkZip(CompressionLevel.Optimal)); + // benchmarks.Add(new BenchmarkZip(CompressionLevel.Fastest)); + // benchmarks.Add(new BenchmarkZip(CompressionLevel.SmallestSize)); + // + // // GZip + // benchmarks.Add(new BenchmarkGZip(CompressionLevel.Optimal)); + // benchmarks.Add(new BenchmarkGZip(CompressionLevel.Fastest)); + // benchmarks.Add(new BenchmarkGZip(CompressionLevel.SmallestSize)); - // LZ4 - foreach (LZ4Level value in Enum.GetValues(typeof(LZ4Level))) - { - benchmarks.Add(new BenchmarkK4osLZ4(value)); - } - - // Zip - foreach (CompressionLevel value in Enum.GetValues(typeof(CompressionLevel))) - { - benchmarks.Add(new BenchmarkZip(value)); - } - - // GZip - benchmarks.Add(new BenchmarkGZip()); + // Deflate + benchmarks.Add(new BenchmarkDeflate(CompressionLevel.Optimal)); + benchmarks.Add(new BenchmarkDeflate(CompressionLevel.Fastest)); + benchmarks.Add(new BenchmarkDeflate(CompressionLevel.SmallestSize)); + var results = new List();