Created
October 26, 2019 10:44
-
-
Save alistairjevans/ab67db8be96cedc2d294118e09ed1714 to your computer and use it in GitHub Desktop.
TupleDictionary Benchmark
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
public class GenericTupleDictionaryBenchmark | |
{ | |
[Params(100, 500, 1000, 2000, 5000, 10000)] | |
public int DictSize { get; set; } | |
private (Type open, Type closed)[] allTestTypes = new[] | |
{ | |
(typeof(Service1<>), typeof(Service1<string>)), | |
(typeof(Service2<>), typeof(Service2<ContainerBuilder>)), | |
(typeof(Service3<>), typeof(Service3<int>)), | |
(typeof(Service4<>), typeof(Service4<Type>)), | |
(typeof(Service5<>), typeof(Service5<Tuple<int, int>>)), | |
(typeof(Service6<>), typeof(Service6<(int, int)>)), | |
(typeof(Service7<>), typeof(Service7<IContainer>)), | |
(typeof(Service8<>), typeof(Service8<bool>)), | |
(typeof(Service9<>), typeof(Service9<string>)), | |
(typeof(Service10<>), typeof(Service10<string>)), | |
(typeof(Service11<>), typeof(Service11<decimal>)), | |
(typeof(Service12<>), typeof(Service12<bool?>)), | |
(typeof(Service13<>), typeof(Service13<GenericTupleDictionaryBenchmark>)), | |
(typeof(Service14<>), typeof(Service14<StringBuilder>)), | |
(typeof(Service15<>), typeof(Service15<IDisposable>)), | |
(typeof(Service16<>), typeof(Service16<Dictionary<Type, Type>>)) | |
}; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
var allTypes = AssemblyLoadContext.Default.Assemblies.SelectMany(x => x.GetTypes().Where(x => x.IsVisible)).ToArray(); | |
// Get all variants of service. | |
var allServiceProviders = Assembly.GetExecutingAssembly().GetTypes() | |
.Where(x => typeof(Service<>).IsAssignableFrom(x)).ToArray(); | |
// Warm up the cache. | |
for(int idx = 0; idx < DictSize; idx++) | |
{ | |
var serviceProvider = allServiceProviders[idx % allServiceProviders.Length]; | |
var backingType = allTypes[idx % allTypes.Length]; | |
if (!backingType.IsValueType) | |
{ | |
// Generate the concrete implementation. | |
var concreteImpl = serviceProvider.MakeGenericType(backingType); | |
concreteImpl.IsGenericTypeDefinedBy(serviceProvider); | |
concreteImpl.IsGenericTypeDefinedByNew(serviceProvider); | |
} | |
} | |
} | |
[Benchmark] | |
public bool IsGenericTypeDefinedBy() | |
{ | |
return CheckAll((open, closed) => closed.IsGenericTypeDefinedBy(open)); | |
} | |
[Benchmark] | |
public bool IsGenericTypeDefinedByNew() | |
{ | |
return CheckAll((open, closed) => closed.IsGenericTypeDefinedByNew(open)); | |
} | |
private bool CheckAll(Func<Type, Type, bool> execution) | |
{ | |
var result = true; | |
foreach(var inputs in allTestTypes) | |
{ | |
result &= execution(inputs.open, inputs.closed); | |
} | |
return result; | |
} | |
public class Service<T> {} | |
public class Service1<T> : Service<T> {} | |
public class Service2<T> : Service<T> { } | |
public class Service3<T> : Service<T> { } | |
public class Service4<T> : Service<T> { } | |
public class Service5<T> : Service<T> { } | |
public class Service6<T> : Service<T> { } | |
public class Service7<T> : Service<T> { } | |
public class Service8<T> : Service<T> { } | |
public class Service9<T> : Service<T> { } | |
public class Service10<T> : Service<T> { } | |
public class Service11<T> : Service<T> { } | |
public class Service12<T> : Service<T> { } | |
public class Service13<T> : Service<T> { } | |
public class Service14<T> : Service<T> { } | |
public class Service15<T> : Service<T> { } | |
public class Service16<T> : Service<T> { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment