From 8f8d3afd3605dfed15127b9af0b8adcecc674890 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Fri, 20 Jul 2018 10:59:43 -0700 Subject: [PATCH] Fix bug in benchmark --- .../MvcEndpointDatasourceBenchmark.cs | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/benchmarks/Microsoft.AspNetCore.Mvc.Performance/MvcEndpointDatasourceBenchmark.cs b/benchmarks/Microsoft.AspNetCore.Mvc.Performance/MvcEndpointDatasourceBenchmark.cs index 982b8d4e22..bb66f15d28 100644 --- a/benchmarks/Microsoft.AspNetCore.Mvc.Performance/MvcEndpointDatasourceBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.Mvc.Performance/MvcEndpointDatasourceBenchmark.cs @@ -4,17 +4,13 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using BenchmarkDotNet.Attributes; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.ActionConstraints; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Routing; -using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.Mvc.Performance { @@ -22,6 +18,11 @@ namespace Microsoft.AspNetCore.Mvc.Performance { private const string DefaultRoute = "{Controller=Home}/{Action=Index}/{id?}"; + // Attribute routes can't have controller and action as parameters, so we edit the + // route template in the test to make it more realistic. + private const string ControllerReplacementToken = "{Controller=Home}"; + private const string ActionReplacementToken = "{Action=Index}"; + private MockActionDescriptorCollectionProvider _conventionalActionProvider; private MockActionDescriptorCollectionProvider _attributeActionProvider; private List _conventionalEndpointInfos; @@ -33,11 +34,11 @@ namespace Microsoft.AspNetCore.Mvc.Performance public void Setup() { _conventionalActionProvider = new MockActionDescriptorCollectionProvider( - Enumerable.Range(0, ActionCount).Select(i => CreateActionDescriptor(i, false)).ToList() + Enumerable.Range(0, ActionCount).Select(i => CreateConventionalRoutedAction(i)).ToList() ); _attributeActionProvider = new MockActionDescriptorCollectionProvider( - Enumerable.Range(0, ActionCount).Select(i => CreateActionDescriptor(i, true)).ToList() + Enumerable.Range(0, ActionCount).Select(i => CreateAttributeRoutedAction(i)).ToList() ); _conventionalEndpointInfos = new List @@ -67,27 +68,40 @@ namespace Microsoft.AspNetCore.Mvc.Performance var endpoints = endpointDataSource.Endpoints; } - private ActionDescriptor CreateActionDescriptor(int id, bool attributeRoute) + private ActionDescriptor CreateAttributeRoutedAction(int id) { - var actionDescriptor = new ActionDescriptor + var routeValues = new Dictionary(StringComparer.OrdinalIgnoreCase) { - RouteValues = new Dictionary + ["Controller"] = "Controller" + id, + ["Action"] = "Index" + }; + + var template = DefaultRoute + .Replace(ControllerReplacementToken, routeValues["Controller"]) + .Replace(ActionReplacementToken, routeValues["Action"]); + + return new ActionDescriptor + { + RouteValues = routeValues, + DisplayName = "Action " + id, + AttributeRouteInfo = new AttributeRouteInfo() + { + Template = template, + } + }; + } + + private ActionDescriptor CreateConventionalRoutedAction(int id) + { + return new ActionDescriptor + { + RouteValues = new Dictionary(StringComparer.OrdinalIgnoreCase) { ["Controller"] = "Controller" + id, ["Action"] = "Index" }, DisplayName = "Action " + id }; - - if (attributeRoute) - { - actionDescriptor.AttributeRouteInfo = new AttributeRouteInfo - { - Template = DefaultRoute - }; - } - - return actionDescriptor; } private MvcEndpointDataSource CreateMvcEndpointDataSource(