68 lines
2.6 KiB
C#
68 lines
2.6 KiB
C#
// 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 BenchmarkDotNet.Attributes;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Routing.Tree;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Microsoft.AspNetCore.Routing.LinkGeneration
|
|
{
|
|
public partial class LinkGenerationGithubBenchmark
|
|
{
|
|
private LinkGenerator _linkGenerator;
|
|
private TreeRouter _treeRouter;
|
|
private (HttpContext HttpContext, RouteValueDictionary AmbientValues) _requestContext;
|
|
private RouteValueDictionary _lookUpValues;
|
|
|
|
[GlobalSetup]
|
|
public void Setup()
|
|
{
|
|
SetupEndpoints();
|
|
|
|
var services = CreateServices();
|
|
_linkGenerator = services.GetRequiredService<LinkGenerator>();
|
|
|
|
// Attribute routing related
|
|
var treeRouteBuilder = services.GetRequiredService<TreeRouteBuilder>();
|
|
foreach (var endpoint in Endpoints)
|
|
{
|
|
CreateOutboundRouteEntry(treeRouteBuilder, endpoint);
|
|
}
|
|
_treeRouter = treeRouteBuilder.Build();
|
|
|
|
_requestContext = CreateCurrentRequestContext();
|
|
|
|
// Get the endpoint to test and pre-populate the lookup values with the defaults
|
|
// (as they are dynamically generated) and update with other required parameter values.
|
|
// /repos/{owner}/{repo}/issues/comments/{commentId}
|
|
var endpointToTest = Endpoints[176];
|
|
_lookUpValues = new RouteValueDictionary(endpointToTest.RoutePattern.Defaults);
|
|
_lookUpValues["owner"] = "aspnet";
|
|
_lookUpValues["repo"] = "routing";
|
|
_lookUpValues["commentId"] = "20202";
|
|
}
|
|
|
|
[Benchmark(Baseline = true)]
|
|
public void TreeRouter()
|
|
{
|
|
var virtualPathData = _treeRouter.GetVirtualPath(new VirtualPathContext(
|
|
_requestContext.HttpContext,
|
|
ambientValues: _requestContext.AmbientValues,
|
|
values: new RouteValueDictionary(_lookUpValues)));
|
|
|
|
AssertUrl("/repos/aspnet/routing/issues/comments/20202", virtualPathData?.VirtualPath);
|
|
}
|
|
|
|
[Benchmark]
|
|
public void EndpointRouting()
|
|
{
|
|
var actualUrl = _linkGenerator.GetLink(
|
|
_requestContext.HttpContext,
|
|
values: new RouteValueDictionary(_lookUpValues));
|
|
|
|
AssertUrl("/repos/aspnet/routing/issues/comments/20202", actualUrl);
|
|
}
|
|
}
|
|
}
|