具有2个不同环境变量的2个不同基准

时间:2019-04-04 06:11:42

标签: c# performance benchmarkdotnet

尝试对在两个不同DLL下运行的函数drawManaged()进行基准测试:

  • drawManaged()在C#中的功能
  • drawManaged()通过swig-interface调用native1base.dll(或其他本机dll)中的drawNative()
  • 使用环境变量%NATIVE_DLL_PATH%可配置要加载的本机DLL
  • 想要对在两个不同的本机DLL中运行的基准drawManaged():native1base.dll和native2optimized.dll

如何使用Benchmarkdotnet做到这一点?

1 个答案:

答案 0 :(得分:0)

Benchmarkdotnet将对标有[Benchmark]属性的方法进行基准测试。

创建一个小的实用程序(或直接在您创建的基准项目中)来调用此函数可能会更好。尝试限制每个调用之间的所有变量,因此,不要在本机情况下设置环境变量,然后直接调用C#函数,使[Benchmark]装饰的方法仅调用您要比较的方法。

您的测试看起来像这样:

    public class DrawManagedVsDrawNative
{
    private DrawManaged drawManaged = new DrawManaged();
    private DrawNative drawNative = new DrawNative();
    private byte[] data;


    [GlobalSetup]
    public void Setup()
    {
       // Some initialization here
    }

    [Benchmark]
    public byte[] DrawManaged() => drawManaged.Draw();

    [Benchmark]
    public byte[] DrawNative() => drawNative.Draw();
}

更有基准测试网为您的测试提供的装饰器和配置选项,有关此问题,请咨询docs

如果确实必须对设置环境变量进行基准测试,则可以使用ManualConfig实现来创建自定义作业。像这样:

 [Config(typeof(ConfigWithCustomEnvVars))]
    public class DrawManagedVsDrawNative
    {
        private class ConfigWithCustomEnvVars : ManualConfig
        {
            private const string EnvVar = "Env_Var_Sample";

            public ConfigWithCustomEnvVars()
            {
                Add(Job.Core.WithId("Variable not set"));
                Add(Job.Core
                    .With(new[] { new EnvironmentVariable(EnvVar, "1") })
                    .WithId("Variable set"));
            }
        }

    private DrawManaged drawManaged = new DrawManaged();
    private DrawNative drawNative = new DrawNative();
    private byte[] data;


    [GlobalSetup]
    public void Setup()
    {
       // Some initialization here
    }

    [Benchmark]
    public byte[] DrawManaged() => drawManaged.Draw();

    [Benchmark]
    public byte[] DrawNative() => drawNative.Draw();

    }