Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
karl-sjogren committed Oct 1, 2023
1 parent ac3933c commit 321e8b7
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 135 deletions.
126 changes: 63 additions & 63 deletions benchmark/RobotsTxt.Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
8 changes: 4 additions & 4 deletions benchmark/RobotsTxt.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BenchmarkDotNet.Running;

namespace RobotsTxt.Benchmarks {
public static class Program {
public static void Main() => BenchmarkRunner.Run<Benchmarks>();
}
namespace RobotsTxt.Benchmarks;

public static class Program {
public static void Main() => BenchmarkRunner.Run<Benchmarks>();
}
135 changes: 68 additions & 67 deletions benchmark/RobotsTxt.Benchmarks/SimpleHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object, object> 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<object, object> 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<IFormCollection> ReadFormAsync(CancellationToken cancellationToken = default) {
throw new NotImplementedException();
}
public override Task<IFormCollection> 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<object, Task> callback, object state) {
}
public override void OnCompleted(Func<object, Task> callback, object state) {
}

public override void OnStarting(Func<object, Task> callback, object state) {
}
public override void OnStarting(Func<object, Task> callback, object state) {
}

public override void Redirect(string location, bool permanent) {
}
public override void Redirect(string location, bool permanent) {
}
}
1 change: 0 additions & 1 deletion src/RobotsTxt/RobotsTxt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<PropertyGroup>
<Title>Robots.txt middleware for ASP.Net Core</Title>
<Description>Robots.txt middleware with fluent interface.</Description>
<VersionPrefix>2.2.0</VersionPrefix>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AssemblyName>RobotsTxtCore</AssemblyName>
Expand Down

0 comments on commit 321e8b7

Please sign in to comment.