Added benchmarks project and basic benchmark (#502)
* Added initial benchmarks for DI
This commit is contained in:
Родитель
84df96ddd6
Коммит
831ad485cc
|
@ -27,4 +27,7 @@ nuget.exe
|
|||
project.lock.json
|
||||
.vs
|
||||
.build/
|
||||
.testPublish/
|
||||
.testPublish/
|
||||
BenchmarkDotNet.Artifacts/
|
||||
BDN.Generated/
|
||||
binaries/
|
|
@ -1,7 +1,6 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.26123.0
|
||||
VisualStudioVersion = 15.0.26228.4
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D26F6F80-63EE-4081-A814-EF3DBABE24D9}"
|
||||
EndProject
|
||||
|
@ -21,6 +20,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.DependencyInjection.Specification.Tests", "src\Microsoft.Extensions.DependencyInjection.Specification.Tests\Microsoft.Extensions.DependencyInjection.Specification.Tests.csproj", "{B9395B0F-E8E3-4913-BABF-E71A76F21231}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Extensions.DependencyInjection.Performance", "test\Microsoft.Extensions.DependencyInjection.Performance\Microsoft.Extensions.DependencyInjection.Performance.csproj", "{2AB56E17-8ADC-4763-B298-79BFECC351A8}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -75,6 +76,18 @@ Global
|
|||
{B9395B0F-E8E3-4913-BABF-E71A76F21231}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{B9395B0F-E8E3-4913-BABF-E71A76F21231}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{B9395B0F-E8E3-4913-BABF-E71A76F21231}.Release|x86.Build.0 = Release|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Release|Mixed Platforms.Build.0 = Release|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -84,5 +97,6 @@ Global
|
|||
{20201277-5461-49EF-811B-63ED3CD274EF} = {88B347C1-CCEC-4827-9564-FFF07C9BF7C7}
|
||||
{4E959D3B-2E0D-4296-B22C-D428DED294F0} = {D26F6F80-63EE-4081-A814-EF3DBABE24D9}
|
||||
{B9395B0F-E8E3-4913-BABF-E71A76F21231} = {D26F6F80-63EE-4081-A814-EF3DBABE24D9}
|
||||
{2AB56E17-8ADC-4763-B298-79BFECC351A8} = {88B347C1-CCEC-4827-9564-FFF07C9BF7C7}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
<Project>
|
||||
<ItemGroup>
|
||||
<ExcludeFromTest Include="$(RepositoryRoot)test\Microsoft.Extensions.DependencyInjection.Performance\Microsoft.Extensions.DependencyInjection.Performance.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -0,0 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\..\build\common.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp1.1</TargetFramework>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Microsoft.Extensions.DependencyInjection\Microsoft.Extensions.DependencyInjection.csproj" />
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.10.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection.Performance
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection.Performance
|
||||
{
|
||||
[Config(typeof(CoreConfig))]
|
||||
public class ResolvePerformance
|
||||
{
|
||||
private const int OperationsPerInvoke = 50000;
|
||||
|
||||
private IServiceProvider _transientSp;
|
||||
private IServiceScope _scopedSp;
|
||||
private IServiceProvider _singletonSp;
|
||||
|
||||
[Setup]
|
||||
public void Setup()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddTransient<A>();
|
||||
services.AddTransient<B>();
|
||||
services.AddTransient<C>();
|
||||
_transientSp = services.BuildServiceProvider();
|
||||
|
||||
|
||||
services = new ServiceCollection();
|
||||
services.AddScoped<A>();
|
||||
services.AddScoped<B>();
|
||||
services.AddScoped<C>();
|
||||
_scopedSp = services.BuildServiceProvider().CreateScope();
|
||||
|
||||
|
||||
services = new ServiceCollection();
|
||||
services.AddSingleton<A>();
|
||||
services.AddSingleton<B>();
|
||||
services.AddSingleton<C>();
|
||||
_singletonSp = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true, OperationsPerInvoke = OperationsPerInvoke)]
|
||||
public void NoDI()
|
||||
{
|
||||
for (int i = 0; i < OperationsPerInvoke; i++)
|
||||
{
|
||||
var temp = new A(new B(new C()));
|
||||
temp.Foo();
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
|
||||
public void Transient()
|
||||
{
|
||||
for (int i = 0; i < OperationsPerInvoke; i++)
|
||||
{
|
||||
var temp = _transientSp.GetService<A>();
|
||||
temp.Foo();
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
|
||||
public void Scoped()
|
||||
{
|
||||
for (int i = 0; i < OperationsPerInvoke; i++)
|
||||
{
|
||||
var temp = _scopedSp.ServiceProvider.GetService<A>();
|
||||
temp.Foo();
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
|
||||
public void Singleton()
|
||||
{
|
||||
for (int i = 0; i < OperationsPerInvoke; i++)
|
||||
{
|
||||
var temp = _singletonSp.GetService<A>();
|
||||
temp.Foo();
|
||||
}
|
||||
}
|
||||
|
||||
private class A
|
||||
{
|
||||
public A(B b)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
public void Foo()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private class B
|
||||
{
|
||||
public B(C c)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private class C
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Diagnosers;
|
||||
using BenchmarkDotNet.Engines;
|
||||
using BenchmarkDotNet.Jobs;
|
||||
using BenchmarkDotNet.Validators;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection.Performance
|
||||
{
|
||||
public class CoreConfig : ManualConfig
|
||||
{
|
||||
public CoreConfig()
|
||||
{
|
||||
Add(JitOptimizationsValidator.FailOnError);
|
||||
Add(MemoryDiagnoser.Default);
|
||||
|
||||
Add(Job.Default
|
||||
.With(BenchmarkDotNet.Environments.Runtime.Core)
|
||||
.WithRemoveOutliers(false)
|
||||
.With(RunStrategy.Throughput)
|
||||
.WithLaunchCount(3)
|
||||
.WithWarmupCount(5)
|
||||
.WithTargetCount(10));
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче