Beispiel #1
0
        private static long MeasureProxy(IContainerAdapter container)
        {
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < LoopCount; i++)
            {
                var result = (ICalculator)container.ResolveProxy(typeof(ICalculator));
            }

            return(watch.ElapsedMilliseconds);
        }
Beispiel #2
0
        private static void WarmUp(IContainerAdapter container)
        {
            var interface1 = (ISingleton)container.Resolve(typeof(ISingleton));
            var interface2 = (ITransient)container.Resolve(typeof(ITransient));
            var combined   = (ICombined)container.Resolve(typeof(ICombined));

            if (container.SupportsInterception)
            {
                var calculator = (ICalculator)container.ResolveProxy(typeof(ICalculator));
                calculator.Add(1, 2);
            }
        }
Beispiel #3
0
        private static long MeasureProxy(IContainerAdapter container)
        {
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < LoopCount; i++)
            {
                var result = (ICalculator)container.ResolveProxy(typeof(ICalculator));

                // Call method because part of the time spent with a proxy is how long does it take to execute a proxied method
                result.Add(5, 10);
            }

            return(watch.ElapsedMilliseconds);
        }
Beispiel #4
0
        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.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);
            }
        }
Beispiel #5
0
        private static long MeasureProxy(IContainerAdapter container)
        {
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < LoopCount; i++)
            {
                var result = (ICalculator)container.ResolveProxy(typeof(ICalculator));

                // Call method because part of the time spent with a proxy is how long does it take to execute a proxied method
                result.Add(5, 10);
            }

            return watch.ElapsedMilliseconds;
        }
Beispiel #6
0
        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.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);
            }
        }
Beispiel #7
0
        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()));
                    }
                }
            }
        }
Beispiel #8
0
        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();
            var interceptionTimeWatch = 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();
                }

                if(container.SupportsInterception)
                {
                    interceptionTimeWatch.Start();
                    var result = (ICalculator)container.ResolveProxy(typeof(ICalculator));

                    // Call method because part of the time spent with a proxy is how long does it take to execute a proxied method
                    result.Add(5, 10);
                    interceptionTimeWatch.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);
            }

            if (container.SupportsInterception)
            {
                outputResult.InterceptionTime = interceptionTimeWatch.ElapsedMilliseconds;
            }
        }
Beispiel #9
0
        private static void WarmUp(IContainerAdapter container)
        {
            var interface1 = (ISingleton)container.Resolve(typeof(ISingleton));
            var interface2 = (ITransient)container.Resolve(typeof(ITransient));
            var combined = (ICombined)container.Resolve(typeof(ICombined));

            if (container.SupportsInterception)
            {
                var calculator = (ICalculator)container.ResolveProxy(typeof(ICalculator));
                calculator.Add(1, 2);
            }
        }
Beispiel #10
0
        private static long MeasureProxy(IContainerAdapter container)
        {
            var watch = Stopwatch.StartNew();

            for (int i = 0; i < LoopCount; i++)
            {
                var result = (ICalculator)container.ResolveProxy(typeof(ICalculator));
            }

            return watch.ElapsedMilliseconds;
        }
Beispiel #11
0
        private static void WarmUp(IContainerAdapter container)
        {
            var interface1 = container.Resolve<ISingleton>();
            var interface2 = container.Resolve<ITransient>();
            var combined = container.Resolve<ISingleton>();

            if (container.SupportsInterception)
            {
                var calculator = container.ResolveProxy<ICalculator>();
                calculator.Add(1, 2);
            }
        }
Beispiel #12
0
        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);
            }
        }