Skip to content

Commit

Permalink
Flush all at once instead of per call
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Sep 3, 2023
1 parent afc4039 commit 8875dcc
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/HyperSharp/Protocol/HyperContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Globalization;
using Microsoft.Toolkit.HighPerformance;

namespace HyperSharp.Protocol
{
Expand All @@ -36,7 +37,7 @@ public class HyperContext
#else
.ToImmutableDictionary();
#endif
private static readonly ReadOnlyMemory<byte> _newLine = new("\r\n"u8.ToArray());
private static readonly byte[] _newLine = "\r\n"u8.ToArray();
private static readonly byte[] _emptyBody = Array.Empty<byte>();

/// <summary>
Expand Down Expand Up @@ -116,9 +117,9 @@ public virtual async Task RespondAsync(HyperStatus status, JsonSerializerOptions
// TODO: Find a solution which allows modification of the body (Gzip) and the base stream (SSL).

// Write request line
await Connection.StreamWriter.WriteAsync(_httpVersions[Version], cancellationToken);
await Connection.StreamWriter.WriteAsync(Encoding.ASCII.GetBytes($"{(int)status.Code} {status.Code}"), cancellationToken);
await Connection.StreamWriter.WriteAsync(_newLine, cancellationToken);
Connection.StreamWriter.Write<byte>(_httpVersions[Version]);
Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes($"{(int)status.Code} {status.Code}"));
Connection.StreamWriter.Write<byte>(_newLine);

// Serialize body ahead of time due to headers
byte[] content = status.Body is null
Expand All @@ -133,20 +134,21 @@ public virtual async Task RespondAsync(HyperStatus status, JsonSerializerOptions

foreach ((string headerName, byte[] value) in status.Headers)
{
await Connection.StreamWriter.WriteAsync(Encoding.ASCII.GetBytes(headerName), cancellationToken);
await Connection.StreamWriter.WriteAsync(": "u8.ToArray(), cancellationToken);
await Connection.StreamWriter.WriteAsync(value, cancellationToken);
await Connection.StreamWriter.WriteAsync(_newLine, cancellationToken);
Connection.StreamWriter.Write<byte>(Encoding.ASCII.GetBytes(headerName));
Connection.StreamWriter.Write<byte>(": "u8.ToArray());
Connection.StreamWriter.Write<byte>(value);
Connection.StreamWriter.Write<byte>(_newLine);
}
await Connection.StreamWriter.WriteAsync(_newLine, cancellationToken);
Connection.StreamWriter.Write<byte>(_newLine);

// Write body
if (content.Length != 0)
{
await Connection.StreamWriter.WriteAsync(content, cancellationToken);
Connection.StreamWriter.Write<byte>(content);
}

HasResponded = true;
await Connection.StreamWriter.FlushAsync(cancellationToken);
}

/// <inheritdoc/>
Expand Down

1 comment on commit 8875dcc

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Machine Information:

BenchmarkDotNet v0.13.7, Void Linux

  • AMD EPYC 7282, 1 CPU, 4 logical and 4 physical cores
  • Hardware Intrinsics: AVX2, AES, BMI1, BMI2, FMA, LZCNT, PCLMUL, POPCNT VectorSize=256
  • .NET 8.0.0 (8.0.23.37506), x64, RyuJIT
  • Total Execution Time: 16m and 45.834s

HttpBenchmarks

Execution Time: 52.857s

HttpClientTestAsync:

Mean: 440.94μs
Error: 6.89μs
StdDev: 67.48μs
Max per second: 2,267.88 (1,000,000,000ns / 440,940.56ns)

ParseHeadersTestAsync:

Mean: 10.26μs
Error: 245ns
StdDev: 2.39μs
Max per second: 97,430.05 (1,000,000,000ns / 10,263.77ns)

AsyncResponderBenchmarks

Execution Time: 4m and 37.753s

DelegateExecutionTimeAsync:

Mean: 67ns
Error: 0ns
StdDev: 3ns
Max per second: 14,851,803.59 (1,000,000,000ns / 67.33ns)

DelegateExecutionTimeAsync:

Mean: 217ns
Error: 1ns
StdDev: 9ns
Max per second: 4,614,353.01 (1,000,000,000ns / 216.72ns)

DelegateExecutionTimeAsync:

Mean: 160ns
Error: 1ns
StdDev: 7ns
Max per second: 6,252,345.61 (1,000,000,000ns / 159.94ns)

DelegateExecutionTimeAsync:

Mean: 213ns
Error: 1ns
StdDev: 9ns
Max per second: 4,697,455.91 (1,000,000,000ns / 212.88ns)

ErrorBenchmarks

Execution Time: 3m and 31.753s

CreateError, Baseline:

Mean: 12ns
Error: 0ns
StdDev: 0ns
Max per second: 83,230,891.56 (1,000,000,000ns / 12.01ns)

CreateErrorWithMessage:

Mean: 11ns
Error: 0ns
StdDev: 1ns
Max per second: 91,897,745.24 (1,000,000,000ns / 10.88ns)

CreateErrorWithSubError:

Mean: 25ns
Error: 0ns
StdDev: 1ns
Max per second: 39,426,173.07 (1,000,000,000ns / 25.36ns)

CreateErrorWithSubErrors:

Mean: 77ns
Error: 0ns
StdDev: 3ns
Max per second: 12,968,100.18 (1,000,000,000ns / 77.11ns)

GenericResultBenchmarks

Execution Time: 3m and 57.121s

CreateGenericSuccessfulResult, Baseline:

Mean: 6ns
Error: 0ns
StdDev: 0ns
Max per second: 155,557,638.16 (1,000,000,000ns / 6.43ns)

CreateGenericFailedResult:

Mean: 22ns
Error: 0ns
StdDev: 1ns
Max per second: 45,290,939.41 (1,000,000,000ns / 22.08ns)

CreateGenericFailedResultWithMultipleErrors:

Mean: 62ns
Error: 0ns
StdDev: 2ns
Max per second: 16,078,510.61 (1,000,000,000ns / 62.19ns)

CreateGenericFailedResultWithValue:

Mean: 13ns
Error: 0ns
StdDev: 0ns
Max per second: 75,747,972.23 (1,000,000,000ns / 13.20ns)

CreateGenericSuccessfulResultWithValue:

Mean: 6ns
Error: 0ns
StdDev: 0ns
Max per second: 158,205,421.65 (1,000,000,000ns / 6.32ns)

ResponderBenchmarks

Execution Time: 3m and 38.921s

DelegateExecutionTime:

Mean: 33ns
Error: 0ns
StdDev: 1ns
Max per second: 30,035,369.30 (1,000,000,000ns / 33.29ns)

DelegateExecutionTime:

Mean: 50ns
Error: 0ns
StdDev: 1ns
Max per second: 20,134,383.13 (1,000,000,000ns / 49.67ns)

DelegateExecutionTime:

Mean: 59ns
Error: 0ns
StdDev: 3ns
Max per second: 16,881,022.52 (1,000,000,000ns / 59.24ns)

DelegateExecutionTime:

Mean: 76ns
Error: 0ns
StdDev: 3ns
Max per second: 13,158,265.94 (1,000,000,000ns / 76.00ns)

DelegateExecutionTime:

Mean: 62ns
Error: 0ns
StdDev: 1ns
Max per second: 16,251,801.62 (1,000,000,000ns / 61.53ns)

ResultBenchmarks

Execution Time: 3m and 7.430s

CreateSuccessfulResult, Baseline:

Mean: 0ns
Error: 0ns
StdDev: 0ns
Max per second: 16,891,842,763.02 (1,000,000,000ns / 0.06ns)

CreateFailedResult:

Mean: 28ns
Error: 0ns
StdDev: 1ns
Max per second: 36,241,817.61 (1,000,000,000ns / 27.59ns)

CreateFailedResultWithMultipleErrors:

Mean: 64ns
Error: 0ns
StdDev: 2ns
Max per second: 15,730,601.48 (1,000,000,000ns / 63.57ns)

CreateFailedResultWithValue:

Mean: 18ns
Error: 0ns
StdDev: 1ns
Max per second: 56,944,037.82 (1,000,000,000ns / 17.56ns)

CreateSuccessfulResultWithValue:

Mean: 3ns
Error: 0ns
StdDev: 0ns
Max per second: 290,104,968.11 (1,000,000,000ns / 3.45ns)

Please sign in to comment.