Last active
December 3, 2022 11:52
-
-
Save mjebrahimi/3d3f02ff29a9bf471ee0d47bcdbc68eb to your computer and use it in GitHub Desktop.
BenchmarkDotNet example with most useful attributes and features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Configs; | |
using BenchmarkDotNet.Exporters; | |
using BenchmarkDotNet.Exporters.Csv; | |
using BenchmarkDotNet.Order; | |
using BenchmarkDotNet.Running; | |
using EasyCompressor; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
namespace EasySerializer.Benchmark | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
#if DEBUG | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine("*****To achieve accurate results, set project configuration to Release mode.*****"); | |
return; | |
#endif | |
BenchmarkRunner.Run<BenchmarkContainer>(); | |
//new ManualConfig { Options = ConfigOptions.DisableLogFile }; | |
//BenchmarkRunner.Run<BenchmarkContainer>(new DebugInProcessConfig()); | |
} | |
} | |
} | |
namespace EasySerializer.Benchmark | |
{ | |
//[DryJob(RuntimeMoniker.NetCoreApp31)] | |
//[LegacyJitX86Job, LegacyJitX64Job, RyuJitX64Job] | |
//[SimpleJob] | |
//[SimpleJob(RunStrategy.Throughput)] | |
//[SimpleJob(RuntimeMoniker.NetCoreApp31)] | |
//[InProcess(true)] | |
//[NativeMemoryProfiler] | |
//[ThreadingDiagnoser] | |
//[ConcurrencyVisualizerProfiler] | |
//[TailCallDiagnoser] | |
//[InliningDiagnoser(true, true)] | |
//[EtwProfiler] | |
//[DisassemblyDiagnoser] //[DisassemblyDiagnoser(printAsm: true, printSource: true)] | |
//[CategoriesColumn, GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] | |
[RPlotExporter] | |
[ShortRunJob] | |
//[DryJob] | |
[MemoryDiagnoser] | |
[KeepBenchmarkFiles(false)] | |
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByMethod)] | |
[RankColumn, Orderer(SummaryOrderPolicy.FastestToSlowest, MethodOrderPolicy.Declared)] | |
public class BenchmarkContainer | |
{ | |
//[Params(1, 2, 3)] | |
//public int X { get; set; } | |
public byte[] ObjectBytes; | |
//[ParamsSource(nameof(GetCompressors))] | |
//public ICompressor Compressor { get; set; } | |
public IEnumerable<ICompressor> GetCompressors | |
{ | |
get | |
{ | |
yield return new GZipCompressor(); | |
yield return new DeflateCompressor(); | |
yield return new BrotliCompressor(); | |
yield return new LZ4Compressor(); | |
yield return new LZMACompressor(); | |
yield return new SnappyCompressor(); | |
yield return new ZstandardCompressor(); | |
yield return new ZstdCompressor(); | |
} | |
} | |
public IEnumerable<object[]> GetDeompressors | |
{ | |
get | |
{ | |
yield return new object[] { new GZipCompressor(), new GZipCompressor().Compress(ObjectBytes) }; | |
yield return new object[] { new DeflateCompressor(), new DeflateCompressor().Compress(ObjectBytes) }; | |
yield return new object[] { new BrotliCompressor(), new BrotliCompressor().Compress(ObjectBytes) }; | |
yield return new object[] { new LZ4Compressor(), new LZ4Compressor().Compress(ObjectBytes) }; | |
yield return new object[] { new LZMACompressor(), new LZMACompressor().Compress(ObjectBytes) }; | |
yield return new object[] { new SnappyCompressor(), new SnappyCompressor().Compress(ObjectBytes) }; | |
yield return new object[] { new ZstandardCompressor(), new ZstandardCompressor().Compress(ObjectBytes) }; | |
yield return new object[] { new ZstdCompressor(), new ZstdCompressor().Compress(ObjectBytes) }; | |
} | |
} | |
//[GlobalSetup] | |
//public void GlobalSetup() | |
//{ | |
// var json = File.ReadAllText("Data\\spotifyAlbum.json"); | |
// var spotify = Serializer.FromJson<SpotifyAlbum>(json); | |
// ObjectBytes = Serializer.Serialize(spotify); | |
//} | |
public BenchmarkContainer() | |
{ | |
var json = File.ReadAllText("Data\\spotifyAlbum.json"); | |
var spotify = Serializer.FromJson<SpotifyAlbum>(json); | |
ObjectBytes = Serializer.Serialize(spotify); | |
} | |
[Benchmark] | |
[ArgumentsSource(nameof(GetCompressors))] | |
//[BenchmarkCategory("Compress")] | |
public void Compress(ICompressor Compressor) | |
{ | |
Compressor.Compress(ObjectBytes); | |
} | |
[Benchmark] | |
[ArgumentsSource(nameof(GetDeompressors))] | |
//[BenchmarkCategory("Decompress")] | |
public void Decompress(ICompressor Compressor, byte[] CompressedBytes) | |
{ | |
Compressor.Decompress(CompressedBytes); | |
} | |
[Benchmark] | |
[ArgumentsSource(nameof(GetCompressors))] | |
//[BenchmarkCategory("Compress")] | |
public void CompressAndDecompress(ICompressor Compressor) | |
{ | |
var compressedBytes = Compressor.Compress(ObjectBytes); | |
Compressor.Decompress(compressedBytes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another config