Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Deflate #2

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/DotCompressorBenchmark.Tools/BenchmarkDeflate.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 10 additions & 6 deletions src/DotCompressorBenchmark.Tools/BenchmarkGZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,33 @@ 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);
}

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))
Expand Down
2 changes: 1 addition & 1 deletion src/DotCompressorBenchmark.Tools/BenchmarkZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 24 additions & 17 deletions src/DotCompressorBenchmark.Tools/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,31 @@ public static List<BenchmarkResult> Benchmark(List<string> 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<BenchmarkResult>();
Expand Down