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

SingleRouteWithConstraintsBenchmark.cs « LinkGeneration « Microbenchmarks « perf « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 925bc5159db7fd8afc7d86afd26745cef2a8e962 (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
// 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 class SingleRouteWithConstraintsBenchmark : EndpointRoutingBenchmarkBase
{
    private TreeRouter _treeRouter;
    private LinkGenerator _linkGenerator;
    private (HttpContext HttpContext, RouteValueDictionary AmbientValues) _requestContext;

    [GlobalSetup]
    public void Setup()
    {
        var template = "Customers/Details/{category}/{region}/{id:int}";
        var defaults = new { controller = "Customers", action = "Details" };
        var requiredValues = new { controller = "Customers", action = "Details" };

        // Endpoint routing related
        SetupEndpoints(CreateEndpoint(template, defaults, requiredValues: requiredValues));
        var services = CreateServices();
        _linkGenerator = services.GetRequiredService<LinkGenerator>();

        // Attribute routing related
        var treeRouteBuilder = services.GetRequiredService<TreeRouteBuilder>();
        CreateOutboundRouteEntry(treeRouteBuilder, Endpoints[0]);
        _treeRouter = treeRouteBuilder.Build();

        _requestContext = CreateCurrentRequestContext();
    }

    [Benchmark(Baseline = true)]
    public void TreeRouter()
    {
        var virtualPathData = _treeRouter.GetVirtualPath(new VirtualPathContext(
            _requestContext.HttpContext,
            ambientValues: _requestContext.AmbientValues,
            values: new RouteValueDictionary(
                new
                {
                    controller = "Customers",
                    action = "Details",
                    category = "Administration",
                    region = "US",
                    id = 10
                })));

        AssertUrl("/Customers/Details/Administration/US/10", virtualPathData?.VirtualPath);
    }

    [Benchmark]
    public void EndpointRouting()
    {
        var actualUrl = _linkGenerator.GetPathByRouteValues(
            _requestContext.HttpContext,
            routeName: null,
            values: new
            {
                controller = "Customers",
                action = "Details",
                category = "Administration",
                region = "US",
                id = 10
            });

        AssertUrl("/Customers/Details/Administration/US/10", actualUrl);
    }
}