diff --git a/benchmark/RobotsTxt.Benchmarks/Benchmarks.cs b/benchmark/RobotsTxt.Benchmarks/Benchmarks.cs index 01ca15e..af5d884 100644 --- a/benchmark/RobotsTxt.Benchmarks/Benchmarks.cs +++ b/benchmark/RobotsTxt.Benchmarks/Benchmarks.cs @@ -12,97 +12,97 @@ using RobotsTxt.Services; #endif -namespace RobotsTxt.Benchmarks { - [Config(typeof(Config))] - public class Benchmarks { - private readonly RobotsTxtOptionsBuilder _optionsBuilder; - private readonly RobotsTxtOptions _options; +namespace RobotsTxt.Benchmarks; + +[Config(typeof(Config))] +public class Benchmarks { + private readonly RobotsTxtOptionsBuilder _optionsBuilder; + private readonly RobotsTxtOptions _options; #if V1 - private readonly RobotsTxtMiddleware _middleware; + private readonly RobotsTxtMiddleware _middleware; #endif #if V2 - private readonly RobotsTxtMiddleware _middleware; - private readonly IRobotsTxtProvider _provider; + private readonly RobotsTxtMiddleware _middleware; + private readonly IRobotsTxtProvider _provider; #endif - public Benchmarks() { - _optionsBuilder = GetNewOptionsBuilder() - .AddSection(section => - section - .AddComment("Allow Googlebot") - .AddUserAgent("Googlebot") - .Allow("/") - ) - .AddSection(section => - section - .AddComment("Allow Bing for most stuff") - .AddUserAgent("Bing") - .Disallow("/bing-should-not-see-this") - .Allow("/") - ) - .AddSection(section => - section - .AddComment("Disallow the rest") - .AddUserAgent("*") - .AddCrawlDelay(TimeSpan.FromSeconds(10)) - .Disallow("/") - ) - .AddSitemap("https://example.com/sitemap.xml"); + public Benchmarks() { + _optionsBuilder = GetNewOptionsBuilder() + .AddSection(section => + section + .AddComment("Allow Googlebot") + .AddUserAgent("Googlebot") + .Allow("/") + ) + .AddSection(section => + section + .AddComment("Allow Bing for most stuff") + .AddUserAgent("Bing") + .Disallow("/bing-should-not-see-this") + .Allow("/") + ) + .AddSection(section => + section + .AddComment("Disallow the rest") + .AddUserAgent("*") + .AddCrawlDelay(TimeSpan.FromSeconds(10)) + .Disallow("/") + ) + .AddSitemap("https://example.com/sitemap.xml"); - _options = _optionsBuilder.Build(); + _options = _optionsBuilder.Build(); #if V1 - _middleware = new RobotsTxtMiddleware(RequestDelegateAsync, _options); + _middleware = new RobotsTxtMiddleware(RequestDelegateAsync, _options); #endif #if V2 - _middleware = new RobotsTxtMiddleware(RequestDelegateAsync); - _provider = new StaticRobotsTxtProvider(_options); + _middleware = new RobotsTxtMiddleware(RequestDelegateAsync); + _provider = new StaticRobotsTxtProvider(_options); #endif - } + } - private static RobotsTxtOptionsBuilder GetNewOptionsBuilder() { + private static RobotsTxtOptionsBuilder GetNewOptionsBuilder() { #if V2 - return new RobotsTxtOptionsBuilder(); + return new RobotsTxtOptionsBuilder(); #else - return (RobotsTxtOptionsBuilder)typeof(RobotsTxtOptionsBuilder).GetConstructor( - BindingFlags.NonPublic | BindingFlags.Instance, - null, Type.EmptyTypes, null).Invoke(null); + return (RobotsTxtOptionsBuilder)typeof(RobotsTxtOptionsBuilder).GetConstructor( + BindingFlags.NonPublic | BindingFlags.Instance, + null, Type.EmptyTypes, null).Invoke(null); #endif - } + } - [Benchmark] - public async Task StaticRobotsTxtProviderAsync() { - for(var i = 0; i < 1000; i++) { - var httpContext = new SimpleHttpContext(); + [Benchmark] + public async Task StaticRobotsTxtProviderAsync() { + for(var i = 0; i < 1000; i++) { + var httpContext = new SimpleHttpContext(); #if V1 - await _middleware.Invoke(httpContext); + await _middleware.Invoke(httpContext); #endif #if V2 - await _middleware.InvokeAsync(httpContext, _provider); + await _middleware.InvokeAsync(httpContext, _provider); #endif - } } + } - public static Task RequestDelegateAsync(HttpContext context) { - return Task.CompletedTask; - } + public static Task RequestDelegateAsync(HttpContext context) { + return Task.CompletedTask; + } - private class Config : ManualConfig { - public Config() { - var baseJob = Job.Default; + private class Config : ManualConfig { + public Config() { + var baseJob = Job.Default; - AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.1.0").WithId("2.1.0").WithCustomBuildConfiguration("V21")); - AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.0.0-preview3").WithId("2.0.0-preview3").WithCustomBuildConfiguration("V2P3")); - AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.0.0-preview2").WithId("2.0.0-preview2").WithCustomBuildConfiguration("V2")); - AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.0.0-preview1").WithId("2.0.0-preview1").WithCustomBuildConfiguration("V2")); - AddJob(baseJob.WithNuGet("RobotsTxtCore", "1.1.0").WithId("1.1.0").WithCustomBuildConfiguration("V1")); + AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.1.0").WithId("2.1.0").WithCustomBuildConfiguration("V21")); + AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.0.0-preview3").WithId("2.0.0-preview3").WithCustomBuildConfiguration("V2P3")); + AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.0.0-preview2").WithId("2.0.0-preview2").WithCustomBuildConfiguration("V2")); + AddJob(baseJob.WithNuGet("RobotsTxtCore", "2.0.0-preview1").WithId("2.0.0-preview1").WithCustomBuildConfiguration("V2")); + AddJob(baseJob.WithNuGet("RobotsTxtCore", "1.1.0").WithId("1.1.0").WithCustomBuildConfiguration("V1")); - AddDiagnoser(MemoryDiagnoser.Default); - AddExporter(MarkdownExporter.GitHub); - } + AddDiagnoser(MemoryDiagnoser.Default); + AddExporter(MarkdownExporter.GitHub); } } } diff --git a/benchmark/RobotsTxt.Benchmarks/Program.cs b/benchmark/RobotsTxt.Benchmarks/Program.cs index c54f23d..9a64a9f 100644 --- a/benchmark/RobotsTxt.Benchmarks/Program.cs +++ b/benchmark/RobotsTxt.Benchmarks/Program.cs @@ -1,7 +1,7 @@ using BenchmarkDotNet.Running; -namespace RobotsTxt.Benchmarks { - public static class Program { - public static void Main() => BenchmarkRunner.Run(); - } +namespace RobotsTxt.Benchmarks; + +public static class Program { + public static void Main() => BenchmarkRunner.Run(); } diff --git a/benchmark/RobotsTxt.Benchmarks/SimpleHttpContext.cs b/benchmark/RobotsTxt.Benchmarks/SimpleHttpContext.cs index 8d5f256..7993dea 100644 --- a/benchmark/RobotsTxt.Benchmarks/SimpleHttpContext.cs +++ b/benchmark/RobotsTxt.Benchmarks/SimpleHttpContext.cs @@ -7,98 +7,99 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; -namespace RobotsTxt.Benchmarks { - // 2021-09-09 KEEP KJ - // To run the benchmarks we need a HttpContext. Constructing a new - // DefaultHttpContext is quite expensive so by faking it with these - // classes lowers the benchmark time with about 50% (or 400 μs). - public class SimpleHttpContext : HttpContext { - private readonly HttpRequest _request; - private readonly HttpResponse _response; - - public SimpleHttpContext() { - _request = new SimpleHttpRequest(); - _response = new SimpleHttpResponse(); - } +namespace RobotsTxt.Benchmarks; + +// 2021-09-09 KEEP KJ +// To run the benchmarks we need a HttpContext. Constructing a new +// DefaultHttpContext is quite expensive so by faking it with these +// classes lowers the benchmark time with about 50% (or 400 μs). +public class SimpleHttpContext : HttpContext { + private readonly HttpRequest _request; + private readonly HttpResponse _response; + + public SimpleHttpContext() { + _request = new SimpleHttpRequest(); + _response = new SimpleHttpResponse(); + } - public override IFeatureCollection Features => throw new NotImplementedException(); + public override IFeatureCollection Features => throw new NotImplementedException(); - public override HttpRequest Request => _request; + public override HttpRequest Request => _request; - public override HttpResponse Response => _response; + public override HttpResponse Response => _response; - public override ConnectionInfo Connection => throw new NotImplementedException(); + public override ConnectionInfo Connection => throw new NotImplementedException(); - public override WebSocketManager WebSockets => throw new NotImplementedException(); + public override WebSocketManager WebSockets => throw new NotImplementedException(); - public override ClaimsPrincipal User { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override IDictionary Items { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override IServiceProvider RequestServices { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override CancellationToken RequestAborted { get => CancellationToken.None; set => throw new NotImplementedException(); } - public override string TraceIdentifier { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override ISession Session { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override ClaimsPrincipal User { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override IDictionary Items { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override IServiceProvider RequestServices { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override CancellationToken RequestAborted { get => CancellationToken.None; set => throw new NotImplementedException(); } + public override string TraceIdentifier { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override ISession Session { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override void Abort() { - throw new NotImplementedException(); - } + public override void Abort() { + throw new NotImplementedException(); } +} - public class SimpleHttpRequest : HttpRequest { - public override HttpContext HttpContext => throw new NotImplementedException(); +public class SimpleHttpRequest : HttpRequest { + public override HttpContext HttpContext => throw new NotImplementedException(); - public override string Method { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override string Scheme { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override bool IsHttps { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override HostString Host { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override PathString PathBase { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override PathString Path { get => new("/robots.txt"); set => throw new NotImplementedException(); } - public override QueryString QueryString { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override IQueryCollection Query { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override string Protocol { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override string Method { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override string Scheme { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override bool IsHttps { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override HostString Host { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override PathString PathBase { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override PathString Path { get => new("/robots.txt"); set => throw new NotImplementedException(); } + public override QueryString QueryString { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override IQueryCollection Query { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override string Protocol { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override IHeaderDictionary Headers => throw new NotImplementedException(); + public override IHeaderDictionary Headers => throw new NotImplementedException(); - public override IRequestCookieCollection Cookies { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override long? ContentLength { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override string ContentType { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override Stream Body { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override IRequestCookieCollection Cookies { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override long? ContentLength { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override string ContentType { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override Stream Body { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override bool HasFormContentType => throw new NotImplementedException(); + public override bool HasFormContentType => throw new NotImplementedException(); - public override IFormCollection Form { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override IFormCollection Form { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override Task ReadFormAsync(CancellationToken cancellationToken = default) { - throw new NotImplementedException(); - } + public override Task ReadFormAsync(CancellationToken cancellationToken = default) { + throw new NotImplementedException(); } +} - public class SimpleHttpResponse : HttpResponse { - private readonly Stream _body = new MemoryStream(); - private string _contentType = string.Empty; +public class SimpleHttpResponse : HttpResponse { + private readonly Stream _body = new MemoryStream(); + private string _contentType = string.Empty; - public SimpleHttpResponse() { - } - public override HttpContext HttpContext => throw new NotImplementedException(); + public SimpleHttpResponse() { + } + + public override HttpContext HttpContext => throw new NotImplementedException(); - public override int StatusCode { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override int StatusCode { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override IHeaderDictionary Headers => new HeaderDictionary(); + public override IHeaderDictionary Headers => new HeaderDictionary(); - public override Stream Body { get => _body; set => throw new NotImplementedException(); } - public override long? ContentLength { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override string ContentType { get => _contentType; set => _contentType = value; } + public override Stream Body { get => _body; set => throw new NotImplementedException(); } + public override long? ContentLength { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public override string ContentType { get => _contentType; set => _contentType = value; } - public override IResponseCookies Cookies => throw new NotImplementedException(); + public override IResponseCookies Cookies => throw new NotImplementedException(); - public override bool HasStarted => throw new NotImplementedException(); + public override bool HasStarted => throw new NotImplementedException(); - public override void OnCompleted(Func callback, object state) { - } + public override void OnCompleted(Func callback, object state) { + } - public override void OnStarting(Func callback, object state) { - } + public override void OnStarting(Func callback, object state) { + } - public override void Redirect(string location, bool permanent) { - } + public override void Redirect(string location, bool permanent) { } } diff --git a/src/RobotsTxt/RobotsTxt.csproj b/src/RobotsTxt/RobotsTxt.csproj index 6a333a9..9f4e738 100644 --- a/src/RobotsTxt/RobotsTxt.csproj +++ b/src/RobotsTxt/RobotsTxt.csproj @@ -2,7 +2,6 @@ Robots.txt middleware for ASP.Net Core Robots.txt middleware with fluent interface. - 2.2.0 net6.0;net7.0;net8.0 latest RobotsTxtCore