1

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
big boy
  • 315
  • 2
  • 13
  • The only way I've used Benchmark is when I have competing ideas or algorithms and I want to compare their performance - I suspect Benchmark was just not intended to be used the way you're using it and doesn't know how to deal with parameterized constructors. If you want to gather performance metrics on an API, Postman might be a good choice. Swagger might do that too. – Vic F Dec 05 '22 at 03:26
  • 3
    `BenchmarkDotNet` need a default constructor to instantiate the class. Check [this](https://stackoverflow.com/questions/73182338/how-to-use-dependencyinjection-in-benchmarkdotnet) for how to setup benchmarks with DI. – Sohaib Jundi Dec 05 '22 at 07:57
  • @SohaibJundi thx that solved it... post as answer and I will accept it – big boy Dec 05 '22 at 13:18

1 Answers1

1

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

Sohaib Jundi
  • 1,576
  • 2
  • 7
  • 15