cic*_*osf 6 dependency-injection .net-4.6 benchmarkdotnet
我想在我现在正在使用的一些遗留代码上使用 BenchmarkDotNet。它是用 C# Net462 编写的。这是一个庞大、古老且复杂的系统,我想对某些特定类中的一些方法进行基准测试。这些类使用依赖注入,我不确定如何做到这一点。到目前为止我看到的所有示例都没有使用任何依赖注入。
有人有什么想法或例子我可以看一下吗?
非常感谢。
Ada*_*nik 10
您需要在ctor具有属性的方法或方法中创建依赖项注入容器[GlobalSetup],解析要进行基准测试的类型并将其存储在字段中。然后在基准测试中使用它并在方法中处置 DI 容器[GlobalCleanup]。
伪代码:
public class BenchmarksDI
{
private IMyInterface _underTest;
private IDependencyContainer _container;
[GlobalSetup]
public void Setup()
{
_container = CallYourCodeThatBuildsDIContainer();
_underTest = _container.Resolve<IMyInterface>();
}
[Benchmark]
public void MethodA() => _underTest.MethodA();
[GlobalCleanup]
public void Cleanup() => _container.Dispose();
}
Run Code Online (Sandbox Code Playgroud)