Esempio n. 1
0
        public void Should_be_faster_than_activator_create_object()
        {
            var comparison = PerformanceComparison.InTicks(10000, 1000);
            var type       = typeof(EmptyCtor);
            var create     = type.CompileTryCreate();

            comparison.AddCase("Native", () => new EmptyCtor());
            var expressionCase = comparison.AddCase("Compiled expression", () => create());
            var reflectionCase = comparison.AddCase("Reflection", () => Activator.CreateInstance(type));

            comparison.Run();

            expressionCase.Average.ShouldBeLessThan(reflectionCase.Average);
        }
Esempio n. 2
0
        public void Should_be_faster_than_reflection_setting_property_value()
        {
            var comparison   = PerformanceComparison.InTicks(10000, 1000);
            var instance     = new TypeWithProperty();
            var propertyInfo = typeof(TypeWithProperty).GetProperty(
                nameof(TypeWithProperty.Value));
            var setter = propertyInfo.CompileSetter();

            comparison.AddCase("Native", () => instance.Value = "fark");
            var expressionCase = comparison.AddCase("Compiled expression", () => setter(instance, "fark"));
            var reflectionCase = comparison.AddCase("Reflection", () => propertyInfo.SetValue(instance, "fark"));

            comparison.Run();

            expressionCase.Average.ShouldBeLessThan(reflectionCase.Average);
        }
Esempio n. 3
0
        public void Should_invoke_method_faster_than_reflection()
        {
            var type       = new TypeCache().GetTypeDescriptor(typeof(InvokerMethods));
            var method     = type.Methods.First(x => x.Name == nameof(InvokerMethods.MethodWithReturnAndParameters));
            var invoke     = method.GenerateAsyncInvoker(type);
            var instance   = new InvokerMethods();
            var parameters = new object[] { "param1", "param2" };

            var comparison = PerformanceComparison.InTicks(10000, 1000);

            comparison.AddCase("Native", () => instance
                               .MethodWithReturnAndParameters((string)parameters[0], (string)parameters[1]));

            var compiledCase   = comparison.AddCase("Compiled expression", () => invoke(instance, parameters));
            var reflectionCase = comparison.AddCase("Reflection", () => method.MethodInfo.Invoke(instance, parameters));

            comparison.Run();

            compiledCase.Average.ShouldBeLessThan(reflectionCase.Average);
        }
Esempio n. 4
0
        public void Should_set_properties_faster_than_reflection()
        {
            var mapper        = new TestValueMapper(_configuration);
            var mapperContext = new ValueMapperContext(new ActionParameter(_actionMethod,
                                                                           Properties[nameof(Model.Value)]), new object[] { "1,2,3" });
            var method = typeof(TestValueMapper)
                         .GetMethods(BindingFlags.Public | BindingFlags.Instance)
                         .First(x => x.Name == nameof(TestValueMapper.Map) && x.IsGenericMethod)
                         .MakeGenericMethod(mapperContext.Type.Type);

            var comparison = PerformanceComparison.InTicks(10000, 1000);

            comparison.AddCase("Native", () => mapper.Map <int>(mapperContext));
            var compiledCase   = comparison.AddCase("Compiled expression", () => mapper.Map(mapperContext));
            var reflectionCase = comparison.AddCase("Reflection", () => method.Invoke(mapper, new object[] { mapperContext }));

            comparison.Run();

            compiledCase.Average.ShouldBeLessThan(reflectionCase.Average);
        }
Esempio n. 5
0
        public void Performance([Values(Host.Owin, Host.IISExpress)] Host host)
        {
            var guid             = Guid.Parse("6e7335ea-5968-4cf5-84a2-0b8ef560a865");
            var url              = $"performancetests/{{0}}/url1/{guid}/5?query1=query1&query2={guid}&query3=5";
            var urlAsync         = $"performancetests/{{0}}/async/url1/{guid}/5?query1=query1&query2={guid}&query3=5";
            var graphiteUrl      = string.Format(url, "graphite");
            var graphiteAsyncUrl = string.Format(urlAsync, "graphite");
            var webapiUrl        = string.Format(url, "webapi");
            var webapiAsyncUrl   = string.Format(urlAsync, "webapi");
            var inputModel       = new PerfInputModel
            {
                Value1 = "value1",
                Value2 = "value2",
                Value3 = "value3"
            };

            Should_match_result(Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(graphiteUrl, inputModel), guid);
            Should_match_result(Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(webapiUrl, inputModel), guid);
            Should_match_result(Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(graphiteAsyncUrl, inputModel), guid);
            Should_match_result(Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(webapiAsyncUrl, inputModel), guid);

            3.Times(() =>
            {
                Thread.Sleep(5000);
                var comparison = PerformanceComparison.InMilliseconds(100, 20, 40);

                var graphite      = comparison.AddCase("Graphite", () => Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(graphiteUrl, inputModel));
                var graphiteAsync = comparison.AddCase("Graphite Async", () => Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(graphiteAsyncUrl, inputModel));
                var webapi        = comparison.AddCase("Web Api", () => Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(webapiUrl, inputModel));
                var webapiAsync   = comparison.AddCase("Web Api Async", () => Http.ForHost(host).PostJson <PerfInputModel, PerfOutputModel>(webapiAsyncUrl, inputModel));

                comparison.Run();

                File.AppendAllText(@"c:\temp\graphite.txt", $"{graphite.Average},{graphiteAsync.Average},{webapi.Average},{webapiAsync.Average}\r\n");
            });
        }