Skip to content

Commit

Permalink
Mzlib benchmarks (dotnet#2358)
Browse files Browse the repository at this point in the history
* baseline to a benchmark
* Pre-allocate memory stream, so the cost of it's creation is not included in the benchmark (#1)
---------
Co-authored-by: Adam Sitnik <[email protected]>
  • Loading branch information
VivianaDuenas authored Jul 25, 2023
1 parent 835726d commit 8f80faa
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/Microsoft.ManagedZLib/benchmarks/ManagedZLibBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public static IEnumerable<string> UncompressedTestFileNames()
}

public CompressedFile? CompressedFile;
private MemoryStream _outputStream;

[GlobalSetup]
public void Setup()
{
Debug.Assert(File != null);
CompressedFile = new CompressedFile(File, Level);
_outputStream = new MemoryStream(UncompressedData.Length);
}


Expand All @@ -44,29 +46,29 @@ public void Setup()
[GlobalCleanup]
public void Cleanup() => CompressedFile?.CompressedDataStream.Dispose();

[Benchmark]
public int DecompressNative()
[Benchmark(Baseline = true)]
public void DecompressNative()
{
CompressedFile!.CompressedDataStream.Position = 0;
MemoryStream expectedStream = new();
_outputStream.Position = 0;

System.IO.Compression.DeflateStream decompressor = new System.IO.Compression.DeflateStream(CompressedFile.CompressedDataStream, System.IO.Compression.CompressionMode.Decompress);
decompressor.CopyTo(expectedStream);
return 0;
decompressor.CopyTo(_outputStream);
}

[Benchmark]
public int DecompressManaged()
public void DecompressManaged()
{
CompressedFile!.CompressedDataStream.Position = 0;
MemoryStream expectedStream = new();
_outputStream.Position = 0;

DeflateStream decompressor = new DeflateStream(CompressedFile.CompressedDataStream, CompressionMode.Decompress);
decompressor.CopyTo(expectedStream);
return 0;
decompressor.CopyTo(_outputStream);
}

public class ProgramRun
{
static void Main() => BenchmarkRunner.Run<ManagedZLibBenchmark>();
}

}
}

0 comments on commit 8f80faa

Please sign in to comment.