|
| 1 | +using Benchmark.BenchmarkNetUtilities; |
| 2 | +using BenchmarkDotNet.Configs; |
| 3 | +using K4os.Compression.LZ4.Encoders; |
| 4 | +using K4os.Compression.LZ4.Streams; |
| 5 | +using MemoryPack; |
| 6 | +using MemoryPack.Compression; |
| 7 | +using System.IO.Compression; |
| 8 | + |
| 9 | +namespace Benchmark.Benchmarks; |
| 10 | + |
| 11 | +[CategoriesColumn] |
| 12 | +[PayloadColumn] |
| 13 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 14 | +public class Compression<T> : SerializerTestBase<T> |
| 15 | +{ |
| 16 | + MemoryStream ms; |
| 17 | + LZ4EncoderSettings Fast; |
| 18 | + LZ4EncoderSettings L10Opt; |
| 19 | + LZ4EncoderSettings L04HC; |
| 20 | + byte[] normal; |
| 21 | + byte[] brotliFast; |
| 22 | + byte[] lz4Fast; |
| 23 | + |
| 24 | + public Compression() |
| 25 | + : base() |
| 26 | + { |
| 27 | + ms = new MemoryStream(); |
| 28 | + Fast = new LZ4EncoderSettings { CompressionLevel = K4os.Compression.LZ4.LZ4Level.L00_FAST }; |
| 29 | + L10Opt = new LZ4EncoderSettings { CompressionLevel = K4os.Compression.LZ4.LZ4Level.L10_OPT }; |
| 30 | + L04HC = new LZ4EncoderSettings { CompressionLevel = K4os.Compression.LZ4.LZ4Level.L04_HC }; |
| 31 | + |
| 32 | + normal = SerializeMemoryPack(); |
| 33 | + brotliFast = BrotliCompressQ1(); |
| 34 | + lz4Fast = LZ4CompressStreamFast(); |
| 35 | + } |
| 36 | + |
| 37 | + [Benchmark(Baseline = true), BenchmarkCategory(Categories.Serialize)] |
| 38 | + public byte[] SerializeMemoryPack() |
| 39 | + { |
| 40 | + return MemoryPackSerializer.Serialize(value, MemoryPackSerializeOptions.Utf8); |
| 41 | + } |
| 42 | + |
| 43 | + [Benchmark] |
| 44 | + public byte[] SerializeMemoryPackUtf16() |
| 45 | + { |
| 46 | + return MemoryPackSerializer.Serialize(value, MemoryPackSerializeOptions.Utf16); |
| 47 | + } |
| 48 | + |
| 49 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 50 | + public byte[] BrotliCompressQ1() |
| 51 | + { |
| 52 | + using var compressor = new BrotliCompressor(quality: 1); |
| 53 | + MemoryPackSerializer.Serialize(compressor, value, MemoryPackSerializeOptions.Utf8); |
| 54 | + return compressor.ToArray(); |
| 55 | + } |
| 56 | + |
| 57 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 58 | + public byte[] BrotliCompressQ2() |
| 59 | + { |
| 60 | + using var compressor = new BrotliCompressor(quality: 2); |
| 61 | + MemoryPackSerializer.Serialize(compressor, value, MemoryPackSerializeOptions.Utf8); |
| 62 | + return compressor.ToArray(); |
| 63 | + } |
| 64 | + |
| 65 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 66 | + public byte[] BrotliCompressQ3() |
| 67 | + { |
| 68 | + using var compressor = new BrotliCompressor(quality: 3); |
| 69 | + MemoryPackSerializer.Serialize(compressor, value, MemoryPackSerializeOptions.Utf8); |
| 70 | + return compressor.ToArray(); |
| 71 | + } |
| 72 | + |
| 73 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 74 | + public byte[] BrotliCompressQ4() |
| 75 | + { |
| 76 | + using var compressor = new BrotliCompressor(quality: 4); |
| 77 | + MemoryPackSerializer.Serialize(compressor, value, MemoryPackSerializeOptions.Utf8); |
| 78 | + return compressor.ToArray(); |
| 79 | + } |
| 80 | + |
| 81 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 82 | + public byte[] BrotliCompressStreamFastest() |
| 83 | + { |
| 84 | + ms.Position = 0; |
| 85 | + using (var brotli = new BrotliStream(ms, CompressionLevel.Fastest, leaveOpen: true)) |
| 86 | + { |
| 87 | + MemoryPackSerializer.SerializeAsync(brotli, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 88 | + } |
| 89 | + ms.Flush(); |
| 90 | + return ms.ToArray(); |
| 91 | + } |
| 92 | + |
| 93 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 94 | + public byte[] BrotliCompressStreamOptimial() |
| 95 | + { |
| 96 | + ms.Position = 0; |
| 97 | + using (var brotli = new BrotliStream(ms, CompressionLevel.Optimal, leaveOpen: true)) |
| 98 | + { |
| 99 | + MemoryPackSerializer.SerializeAsync(brotli, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 100 | + } |
| 101 | + ms.Flush(); |
| 102 | + return ms.ToArray(); |
| 103 | + } |
| 104 | + |
| 105 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 106 | + public byte[] BrotliCompressStreamSmallestSize() |
| 107 | + { |
| 108 | + ms.Position = 0; |
| 109 | + using (var brotli = new BrotliStream(ms, CompressionLevel.SmallestSize, leaveOpen: true)) |
| 110 | + { |
| 111 | + MemoryPackSerializer.SerializeAsync(brotli, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 112 | + } |
| 113 | + ms.Flush(); |
| 114 | + return ms.ToArray(); |
| 115 | + } |
| 116 | + |
| 117 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 118 | + public byte[] BrotliCompressStreamNoCompression() |
| 119 | + { |
| 120 | + ms.Position = 0; |
| 121 | + using (var brotli = new BrotliStream(ms, CompressionLevel.NoCompression, leaveOpen: true)) |
| 122 | + { |
| 123 | + MemoryPackSerializer.SerializeAsync(brotli, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 124 | + } |
| 125 | + ms.Flush(); |
| 126 | + return ms.ToArray(); |
| 127 | + } |
| 128 | + |
| 129 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 130 | + public byte[] LZ4CompressStreamFast() |
| 131 | + { |
| 132 | + |
| 133 | + |
| 134 | + ms.Position = 0; |
| 135 | + using (var lz4 = LZ4Stream.Encode(ms, Fast, leaveOpen: true)) |
| 136 | + { |
| 137 | + MemoryPackSerializer.SerializeAsync(lz4, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 138 | + } |
| 139 | + ms.Flush(); |
| 140 | + return ms.ToArray(); |
| 141 | + } |
| 142 | + |
| 143 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 144 | + public byte[] LZ4CompressStreamHc04() |
| 145 | + { |
| 146 | + |
| 147 | + |
| 148 | + ms.Position = 0; |
| 149 | + using (var lz4 = LZ4Stream.Encode(ms, L04HC, leaveOpen: true)) |
| 150 | + { |
| 151 | + MemoryPackSerializer.SerializeAsync(lz4, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 152 | + } |
| 153 | + ms.Flush(); |
| 154 | + return ms.ToArray(); |
| 155 | + } |
| 156 | + |
| 157 | + [Benchmark, BenchmarkCategory(Categories.Serialize)] |
| 158 | + public byte[] LZ4CompressStreamL10Opt() |
| 159 | + { |
| 160 | + ms.Position = 0; |
| 161 | + using (var lz4 = LZ4Stream.Encode(ms, L10Opt, leaveOpen: true)) |
| 162 | + { |
| 163 | + MemoryPackSerializer.SerializeAsync(lz4, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 164 | + } |
| 165 | + ms.Flush(); |
| 166 | + return ms.ToArray(); |
| 167 | + } |
| 168 | + |
| 169 | + //Decompress |
| 170 | + |
| 171 | + [Benchmark(Baseline = true), BenchmarkCategory(Categories.Deserialize)] |
| 172 | + public T? DeserializeMemoryPack() |
| 173 | + { |
| 174 | + return MemoryPackSerializer.Deserialize<T>(normal); |
| 175 | + } |
| 176 | + |
| 177 | + [Benchmark(), BenchmarkCategory(Categories.Deserialize)] |
| 178 | + public T? DecompressBrotli() |
| 179 | + { |
| 180 | + using var decompressor = new BrotliDecompressor(); |
| 181 | + return MemoryPackSerializer.Deserialize<T>(decompressor.Decompress(brotliFast)); |
| 182 | + } |
| 183 | + |
| 184 | + [Benchmark(), BenchmarkCategory(Categories.Deserialize)] |
| 185 | + public T? DecompressBrotliStream() |
| 186 | + { |
| 187 | + using (var ms2 = new MemoryStream(brotliFast)) |
| 188 | + using (var brotli = new BrotliStream(ms2, CompressionMode.Decompress)) |
| 189 | + { |
| 190 | + return MemoryPackSerializer.DeserializeAsync<T>(brotli).GetAwaiter().GetResult(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + [Benchmark(), BenchmarkCategory(Categories.Deserialize)] |
| 195 | + public T? LZ4Decompress() |
| 196 | + { |
| 197 | + using (var ms2 = new MemoryStream(lz4Fast)) |
| 198 | + using (var lz4 = LZ4Stream.Decode(ms2, leaveOpen: true)) |
| 199 | + { |
| 200 | + return MemoryPackSerializer.DeserializeAsync<T>(lz4).GetAwaiter().GetResult(); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // GZip |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + //[Benchmark] |
| 209 | + //public byte[] GZipCompressStreamFastest() |
| 210 | + //{ |
| 211 | + // ms.Position = 0; |
| 212 | + // using (var GZip = new GZipStream(ms, CompressionLevel.Fastest, leaveOpen: true)) |
| 213 | + // { |
| 214 | + // MemoryPackSerializer.SerializeAsync(GZip, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 215 | + // } |
| 216 | + // ms.Flush(); |
| 217 | + // return ms.ToArray(); |
| 218 | + //} |
| 219 | + |
| 220 | + //[Benchmark] |
| 221 | + //public byte[] GZipCompressStreamOptimial() |
| 222 | + //{ |
| 223 | + // ms.Position = 0; |
| 224 | + // using (var GZip = new GZipStream(ms, CompressionLevel.Optimal, leaveOpen: true)) |
| 225 | + // { |
| 226 | + // MemoryPackSerializer.SerializeAsync(GZip, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 227 | + // } |
| 228 | + // ms.Flush(); |
| 229 | + // return ms.ToArray(); |
| 230 | + //} |
| 231 | + |
| 232 | + //[Benchmark] |
| 233 | + //public byte[] GZipCompressStreamSmallestSize() |
| 234 | + //{ |
| 235 | + // ms.Position = 0; |
| 236 | + // using (var GZip = new GZipStream(ms, CompressionLevel.SmallestSize, leaveOpen: true)) |
| 237 | + // { |
| 238 | + // MemoryPackSerializer.SerializeAsync(GZip, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 239 | + // } |
| 240 | + // ms.Flush(); |
| 241 | + // return ms.ToArray(); |
| 242 | + //} |
| 243 | + |
| 244 | + //[Benchmark] |
| 245 | + //public byte[] GZipCompressStreamNoCompression() |
| 246 | + //{ |
| 247 | + // ms.Position = 0; |
| 248 | + // using (var GZip = new GZipStream(ms, CompressionLevel.NoCompression, leaveOpen: true)) |
| 249 | + // { |
| 250 | + // MemoryPackSerializer.SerializeAsync(GZip, value, MemoryPackSerializeOptions.Utf8).ConfigureAwait(false).GetAwaiter().GetResult(); |
| 251 | + // } |
| 252 | + // ms.Flush(); |
| 253 | + // return ms.ToArray(); |
| 254 | + //} |
| 255 | + |
| 256 | +} |
0 commit comments