New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to use non compile-time constants as Parameters #571

Closed
adamsitnik opened this Issue Oct 22, 2017 · 1 comment

Comments

Projects
None yet
1 participant
@adamsitnik
Member

adamsitnik commented Oct 22, 2017

IParam

In case you want to use values which are not compile-time constants, you have to create a type which implements IParam interface. This is required because internally BenchmarkDotNet generates and compiles code for every benchmark. We know how to deal with primitive types, but we don't want to implement complex logic for creating complex types. This responsibility is transferred to the users ;)

Example (IParam)

public class IntroIParam
{
   public struct VeryCustomStruct
   {
       public readonly int X, Y;

       public VeryCustomStruct(int x, int y)
       {
           X = x;
           Y = y;
       }
   }

   public class CustomParam : IParam
   {
       private readonly VeryCustomStruct value;

       public CustomParam(VeryCustomStruct value) => this.value = value;

       public object Value => value;

       public string DisplayText => $"({value.X},{value.Y})";

       public string ToSourceCode() => $"new VeryCustomStruct({value.X}, {value.Y})";
   }

   [ParamsSource(nameof(Parameters))]
   public VeryCustomStruct Field;

   public IEnumerable<IParam> Parameters()
   {
       yield return new CustomParam(new VeryCustomStruct(100, 10));
       yield return new CustomParam(new VeryCustomStruct(100, 20));
       yield return new CustomParam(new VeryCustomStruct(200, 10));
       yield return new CustomParam(new VeryCustomStruct(200, 20));
   }

   [Benchmark]
   public void Benchmark() => Thread.Sleep(Field.X + Field.Y);
}
Method Field Mean Error StdDev
Benchmark (100,10) 110.4 ms 0.1148 ms 0.1074 ms
Benchmark (100,20) 120.4 ms 0.0843 ms 0.0788 ms
Benchmark (200,10) 210.4 ms 0.0892 ms 0.0834 ms
Benchmark (200,20) 220.4 ms 0.0949 ms 0.0887 ms

@adamsitnik adamsitnik added this to the v0.10.10 milestone Oct 22, 2017

@adamsitnik adamsitnik self-assigned this Oct 22, 2017

@adamsitnik adamsitnik referenced this issue Oct 22, 2017

Closed

ParamsSource #350

@adamsitnik

This comment has been minimized.

Show comment
Hide comment
@adamsitnik

adamsitnik Oct 22, 2017

Member

I know that it's not perfect solution, but it allows to get people what they need. And it's also very flexible ;)

Member

adamsitnik commented Oct 22, 2017

I know that it's not perfect solution, but it allows to get people what they need. And it's also very flexible ;)

alinasmirnova added a commit to alinasmirnova/BenchmarkDotNet that referenced this issue Sep 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment