【问题标题】:How to benchmark two different versions of the same non-NuGet library in BenchmarkDotNet?如何在 BenchmarkDotNet 中对同一个非 NuGet 库的两个不同版本进行基准测试?
【发布时间】:2019-11-22 08:32:41
【问题描述】:

我正在尝试使用 BenchmarkDotNet 为库准备性能回归测试。这要求我将相同的测试与旧(稳定)版本的库进行比较。现在,可以选择提供different versions of a NuGet package to a job。似乎没有引用不同程序集的选项。

我尝试过自定义构建配置:

  <ItemGroup Condition="'$(Configuration)' == 'Baseline'">
    <Reference Include="MyAssembly">
      <HintPath>lib\baseline\MyAssembly.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition="'$(Configuration)' != 'Baseline'">
    <Reference Include="MyAssembly">
      <HintPath>lib\current\MyAssembly.dll</HintPath>
    </Reference>
  </ItemGroup>

但是当尝试通过 BenchmarkDotNet 使用这些配置时

    public static void Main(string[] args) {
      var summary = BenchmarkRunner.Run(typeof(Program).Assembly,
        DefaultConfig.Instance
          .With(Job.Default.WithCustomBuildConfiguration("Baseline"))
          .With(Job.Default.WithCustomBuildConfiguration("Current")));
    }

我收到构建错误,表明该程序集根本没有被引用。 BenchmarkDotNet 还有助于清除它创建的任何临时工件,因此我什至无法查看生成的项目文件来弄清楚它的外观。

这里唯一的解决方法是将库包装在 NuGet 包中吗?还是我在(对于这种情况下看似稀疏的)文档中忽略了什么?

This issue 似乎与我遇到的构建错误隐约相关。

【问题讨论】:

  • 你的目标是哪个框架?您如何从命令提示符完全运行它?你能发布你得到的错误吗?或者创建一个带有 repro 案例的小型 GitHub 存储库?
  • 顺便说一句,使用自定义构建配置时,您需要记住启用优化,默认情况下仅对 Release 启用优化(这是 MSBuild 行为)xml &lt;Optimize&gt;true&lt;/Optimize&gt;

标签: benchmarkdotnet


【解决方案1】:

我能够得到类似的工作,这也可能对你有用。就我而言,我想从基准项目的相同构建中引用 NuGet 和本地项目。

在添加PackageReferenceProjectReference 之后,我(手动)将Aliases 添加到ProjectReference

<ItemGroup>
  <ProjectReference Include="..\src\Combinatorics\Combinatorics.csproj" Aliases="localbuild" />
</ItemGroup>

dll 名称确实需要不同(因为两个 dll 最终都在输出目录中)。所以我更改了引用项目的 dll 名称(但仅在本地构建时而不是在进行持续集成构建时):

<PropertyGroup>
  <AssemblyName Condition="'$(CI)'!='true'">local.Combinatorics</AssemblyName>
</PropertyGroup>

然后我可以使用extern aliasusing 在同一个应用程序中使用这两个程序集:

extern alias localbuild;
using NugetCombinatorics = Combinatorics.Collections;
using LocalCombinatorics = localbuild::Combinatorics.Collections;

...

[BenchmarkCategory("Enumerate"), Benchmark(Baseline = true)]
public void EnumerateOld()
{
    var permutations = new NugetCombinatorics.Permutations<int>(_source);
    foreach (var p in permutations)
        ;
}

[BenchmarkCategory("Enumerate"), Benchmark]
public void EnumerateNew()
{
    var permutations = new LocalCombinatorics.Permutations<int>(_source);
    foreach (var p in permutations)
        ;
}

重复的代码并不理想,我有点滥用类别而不是使用正确的运行,但它完成了工作。

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 2018-05-05
    • 2016-02-20
    • 1970-01-01
    • 2011-03-28
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多