forked from autofac/Autofac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:autofac/Autofac into develop
- Loading branch information
Showing
131 changed files
with
2,820 additions
and
1,491 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
bench/Autofac.Benchmarks/ConcurrencyNestedScopeBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Autofac.Benchmarks | ||
{ | ||
/// <summary> | ||
/// Test the performance of resolving a relatively simple object graph, but within 2 levels of nested lifetime scopes, | ||
/// to simulate a 'per-request' scope model, combined with a unit-of-work inside request. | ||
/// </summary> | ||
public class ConcurrencyNestedScopeBenchmark | ||
{ | ||
private readonly IContainer _container; | ||
|
||
public ConcurrencyNestedScopeBenchmark() | ||
{ | ||
var builder = new ContainerBuilder(); | ||
builder.RegisterType<MockGlobalSingleton>().SingleInstance(); | ||
builder.RegisterType<MockRequestScopeService1>().InstancePerMatchingLifetimeScope("request"); | ||
builder.RegisterType<MockRequestScopeService2>().InstancePerMatchingLifetimeScope("request"); | ||
builder.RegisterType<MockUnitOfWork>().InstancePerLifetimeScope(); | ||
_container = builder.Build(); | ||
} | ||
|
||
[Params(100 /*, 100, 1_000 */)] | ||
public int ConcurrentRequests { get; set; } | ||
|
||
[Params(10)] | ||
public int RepeatCount { get; set; } | ||
|
||
[Benchmark] | ||
public async Task MultipleResolvesOnMultipleTasks() | ||
{ | ||
var tasks = new List<Task>(ConcurrentRequests); | ||
|
||
for (var i = 0; i < ConcurrentRequests; i++) | ||
{ | ||
var task = Task.Run(() => | ||
{ | ||
try | ||
{ | ||
for (var j = 0; j < RepeatCount; j++) | ||
{ | ||
// Start request | ||
using(var requestScope = _container.BeginLifetimeScope("request")) | ||
{ | ||
var service1 = requestScope.Resolve<MockRequestScopeService1>(); | ||
Assert.NotNull(service1); | ||
|
||
using (var unitOfWorkScope = requestScope.BeginLifetimeScope()) | ||
{ | ||
var nestedRequestService2 = unitOfWorkScope.Resolve<MockRequestScopeService2>(); | ||
Assert.NotNull(nestedRequestService2); | ||
var unitOfWork = unitOfWorkScope.Resolve<MockUnitOfWork>(); | ||
Assert.NotNull(unitOfWork); | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.True(false, ex.ToString()); | ||
} | ||
}); | ||
tasks.Add(task); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
} | ||
|
||
internal class MockGlobalSingleton | ||
{ | ||
} | ||
|
||
internal class MockRequestScopeService1 | ||
{ | ||
public MockRequestScopeService1() { } | ||
} | ||
|
||
internal class MockRequestScopeService2 | ||
{ | ||
public MockRequestScopeService2(MockRequestScopeService1 service1, MockGlobalSingleton singleton) { } | ||
} | ||
|
||
internal class MockUnitOfWork | ||
{ | ||
public MockUnitOfWork(MockGlobalSingleton singleton) {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// This software is part of the Autofac IoC container | ||
// Copyright © 2019 Autofac Contributors | ||
// https://autofac.org | ||
// | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the "Software"), to deal in the Software without | ||
// restriction, including without limitation the rights to use, | ||
// copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
// OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
using System.Linq; | ||
using Autofac.Core; | ||
|
||
namespace Autofac.Builder | ||
{ | ||
internal static class BuildCallbackManager | ||
{ | ||
private static readonly TypedService CallbackServiceType = new TypedService(typeof(BuildCallbackService)); | ||
|
||
private const string BuildCallbacksExecutedKey = nameof(BuildCallbacksExecutedKey); | ||
|
||
/// <summary> | ||
/// Executes the newly-registered build callbacks for a given scope/container.. | ||
/// </summary> | ||
/// <param name="scope">The new scope/container.</param> | ||
internal static void RunBuildCallbacks(ILifetimeScope scope) | ||
{ | ||
var buildCallbackServices = scope.ComponentRegistry.RegistrationsFor(CallbackServiceType); | ||
|
||
foreach (var srv in buildCallbackServices) | ||
{ | ||
// Use the metadata to track whether we've executed already, to avoid issuing a resolve request. | ||
if (srv.Metadata.ContainsKey(BuildCallbacksExecutedKey)) | ||
{ | ||
continue; | ||
} | ||
|
||
var request = new ResolveRequest(CallbackServiceType, srv, Enumerable.Empty<Parameter>()); | ||
var component = (BuildCallbackService)scope.ResolveComponent(request); | ||
srv.Metadata[BuildCallbacksExecutedKey] = true; | ||
|
||
// Run the callbacks for the relevant scope. | ||
component.Execute(scope); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.