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 System.Collections.Immutable;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;

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 13.0.1
        private class Config : ManualConfig
        {
            public Config()
            {
                var baseJob = Job.MediumRun;

                string[] targetVersions = [
                    "9.0.0",
                    "9.0.3",
                    "9.0.5",
                ];

                foreach (var version in targetVersions)
                {
                    AddJob(baseJob.WithNuGet("System.Collections.Immutable", version)
                                  .WithId($"v{version}"));
                }
            }
        }

        private static readonly Random rand = new Random(Seed: 0);
        private static readonly double[] values = Enumerable.Range(1, 10_000).Select(x => rand.NextDouble()).ToArray();

        [Benchmark]
        public void ToImmutableArrayBenchmark()
        {
            var results = values.ToImmutableArray();
        }
    }
}

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