2021-06-01 05:13:45 +10:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Benchmarks
|
|
|
|
|
{
|
|
|
|
|
[MediumRunJob]
|
|
|
|
|
[MemoryDiagnoser]
|
|
|
|
|
public class TypeLoaderBenchmarks
|
|
|
|
|
{
|
|
|
|
|
private readonly TypeLoader _typeLoader1;
|
|
|
|
|
private readonly TypeLoader _typeLoader2;
|
|
|
|
|
|
|
|
|
|
public TypeLoaderBenchmarks()
|
|
|
|
|
{
|
|
|
|
|
var typeFinder1 = new TypeFinder(
|
|
|
|
|
new NullLogger<TypeFinder>(),
|
2021-07-09 16:15:37 -06:00
|
|
|
new DefaultUmbracoAssemblyProvider(GetType().Assembly, NullLoggerFactory.Instance));
|
2021-06-01 05:13:45 +10:00
|
|
|
|
|
|
|
|
var cache = new ObjectCacheAppCache();
|
|
|
|
|
_typeLoader1 = new TypeLoader(
|
|
|
|
|
typeFinder1,
|
2021-07-09 16:15:37 -06:00
|
|
|
new VaryingRuntimeHash(),
|
2021-06-01 05:13:45 +10:00
|
|
|
cache,
|
|
|
|
|
null,
|
|
|
|
|
new NullLogger<TypeLoader>(),
|
2021-07-09 15:31:01 -06:00
|
|
|
new NoopProfiler());
|
2021-06-01 05:13:45 +10:00
|
|
|
|
|
|
|
|
// populate the cache
|
|
|
|
|
cache.Insert(
|
2021-07-12 09:49:27 -06:00
|
|
|
_typeLoader1.CacheKey,
|
2021-06-01 05:13:45 +10:00
|
|
|
GetCache,
|
|
|
|
|
TimeSpan.FromDays(1));
|
|
|
|
|
|
|
|
|
|
_typeLoader2 = new TypeLoader(
|
|
|
|
|
typeFinder1,
|
2021-07-09 16:15:37 -06:00
|
|
|
new VaryingRuntimeHash(),
|
2021-06-01 05:13:45 +10:00
|
|
|
NoAppCache.Instance,
|
|
|
|
|
null,
|
|
|
|
|
new NullLogger<TypeLoader>(),
|
2021-07-09 15:31:01 -06:00
|
|
|
new NoopProfiler());
|
2021-06-01 05:13:45 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Use a predefined cache of types (names) - this is an example of what is saved to disk and loaded on startup
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private Dictionary<(string, string), IEnumerable<string>> GetCache()
|
|
|
|
|
=> new Dictionary<(string, string), IEnumerable<string>>
|
|
|
|
|
{
|
|
|
|
|
[(typeof(ITypeLoaderBenchmarkPlugin).FullName, null)] = new List<string>
|
|
|
|
|
{
|
|
|
|
|
typeof(CustomAssemblyProvider1).FullName,
|
|
|
|
|
typeof(CustomAssemblyProvider2).FullName,
|
|
|
|
|
typeof(CustomAssemblyProvider3).FullName
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[Benchmark(Baseline = true)]
|
|
|
|
|
public void WithTypesCache()
|
|
|
|
|
{
|
|
|
|
|
var found = _typeLoader1.GetTypes<ITypeLoaderBenchmarkPlugin>().Count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
|
public void WithoutTypesCache()
|
|
|
|
|
{
|
|
|
|
|
var found = _typeLoader2.GetTypes<ITypeLoaderBenchmarkPlugin>(false).Count();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// These are the types we'll find for the benchmark
|
|
|
|
|
|
|
|
|
|
public interface ITypeLoaderBenchmarkPlugin
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CustomAssemblyProvider1 : ITypeLoaderBenchmarkPlugin
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CustomAssemblyProvider2 : ITypeLoaderBenchmarkPlugin
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CustomAssemblyProvider3 : ITypeLoaderBenchmarkPlugin
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|