Skip to content

Instantly share code, notes, and snippets.

@mjebrahimi
Last active December 3, 2022 11:52
Show Gist options
  • Save mjebrahimi/3d3f02ff29a9bf471ee0d47bcdbc68eb to your computer and use it in GitHub Desktop.
Save mjebrahimi/3d3f02ff29a9bf471ee0d47bcdbc68eb to your computer and use it in GitHub Desktop.
BenchmarkDotNet example with most useful attributes and features
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);
}
}
}
@mjebrahimi
Copy link
Author

Another config

using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Horology;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Environments;

namespace DNTPersianUtils.Core.PerformanceTests
{
    class Program
    {
        static void Main(string[] args)
        {
            runBenchmarks();
        }

        private static void runBenchmarks()
        {
            var config = ManualConfig.Create(DefaultConfig.Instance)
                                .With(BenchmarkDotNet.Analysers.EnvironmentAnalyser.Default)
                                .With(BenchmarkDotNet.Exporters.MarkdownExporter.GitHub)
                                .With(BenchmarkDotNet.Diagnosers.MemoryDiagnoser.Default)
                                .With(StatisticColumn.Mean)
                                .With(StatisticColumn.Median)
                                .With(StatisticColumn.StdDev)
                                .With(StatisticColumn.OperationsPerSecond)
                                .With(BaselineRatioColumn.RatioMean)
                                .With(RankColumn.Arabic)
                                .With(Job.Default.With(CoreRuntime.Core31)
                                    .WithIterationCount(300)
                                    .WithInvocationCount(160)
                                    .WithIterationTime(TimeInterval.FromSeconds(1000))
                                    .WithWarmupCount(4)
                                    .WithLaunchCount(1));
            BenchmarkRunner.Run<BenchmarkTestsForStringReplace>(config);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment