aspnetcore/test/Microsoft.AspNetCore.Server.../DotSegmentRemovalBenchmark.cs

61 lines
2.4 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 System.Text;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Http;
namespace Microsoft.AspNetCore.Server.Kestrel.Performance
{
[Config(typeof(CoreConfig))]
public class DotSegmentRemovalBenchmark
{
// Immutable
private const string _noDotSegments = "/long/request/target/for/benchmarking/what/else/can/we/put/here";
private const string _singleDotSegments = "/long/./request/./target/./for/./benchmarking/./what/./else/./can/./we/./put/./here";
private const string _doubleDotSegments = "/long/../request/../target/../for/../benchmarking/../what/../else/../can/../we/../put/../here";
private readonly byte[] _noDotSegmentsAscii = Encoding.ASCII.GetBytes(_noDotSegments);
private readonly byte[] _singleDotSegmentsAscii = Encoding.ASCII.GetBytes(_singleDotSegments);
private readonly byte[] _doubleDotSegmentsAscii = Encoding.ASCII.GetBytes(_doubleDotSegments);
private readonly byte[] _noDotSegmentsBytes = new byte[_noDotSegments.Length];
private readonly byte[] _singleDotSegmentsBytes = new byte[_singleDotSegments.Length];
private readonly byte[] _doubleDotSegmentsBytes = new byte[_doubleDotSegments.Length];
[Benchmark(Baseline = true)]
public unsafe int NoDotSegments()
{
_noDotSegmentsAscii.CopyTo(_noDotSegmentsBytes);
fixed (byte* start = _noDotSegmentsBytes)
{
return PathNormalizer.RemoveDotSegments(start, start + _noDotSegments.Length);
}
}
[Benchmark]
public unsafe int SingleDotSegments()
{
_singleDotSegmentsAscii.CopyTo(_singleDotSegmentsBytes);
fixed (byte* start = _singleDotSegmentsBytes)
{
return PathNormalizer.RemoveDotSegments(start, start + _singleDotSegments.Length);
}
}
[Benchmark]
public unsafe int DoubleDotSegments()
{
_doubleDotSegmentsAscii.CopyTo(_doubleDotSegmentsBytes);
fixed (byte* start = _doubleDotSegmentsBytes)
{
return PathNormalizer.RemoveDotSegments(start, start + _doubleDotSegments.Length);
}
}
}
}