Search code examples
c#benchmarkingbenchmarkdotnet

Show Only Summary Section of BenchmarkDotNet


I'm benchmarking some .net framework stuffs, I'm using .net framework, C# and BenchmarkDotNet

What I want to do is; I'm writing a lot of benchmark tests and I'm only interested in summary sections of the reports. How can I configure BenchmarkDotNet to show only summary section of tests?

Here is a screenshot to be more clear;

enter image description here


Solution

  • Why do you want skip logs? Benchmarks could take a lot of time, if you disable logs, you will look at the black screen for some time. If something goes wrong, then you will not know about it.

    However, there is a workaround. BenchmarkDotNet uses special Configs for customization. Loggers are a part of these Configs. If you don't specify any config, the default one will be using it. You can easily extend it, but there is no nice API to disable a part of the default config (hopefully, will be added soon; the corresponded API is in the discussion stage right now). So, you have to define own config, add all parts of the default config except loggers and pass it to BenchmarkRunner. Then the ConsoleLogger will not be used. After that, you have to print the summary table and conclusions to console manually. Also, the full log and the summary table in markdown format will be in the BenchmarkDotNet.Artifacts folder.

    The source code:

    var config = new ManualConfig();
    config.Add(DefaultConfig.Instance.GetColumnProviders().ToArray());
    config.Add(DefaultConfig.Instance.GetExporters().ToArray());
    config.Add(DefaultConfig.Instance.GetDiagnosers().ToArray());
    config.Add(DefaultConfig.Instance.GetAnalysers().ToArray());
    config.Add(DefaultConfig.Instance.GetJobs().ToArray());
    config.Add(DefaultConfig.Instance.GetValidators().ToArray());
    config.UnionRule = ConfigUnionRule.AlwaysUseGlobal; // Overriding the default
    
    var summary = BenchmarkRunner.Run<TestBench>(config);
    
    var logger = ConsoleLogger.Default;
    MarkdownExporter.Console.ExportToLog(summary, logger);
    ConclusionHelper.Print(logger, config.GetCompositeAnalyser().Analyse(summary).ToList());