Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LinkGenerationGithubBenchmark.cs « LinkGeneration « Microbenchmarks « perf « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8df2a07ce4345f010b593cfb01d58fa27289767d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

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 Baseline()
    {
        var url = $"/repos/{_lookUpValues["owner"]}/{_lookUpValues["repo"]}/issues/comments/{_lookUpValues["commentId"]}";
        AssertUrl("/repos/aspnet/routing/issues/comments/20202", url);
    }

    [Benchmark]
    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.GetPathByRouteValues(
            _requestContext.HttpContext,
            routeName: null,
            values: _lookUpValues);

        AssertUrl("/repos/aspnet/routing/issues/comments/20202", actualUrl);
    }
}