aspnetcore/benchmarks/Microsoft.AspNetCore.Routin.../Matching/MatcherFindCandidateSetAzur...

75 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 System;
using BenchmarkDotNet.Attributes;
namespace Microsoft.AspNetCore.Routing.Matching
{
// Generated from https://github.com/Azure/azure-rest-api-specs
public partial class MatcherFindCandidateSetAzureBenchmark : EndpointRoutingBenchmarkBase
{
// SegmentCount should be max-segments + 1, but we don't have a good way to compute
// it here, so using 16 as a safe guess.
private const int SegmentCount = 16;
private const int SampleCount = 100;
private BarebonesMatcher _baseline;
private DfaMatcher _dfa;
private int[] _samples;
[GlobalSetup]
public void Setup()
{
SetupEndpoints();
SetupRequests();
// The perf is kinda slow for these benchmarks, so we do some sampling
// of the request data.
_samples = SampleRequests(EndpointCount, SampleCount);
_baseline = (BarebonesMatcher)SetupMatcher(new BarebonesMatcherBuilder());
_dfa = (DfaMatcher)SetupMatcher(CreateDfaMatcherBuilder());
}
[Benchmark(Baseline = true, OperationsPerInvoke = SampleCount)]
public void Baseline()
{
for (var i = 0; i < SampleCount; i++)
{
var sample = _samples[i];
var httpContext = Requests[sample];
var path = httpContext.Request.Path.Value;
var segments = new ReadOnlySpan<PathSegment>(Array.Empty<PathSegment>());
var candidates = _baseline.Matchers[sample].FindCandidateSet(path, segments);
var endpoint = candidates[0].Endpoint;
Validate(httpContext, Endpoints[sample], endpoint);
}
}
[Benchmark(OperationsPerInvoke = SampleCount)]
public void Dfa()
{
for (var i = 0; i < SampleCount; i++)
{
var sample = _samples[i];
var httpContext = Requests[sample];
var path = httpContext.Request.Path.Value;
Span<PathSegment> segments = stackalloc PathSegment[SegmentCount];
var count = FastPathTokenizer.Tokenize(path, segments);
var candidates = _dfa.FindCandidateSet(httpContext, path, segments.Slice(0, count));
var endpoint = candidates[0].Endpoint;
Validate(httpContext, Endpoints[sample], endpoint);
}
}
}
}