Search code examples
c#benchmarkdotnet

Can't run benchmark when I declare a constructor


I am new to benchmarking, I am trying to benchmark my controllers endpoints, but I'm getting an error BenchmarkDotNet has failed to build the auto-generated boilerplate code., when I run some tests without the class constructor everything runs smoothly... something I must be doing wrong...

(I have created a startup class for dependency injection)

benchmark class

    [MemoryDiagnoser]
    public class UserController
    {
        private readonly UsersController _controller;
        // when I comment this out everything runs smoothly...
        /*public UserController(ApplicationDbContext context, IWebHostEnvironment hostingEnvironment) 
        {
            _controller = new UsersController(context, hostingEnvironment);
        }*/

        [Benchmark]
        public async Task LoginUserErrorAsync()
        {
            var test = ("kibkjbc").Substring(2);
            // await _controller.LoginUserAsync();
        }
    }

program class

    internal class Program
    {
        static void Main(string[] args)
        {
            var config = new ManualConfig()
                .WithOptions(ConfigOptions.DisableOptimizationsValidator)
                .AddValidator(JitOptimizationsValidator.DontFailOnError)
                .AddLogger(ConsoleLogger.Default)
                .AddColumnProvider(DefaultColumnProviders.Instance);

            BenchmarkRunner.Run<UserController>(config);
        }
    }

full error output

// Build Error: Standard output:

 Standard error:
 MSBuild version 17.4.0+18d5aef85 for .NET
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj : warning NU1701: Package 'ImageProcessor 2.9.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj : warning NU1701: Package 'ImageProcessor.Plugins.WebP 1.3.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.9' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\1846966c-92e8-4ff7-aca2-8664b906401e.notcs(189,16): error CS7036: There is no argument given that corresponds to the required parameter 'context' of 'UserController.UserController(ApplicationDbContext, IWebHostEnvironment)' [C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj]
Build FAILED.
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj : warning NU1701: Package 'ImageProcessor 2.9.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj : warning NU1701: Package 'ImageProcessor.Plugins.WebP 1.3.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj : warning NU1701: Package 'Microsoft.AspNet.WebApi.Core 5.2.9' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net7.0'. This package may not be fully compatible with your project.
C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\1846966c-92e8-4ff7-aca2-8664b906401e.notcs(189,16): error CS7036: There is no argument given that corresponds to the required parameter 'context' of 'UserController.UserController(ApplicationDbContext, IWebHostEnvironment)' [C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e\BenchmarkDotNet.Autogenerated.csproj]
    3 Warning(s)
    1 Error(s)
Time Elapsed 00:00:03.73

// BenchmarkDotNet has failed to build the auto-generated boilerplate code.
// It can be found in C:\Users\Hoizen\source\repos\PrestigeServer\PrestigeServer.Benchmark\bin\Release\net7.0\1846966c-92e8-4ff7-aca2-8664b906401e
// Please follow the troubleshooting guide: https://benchmarkdotnet.org/articles/guides/troubleshooting.html

Solution

  • BenchmarkDotNet need a default constructor to instantiate the class. Check this for how to setup benchmarks with DI.