-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeflateCompressorBenchmarks.cs
More file actions
101 lines (88 loc) · 3.17 KB
/
DeflateCompressorBenchmarks.cs
File metadata and controls
101 lines (88 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.CompilerServices;
namespace LibDeflate.Benchmarks;
[MemoryDiagnoser]
[SimpleJob]
public class DeflateCompressorBenchmarks
{
private static string AssetBase
{
get
{
var cwd = Directory.GetCurrentDirectory();
string assetsDir;
while (!Directory.Exists(assetsDir = Path.Join(cwd, "assets")))
{
cwd = Path.GetDirectoryName(cwd);
}
return assetsDir;
}
}
private static IEnumerable<int> Levels
{
get
{
yield return 0;
yield return 1;
yield return -1;
yield return 9;
}
}
[GlobalSetup]
public static void PrepareTestAssets()
{
var assetsFolder = Path.Join(AssetBase, "UncompressedTestFiles");
var testFiles = new Dictionary<string, byte[]>();
foreach (var file in Directory.EnumerateFiles(assetsFolder, null, SearchOption.AllDirectories))
{
var key = Path.GetRelativePath(assetsFolder, file);
testFiles.Add(key, File.ReadAllBytes(file));
}
TestFiles = testFiles;
}
public static Dictionary<string, byte[]> TestFiles { get; set; }
public static IEnumerable<object[]> GetInputs() => from key in TestFiles.Keys
from level in Levels
select new object[] { key, level };
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static CompressionLevel ToLevelEnum(int level) => level switch
{
0 => CompressionLevel.NoCompression,
1 => CompressionLevel.Fastest,
-1 => CompressionLevel.Optimal,
9 => CompressionLevel.SmallestSize
};
#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(GetInputs))]
public void DeflateSIO(string testFile, int level)
{
var input = TestFiles[testFile];
using var outputMs = new MemoryStream(input.Length);
using var deflateStream = new DeflateStream(outputMs, ToLevelEnum(level));
deflateStream.Write(input);
}
[Benchmark]
[ArgumentsSource(nameof(GetInputs))]
public void DeflateLibdeflate_MemoryOwner(string testFile, int level)
{
var input = TestFiles[testFile];
using var compressor = new DeflateCompressor(level);
using var owner = compressor.Compress(input);
}
[Benchmark]
[ArgumentsSource(nameof(GetInputs))]
public void DeflateLibdeflate_Buffer(string testFile, int level)
{
var input = TestFiles[testFile];
using var compressor = new DeflateCompressor(level);
var output = new byte[input.Length];
var bytesWritten = compressor.Compress(input, output);
}
}