From e5c520b4ca65978e7ae88194fbc354f62edfb908 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 6 Sep 2018 12:10:20 +1200 Subject: [PATCH] Add DfaMatcherBuilder benchmarks (#777) --- .../Matching/MatcherAzureBenchmark.cs | 3 +- ...=> MatcherAzureBenchmarkBase.generated.cs} | 10 ++-- .../Matching/MatcherBuilderAzureBenchmark.cs | 31 ++++++++++++ .../Matching/MatcherBuilderGithubBenchmark.cs | 31 ++++++++++++ .../MatcherBuilderMultipleEntryBenchmark.cs | 50 +++++++++++++++++++ .../Matching/MatcherGithubBenchmark.cs | 2 +- ...> MatcherGithubBenchmarkBase.generated.cs} | 10 ++-- tools/Swaggatherer/Template.cs | 8 +-- 8 files changed, 128 insertions(+), 17 deletions(-) rename benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/{MatcherAzureBenchmark.generated.cs => MatcherAzureBenchmarkBase.generated.cs} (99%) create mode 100644 benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderAzureBenchmark.cs create mode 100644 benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderGithubBenchmark.cs create mode 100644 benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderMultipleEntryBenchmark.cs rename benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/{MatcherGithubBenchmark.generated.cs => MatcherGithubBenchmarkBase.generated.cs} (99%) diff --git a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmark.cs b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmark.cs index b68255e3cd..a934316c1f 100644 --- a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmark.cs @@ -3,12 +3,11 @@ using System.Threading.Tasks; using BenchmarkDotNet.Attributes; -using Microsoft.AspNetCore.Http.Features; namespace Microsoft.AspNetCore.Routing.Matching { // Generated from https://github.com/Azure/azure-rest-api-specs - public partial class MatcherAzureBenchmark : EndpointRoutingBenchmarkBase + public class MatcherAzureBenchmark : MatcherAzureBenchmarkBase { private const int SampleCount = 100; diff --git a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmark.generated.cs b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmarkBase.generated.cs similarity index 99% rename from benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmark.generated.cs rename to benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmarkBase.generated.cs index ebb347ea51..1dc30336e3 100644 --- a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmark.generated.cs +++ b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherAzureBenchmarkBase.generated.cs @@ -7,11 +7,11 @@ using Microsoft.AspNetCore.Http; namespace Microsoft.AspNetCore.Routing.Matching { // This code was generated by the Swaggatherer - public partial class MatcherAzureBenchmark : EndpointRoutingBenchmarkBase + public abstract partial class MatcherAzureBenchmarkBase : EndpointRoutingBenchmarkBase { - private const int EndpointCount = 5160; + private protected const int EndpointCount = 5160; - private void SetupEndpoints() + private protected void SetupEndpoints() { Endpoints = new RouteEndpoint[5160]; Endpoints[0] = CreateEndpoint("/account", "GET"); @@ -5176,7 +5176,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Endpoints[5159] = CreateEndpoint("/{tenantID}/groups/{groupObjectId}/$links/members/{memberObjectId}", "DELETE"); } - private void SetupRequests() + private protected void SetupRequests() { Requests = new HttpContext[5160]; Requests[0] = new DefaultHttpContext(); @@ -25821,7 +25821,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Requests[5159].Request.Path = "/67fb987d/groups/04d01a8c-6135/$links/members/71b75dbe-0076-"; } - private Matcher SetupMatcher(MatcherBuilder builder) + private protected Matcher SetupMatcher(MatcherBuilder builder) { builder.AddEndpoint(Endpoints[0]); builder.AddEndpoint(Endpoints[1]); diff --git a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderAzureBenchmark.cs b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderAzureBenchmark.cs new file mode 100644 index 0000000000..a839075c82 --- /dev/null +++ b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderAzureBenchmark.cs @@ -0,0 +1,31 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using BenchmarkDotNet.Attributes; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNetCore.Routing.Matching +{ + // Generated from https://github.com/APIs-guru/openapi-directory + // Use https://editor2.swagger.io/ to convert from yaml to json- + public class MatcherBuilderAzureBenchmark : MatcherAzureBenchmarkBase + { + private IServiceProvider _services; + + [GlobalSetup] + public void Setup() + { + SetupEndpoints(); + + _services = CreateServices(); + } + + [Benchmark] + public void Dfa() + { + var builder = _services.GetRequiredService(); + SetupMatcher(builder); + } + } +} \ No newline at end of file diff --git a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderGithubBenchmark.cs b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderGithubBenchmark.cs new file mode 100644 index 0000000000..4e0244e46e --- /dev/null +++ b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderGithubBenchmark.cs @@ -0,0 +1,31 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using BenchmarkDotNet.Attributes; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNetCore.Routing.Matching +{ + // Generated from https://github.com/APIs-guru/openapi-directory + // Use https://editor2.swagger.io/ to convert from yaml to json- + public class MatcherBuilderGithubBenchmark : MatcherGithubBenchmarkBase + { + private IServiceProvider _services; + + [GlobalSetup] + public void Setup() + { + SetupEndpoints(); + + _services = CreateServices(); + } + + [Benchmark] + public void Dfa() + { + var builder = _services.GetRequiredService(); + SetupMatcher(builder); + } + } +} \ No newline at end of file diff --git a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderMultipleEntryBenchmark.cs b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderMultipleEntryBenchmark.cs new file mode 100644 index 0000000000..002118caca --- /dev/null +++ b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherBuilderMultipleEntryBenchmark.cs @@ -0,0 +1,50 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using BenchmarkDotNet.Attributes; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNetCore.Routing.Matching +{ + public partial class MatcherBuilderMultipleEntryBenchmark : EndpointRoutingBenchmarkBase + { + private IServiceProvider _services; + + [GlobalSetup] + public void Setup() + { + Endpoints = new RouteEndpoint[10]; + Endpoints[0] = CreateEndpoint("/product", "GET"); + Endpoints[1] = CreateEndpoint("/product/{id}", "GET"); + + Endpoints[2] = CreateEndpoint("/account", "GET"); + Endpoints[3] = CreateEndpoint("/account/{id}"); + Endpoints[4] = CreateEndpoint("/account/{id}", "POST"); + Endpoints[5] = CreateEndpoint("/account/{id}", "UPDATE"); + + Endpoints[6] = CreateEndpoint("/v2/account", "GET"); + Endpoints[7] = CreateEndpoint("/v2/account/{id}"); + Endpoints[8] = CreateEndpoint("/v2/account/{id}", "POST"); + Endpoints[9] = CreateEndpoint("/v2/account/{id}", "UPDATE"); + + _services = CreateServices(); + } + + private Matcher SetupMatcher(MatcherBuilder builder) + { + for (int i = 0; i < Endpoints.Length; i++) + { + builder.AddEndpoint(Endpoints[i]); + } + return builder.Build(); + } + + [Benchmark] + public void Dfa() + { + var builder = _services.GetRequiredService(); + SetupMatcher(builder); + } + } +} \ No newline at end of file diff --git a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmark.cs b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmark.cs index 0859fd7a72..3657b42c8b 100644 --- a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmark.cs +++ b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmark.cs @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Routing.Matching { // Generated from https://github.com/APIs-guru/openapi-directory // Use https://editor2.swagger.io/ to convert from yaml to json- - public partial class MatcherGithubBenchmark : EndpointRoutingBenchmarkBase + public class MatcherGithubBenchmark : MatcherGithubBenchmarkBase { private BarebonesMatcher _baseline; private Matcher _dfa; diff --git a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmark.generated.cs b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmarkBase.generated.cs similarity index 99% rename from benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmark.generated.cs rename to benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmarkBase.generated.cs index 01b2edd58f..3ac68c89e0 100644 --- a/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmark.generated.cs +++ b/benchmarks/Microsoft.AspNetCore.Routing.Performance/Matching/MatcherGithubBenchmarkBase.generated.cs @@ -7,11 +7,11 @@ using Microsoft.AspNetCore.Http; namespace Microsoft.AspNetCore.Routing.Matching { // This code was generated by the Swaggatherer - public partial class MatcherGithubBenchmark : EndpointRoutingBenchmarkBase + public partial class MatcherGithubBenchmarkBase : EndpointRoutingBenchmarkBase { - private const int EndpointCount = 243; + private protected const int EndpointCount = 243; - private void SetupEndpoints() + private protected void SetupEndpoints() { Endpoints = new RouteEndpoint[243]; Endpoints[0] = CreateEndpoint("/emojis", "GET"); @@ -259,7 +259,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Endpoints[242] = CreateEndpoint("/repos/{owner}/{repo}/{archive_format}/{path}", "GET"); } - private void SetupRequests() + private protected void SetupRequests() { Requests = new HttpContext[243]; Requests[0] = new DefaultHttpContext(); @@ -1236,7 +1236,7 @@ namespace Microsoft.AspNetCore.Routing.Matching Requests[242].Request.Path = "/repos/21a74/f36c8/805b0492-b723-/680ad"; } - private Matcher SetupMatcher(MatcherBuilder builder) + private protected Matcher SetupMatcher(MatcherBuilder builder) { builder.AddEndpoint(Endpoints[0]); builder.AddEndpoint(Endpoints[1]); diff --git a/tools/Swaggatherer/Template.cs b/tools/Swaggatherer/Template.cs index c5c0c7b42e..8774c6f578 100644 --- a/tools/Swaggatherer/Template.cs +++ b/tools/Swaggatherer/Template.cs @@ -74,21 +74,21 @@ namespace Microsoft.AspNetCore.Routing // This code was generated by the Swaggatherer public partial class GeneratedBenchmark : EndpointRoutingBenchmarkBase {{ - private const int EndpointCount = {3}; + private protected const int EndpointCount = {3}; - private void SetupEndpoints() + private protected void SetupEndpoints() {{ Endpoints = new RouteEndpoint[{3}]; {0} }} - private void SetupRequests() + private protected void SetupRequests() {{ Requests = new HttpContext[{3}]; {1} }} - private Matcher SetupMatcher(MatcherBuilder builder) + private protected Matcher SetupMatcher(MatcherBuilder builder) {{ {2} return builder.Build();