Table of Contents

Sample: IntroNuGet

You can set specific versions of NuGet dependencies for each job. It allows comparing different versions of the same package (if there are no breaking changes in API).

Source code

using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using Newtonsoft.Json;

namespace BenchmarkDotNet.Samples
{
    /// <summary>
    /// Benchmarks between various versions of a NuGet package
    /// </summary>
    /// <remarks>
    /// Only supported with the CsProjCoreToolchain toolchain
    /// </remarks>
    [Config(typeof(Config))]
    public class IntroNuGet
    {
        // Specify jobs with different versions of the same NuGet package to benchmark.
        // The NuGet versions referenced on these jobs must be greater or equal to the
        // same NuGet version referenced in this benchmark project.
        // Example: This benchmark project references Newtonsoft.Json 9.0.1
        private class Config : ManualConfig
        {
            public Config()
            {
                var baseJob = Job.MediumRun;

                AddJob(baseJob.WithNuGet("Newtonsoft.Json", "11.0.2").WithId("11.0.2"));
                AddJob(baseJob.WithNuGet("Newtonsoft.Json", "11.0.1").WithId("11.0.1"));
                AddJob(baseJob.WithNuGet("Newtonsoft.Json", "10.0.3").WithId("10.0.3"));
                AddJob(baseJob.WithNuGet("Newtonsoft.Json", "10.0.2").WithId("10.0.2"));
                AddJob(baseJob.WithNuGet("Newtonsoft.Json", "10.0.1").WithId("10.0.1"));
                AddJob(baseJob.WithNuGet("Newtonsoft.Json", "9.0.1").WithId("9.0.1"));
            }
        }

        [Benchmark]
        public void SerializeAnonymousObject()
            => JsonConvert.SerializeObject(
                new { hello = "world", price = 1.99, now = DateTime.UtcNow });
    }
}

Output

Method Job NuGetReferences Mean Error StdDev
SerializeAnonymousObject 10.0.1 Newtonsoft.Json 10.0.1 2.926 us 0.0795 us 0.0283 us
SerializeAnonymousObject 10.0.2 Newtonsoft.Json 10.0.2 2.877 us 0.5928 us 0.2114 us
SerializeAnonymousObject 10.0.3 Newtonsoft.Json 10.0.3 2.706 us 0.1251 us 0.0446 us
SerializeAnonymousObject 11.0.1 Newtonsoft.Json 11.0.1 2.778 us 0.5037 us 0.1796 us
SerializeAnonymousObject 11.0.2 Newtonsoft.Json 11.0.2 2.644 us 0.0609 us 0.0217 us
SerializeAnonymousObject 9.0.1 Newtonsoft.Json 9.0.1 2.722 us 0.3552 us 0.1267 us