|
@@ -2,7 +2,6 @@
|
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
|
|
using System;
|
|
|
-using System.Buffers;
|
|
|
using System.IO.Pipelines;
|
|
|
using System.Threading.Tasks;
|
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
|
|
@@ -10,8 +9,11 @@ using Utf8Json;
|
|
|
|
|
|
namespace PlatformBenchmarks
|
|
|
{
|
|
|
- public class BenchmarkApplication : HttpConnection
|
|
|
+ public partial class BenchmarkApplication
|
|
|
{
|
|
|
+ private readonly static AsciiString _applicationName = "Kestrel Platform-Level Application";
|
|
|
+ public static AsciiString ApplicationName => _applicationName;
|
|
|
+
|
|
|
private readonly static AsciiString _crlf = "\r\n";
|
|
|
private readonly static AsciiString _eoh = "\r\n\r\n"; // End Of Headers
|
|
|
private readonly static AsciiString _http11OK = "HTTP/1.1 200 OK\r\n";
|
|
@@ -21,46 +23,41 @@ namespace PlatformBenchmarks
|
|
|
private readonly static AsciiString _headerContentTypeText = "Content-Type: text/plain\r\n";
|
|
|
private readonly static AsciiString _headerContentTypeJson = "Content-Type: application/json\r\n";
|
|
|
|
|
|
-
|
|
|
private readonly static AsciiString _plainTextBody = "Hello, World!";
|
|
|
|
|
|
- private static class Paths
|
|
|
+ public static class Paths
|
|
|
{
|
|
|
public readonly static AsciiString Plaintext = "/plaintext";
|
|
|
public readonly static AsciiString Json = "/json";
|
|
|
}
|
|
|
|
|
|
- private bool _isPlainText;
|
|
|
- private bool _isJson;
|
|
|
+ private RequestType _requestType;
|
|
|
|
|
|
- public override void OnStartLine(HttpMethod method, HttpVersion version, Span<byte> target, Span<byte> path, Span<byte> query, Span<byte> customMethod, bool pathEncoded)
|
|
|
+ public void OnStartLine(HttpMethod method, HttpVersion version, Span<byte> target, Span<byte> path, Span<byte> query, Span<byte> customMethod, bool pathEncoded)
|
|
|
{
|
|
|
- if (path.StartsWith(Paths.Plaintext) && method == HttpMethod.Get)
|
|
|
- {
|
|
|
- _isPlainText = true;
|
|
|
- }
|
|
|
- else if (path.StartsWith(Paths.Json) && method == HttpMethod.Get)
|
|
|
+ var requestType = RequestType.NotRecognized;
|
|
|
+ if (method == HttpMethod.Get)
|
|
|
{
|
|
|
- _isJson = true;
|
|
|
+ if (Paths.Plaintext.Length <= path.Length && path.StartsWith(Paths.Plaintext))
|
|
|
+ {
|
|
|
+ requestType = RequestType.PlainText;
|
|
|
+ }
|
|
|
+ else if (Paths.Json.Length <= path.Length && path.StartsWith(Paths.Json))
|
|
|
+ {
|
|
|
+ requestType = RequestType.Json;
|
|
|
+ }
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- _isPlainText = false;
|
|
|
- _isJson = false;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- public override void OnHeader(Span<byte> name, Span<byte> value)
|
|
|
- {
|
|
|
+ _requestType = requestType;
|
|
|
}
|
|
|
|
|
|
- public override ValueTask ProcessRequestAsync()
|
|
|
+ public ValueTask ProcessRequestAsync()
|
|
|
{
|
|
|
- if (_isPlainText)
|
|
|
+ if (_requestType == RequestType.PlainText)
|
|
|
{
|
|
|
PlainText(Writer);
|
|
|
}
|
|
|
- else if (_isJson)
|
|
|
+ else if (_requestType == RequestType.Json)
|
|
|
{
|
|
|
Json(Writer);
|
|
|
}
|
|
@@ -72,14 +69,10 @@ namespace PlatformBenchmarks
|
|
|
return default;
|
|
|
}
|
|
|
|
|
|
- public override async ValueTask OnReadCompletedAsync()
|
|
|
- {
|
|
|
- await Writer.FlushAsync();
|
|
|
- }
|
|
|
-
|
|
|
private static void PlainText(PipeWriter pipeWriter)
|
|
|
{
|
|
|
- var writer = new BufferWriter<PipeWriter>(pipeWriter);
|
|
|
+ var writer = GetWriter(pipeWriter);
|
|
|
+
|
|
|
// HTTP 1.1 OK
|
|
|
writer.Write(_http11OK);
|
|
|
|
|
@@ -94,7 +87,7 @@ namespace PlatformBenchmarks
|
|
|
|
|
|
// Content-Length header
|
|
|
writer.Write(_headerContentLength);
|
|
|
- writer.WriteNumeric((ulong)_plainTextBody.Length);
|
|
|
+ writer.WriteNumeric((uint)_plainTextBody.Length);
|
|
|
|
|
|
// End of headers
|
|
|
writer.Write(_eoh);
|
|
@@ -106,7 +99,7 @@ namespace PlatformBenchmarks
|
|
|
|
|
|
private static void Json(PipeWriter pipeWriter)
|
|
|
{
|
|
|
- var writer = new BufferWriter<PipeWriter>(pipeWriter);
|
|
|
+ var writer = GetWriter(pipeWriter);
|
|
|
|
|
|
// HTTP 1.1 OK
|
|
|
writer.Write(_http11OK);
|
|
@@ -123,7 +116,7 @@ namespace PlatformBenchmarks
|
|
|
// Content-Length header
|
|
|
writer.Write(_headerContentLength);
|
|
|
var jsonPayload = JsonSerializer.SerializeUnsafe(new { message = "Hello, World!" });
|
|
|
- writer.WriteNumeric((ulong)jsonPayload.Count);
|
|
|
+ writer.WriteNumeric((uint)jsonPayload.Count);
|
|
|
|
|
|
// End of headers
|
|
|
writer.Write(_eoh);
|
|
@@ -135,7 +128,7 @@ namespace PlatformBenchmarks
|
|
|
|
|
|
private static void Default(PipeWriter pipeWriter)
|
|
|
{
|
|
|
- var writer = new BufferWriter<PipeWriter>(pipeWriter);
|
|
|
+ var writer = GetWriter(pipeWriter);
|
|
|
|
|
|
// HTTP 1.1 OK
|
|
|
writer.Write(_http11OK);
|
|
@@ -153,5 +146,12 @@ namespace PlatformBenchmarks
|
|
|
writer.Write(_crlf);
|
|
|
writer.Commit();
|
|
|
}
|
|
|
+
|
|
|
+ private enum RequestType
|
|
|
+ {
|
|
|
+ NotRecognized,
|
|
|
+ PlainText,
|
|
|
+ Json
|
|
|
+ }
|
|
|
}
|
|
|
}
|