public override void MethodToBenchmark(IContainerAdapter container) { using (var childContainer = container.CreateChildContainerAdapter()) { childContainer.Prepare(); var scopedCombined = (ICombined1)childContainer.Resolve(typeof(ICombined1)); } using (var childContainer = container.CreateChildContainerAdapter()) { childContainer.Prepare(); var scopedCombined = (ICombined2)childContainer.Resolve(typeof(ICombined2)); } using (var childContainer = container.CreateChildContainerAdapter()) { childContainer.Prepare(); var scopedCombined = (ICombined3)childContainer.Resolve(typeof(ICombined3)); } }
private static void WarmUp(IContainerAdapter container) { var interface1 = (ISingleton)container.Resolve(typeof(ISingleton)); if (interface1 == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ISingleton))); } var interface2 = (ITransient)container.Resolve(typeof(ITransient)); if (interface2 == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ITransient))); } var combined = (ICombined)container.Resolve(typeof(ICombined)); if (combined == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ICombined))); } var complex = (IComplex)container.Resolve(typeof(IComplex)); if (complex == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(IComplex))); } if (container.SupportsPropertyInjection) { var propertyInjectionObject = (ComplexPropertyObject)container.Resolve(typeof(IComplexPropertyObject)); var propertyInjectionObject2 = (ComplexPropertyObject)container.Resolve(typeof(IComplexPropertyObject)); if (propertyInjectionObject == null || propertyInjectionObject2 == null) { throw new Exception( string.Format( "Container {0} could not create type {1}", container.PackageName, typeof(IComplexPropertyObject))); } if (object.ReferenceEquals(propertyInjectionObject, propertyInjectionObject2) || !object.ReferenceEquals(propertyInjectionObject.ServiceA, propertyInjectionObject2.ServiceA) || !object.ReferenceEquals(propertyInjectionObject.ServiceB, propertyInjectionObject2.ServiceB) || !object.ReferenceEquals(propertyInjectionObject.ServiceC, propertyInjectionObject2.ServiceC) || object.ReferenceEquals(propertyInjectionObject.SubObjectA, propertyInjectionObject2.SubObjectA) || object.ReferenceEquals(propertyInjectionObject.SubObjectB, propertyInjectionObject2.SubObjectB) || object.ReferenceEquals(propertyInjectionObject.SubObjectC, propertyInjectionObject2.SubObjectC)) { throw new Exception( string.Format( "Container {0} could not correctly create type {1}", container.PackageName, typeof(IComplexPropertyObject))); } propertyInjectionObject.Verify(container.PackageName); } if (container.SupportGeneric) { var generic = (ImportGeneric<int>)container.Resolve(typeof(ImportGeneric<int>)); if (generic == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportGeneric<int>))); } } if (container.SupportsMultiple) { var importMultiple = (ImportMultiple)container.Resolve(typeof(ImportMultiple)); if (importMultiple == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportMultiple))); } } if (container.SupportsConditional) { var importObject = (ImportConditionObject)container.Resolve(typeof(ImportConditionObject)); if (importObject == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportConditionObject))); } var importObject2 = (ImportConditionObject2)container.Resolve(typeof(ImportConditionObject2)); if (importObject2 == null) { throw new Exception(string.Format("Container {0} could not create type {1}", container.PackageName, typeof(ImportConditionObject2))); } } if (container.SupportsInterception) { var calculator = (ICalculator)container.ResolveProxy(typeof(ICalculator)); calculator.Add(1, 2); } if (container.SupportsChildContainer) { using (var childContainer = container.CreateChildContainerAdapter()) { childContainer.Prepare(); ICombined scopedCombined = (ICombined)childContainer.Resolve(typeof(ICombined)); if (scopedCombined == null) { throw new Exception(string.Format("Child Container {0} could not create type {1}", container.PackageName, typeof(ICombined))); } if (!(scopedCombined is ScopedCombined)) { throw new Exception(string.Format("Child Container {0} resolved type incorrectly should have been {1} but was {2}", container.PackageName, typeof(ICombined), scopedCombined.GetType())); } } } }
private static void MeasureResolvePerformance(IContainerAdapter container, Result outputResult) { var singletonWatch = new Stopwatch(); var transientWatch = new Stopwatch(); var combinedWatch = new Stopwatch(); var conditionsWatch = new Stopwatch(); var genericWatch = new Stopwatch(); var complexWatch = new Stopwatch(); var multipleWatch = new Stopwatch(); var propertyWatch = new Stopwatch(); var childContainerWatch = new Stopwatch(); for (int i = 0; i < LoopCount; i++) { singletonWatch.Start(); var result1 = (ISingleton)container.Resolve(typeof(ISingleton)); singletonWatch.Stop(); transientWatch.Start(); var result2 = (ITransient)container.Resolve(typeof(ITransient)); transientWatch.Stop(); combinedWatch.Start(); var result3 = (ICombined)container.Resolve(typeof(ICombined)); combinedWatch.Stop(); complexWatch.Start(); var complexResult = (IComplex)container.Resolve(typeof(IComplex)); complexWatch.Stop(); if (container.SupportsPropertyInjection) { propertyWatch.Start(); var propertyInjectionResult = (IComplexPropertyObject)container.Resolve(typeof(IComplexPropertyObject)); propertyWatch.Stop(); } if (container.SupportsConditional) { conditionsWatch.Start(); var result4 = (ImportConditionObject)container.Resolve(typeof(ImportConditionObject)); var result5 = (ImportConditionObject2)container.Resolve(typeof(ImportConditionObject2)); conditionsWatch.Stop(); } if (container.SupportGeneric) { genericWatch.Start(); var genericResult = (ImportGeneric<int>)container.Resolve(typeof(ImportGeneric<int>)); genericWatch.Stop(); } if (container.SupportsMultiple) { multipleWatch.Start(); var importMultiple = (ImportMultiple)container.Resolve(typeof(ImportMultiple)); multipleWatch.Stop(); } if (container.SupportsChildContainer && i < ChildContainerLoopCount) { // Note I'm writing the test this way specifically because it matches the Per Request bootstrapper for Nance // It's also a very common pattern used in MVC applications where a scope is created per request childContainerWatch.Start(); using (var childContainer = container.CreateChildContainerAdapter()) { childContainer.Prepare(); var scopedCombined = childContainer.Resolve(typeof(ICombined)); } childContainerWatch.Stop(); } } outputResult.SingletonTime = singletonWatch.ElapsedMilliseconds; outputResult.TransientTime = transientWatch.ElapsedMilliseconds; outputResult.CombinedTime = combinedWatch.ElapsedMilliseconds; outputResult.ComplexTime = complexWatch.ElapsedMilliseconds; if (container.SupportsPropertyInjection) { outputResult.PropertyInjectionTime = propertyWatch.ElapsedMilliseconds; } if (container.SupportGeneric) { outputResult.GenericTime = genericWatch.ElapsedMilliseconds; } if (container.SupportsMultiple) { outputResult.MultipleImport = multipleWatch.ElapsedMilliseconds; } if (container.SupportsConditional) { outputResult.ConditionalTime = conditionsWatch.ElapsedMilliseconds; } if (container.SupportsChildContainer) { outputResult.ChildContainerTime = childContainerWatch.ElapsedMilliseconds * (LoopCount / ChildContainerLoopCount); } }