Bad http/3 performance in net7 preview1
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
Build and run below programs with net7 preview 1, http3 version is ~10x slower than http2 version
Benchmark command: `hyperfine "./app 3000"`
result with http2:
```
Time (mean ± σ): 565.4 ms ± 31.9 ms [User: 745.1 ms, System: 169.0 ms]
Range (min … max): 501.2 ms … 625.2 ms 10 runs
```
result with http3:
```
Time (mean ± σ): 5.638 s ± 0.846 s [User: 10.635 s, System: 8.082 s]
Range (min … max): 4.191 s … 6.623 s 10 runs
```
### Expected Behavior
http3 version of the programs should not be ~10x slower than http2 version
### Steps To Reproduce
Build command: `dotnet publish -c Release -r linux-x64 -f net7 --self-contained true -p:PublishSingleFile=true`
OS: wsl2 ubuntu 20.04 (on win11)
libmsquic version: 1.9.0
code(http2)
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Logging;
static class Program
{
private static readonly HttpClient s_client = new HttpClient(new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true,
})
{
Timeout = TimeSpan.FromSeconds(1),
DefaultRequestVersion = HttpVersion.Version20,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher,
};
static Program()
{
ServicePointManager.ReusePort = true;
// https://docs.microsoft.com/en-US/troubleshoot/aspnet/performance-call-web-service
ServicePointManager.DefaultConnectionLimit = 12 * Environment.ProcessorCount;
// https://blogs.msdn.microsoft.com/windowsazurestorage/2010/06/25/nagles-algorithm-is-not-friendly-towards-small-requests/
ServicePointManager.UseNagleAlgorithm = false;
}
public static async Task Main(string[] args)
{
int n;
if (args.Length < 1 || !int.TryParse(args[0], out n))
{
n = 10;
}
var port = 30000 + new Random().Next(10000);
var app = CreateWebApplication(port);
app.MapPost("/", async ctx =>
{
using var sr = new StreamReader(ctx.Request.Body);
var bodyText = await sr.ReadToEndAsync().ConfigureAwait(false);
var payload = JsonSerializer.Deserialize(bodyText);
ctx.Response.StatusCode = 200;
await ctx.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(payload.Value.ToString())).ConfigureAwait(false);
});
using var serverTask = app.RunAsync();
var sum = 0;
var api = $"https://localhost:{port}/";
var tasks = new List>(n);
for (var i = 1; i <= n; i++)
{
tasks.Add(SendAsync(api, i));
}
foreach (var task in tasks)
{
sum += await task.ConfigureAwait(false);
}
Console.WriteLine(sum);
Environment.Exit(0);
}
private static async Task SendAsync(string api, int value)
{
var payload = JsonSerializer.Serialize(new Payload { Value = value });
while (true)
{
try
{
var content = new StringContent(payload, Encoding.UTF8);
var response = await s_client.PostAsync(api, content).ConfigureAwait(false);
return int.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
catch (Exception e)
{
#if DEBUG
Console.Error.WriteLine(e);
#endif
}
}
}
private static WebApplication CreateWebApplication(int port)
{
var builder = WebApplication.CreateBuilder();
builder.WebHost.ConfigureLogging((context, logging) =>
{
logging.ClearProviders();
}).UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
options.ListenLocalhost(port, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
listenOptions.UseHttps();
});
});
return builder.Build();
}
}
public struct Payload
{
[JsonPropertyName("value")]
public int Value { get; set; }
}
```
code(http3)
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Logging;
static class Program
{
private static readonly HttpClient s_client = new HttpClient(new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (_, _, _, _) => true,
})
{
Timeout = TimeSpan.FromSeconds(1),
DefaultRequestVersion = HttpVersion.Version30,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher,
};
static Program()
{
ServicePointManager.ReusePort = true;
// https://docs.microsoft.com/en-US/troubleshoot/aspnet/performance-call-web-service
ServicePointManager.DefaultConnectionLimit = 12 * Environment.ProcessorCount;
// https://blogs.msdn.microsoft.com/windowsazurestorage/2010/06/25/nagles-algorithm-is-not-friendly-towards-small-requests/
ServicePointManager.UseNagleAlgorithm = false;
}
public static async Task Main(string[] args)
{
int n;
if (args.Length < 1 || !int.TryParse(args[0], out n))
{
n = 10;
}
var port = 30000 + new Random().Next(10000);
var app = CreateWebApplication(port);
app.MapPost("/", async ctx =>
{
using var sr = new StreamReader(ctx.Request.Body);
var bodyText = await sr.ReadToEndAsync().ConfigureAwait(false);
var payload = JsonSerializer.Deserialize(bodyText);
ctx.Response.StatusCode = 200;
await ctx.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(payload.Value.ToString())).ConfigureAwait(false);
});
using var serverTask = app.RunAsync();
var sum = 0;
var api = $"https://localhost:{port}/";
var tasks = new List>(n);
for (var i = 1; i <= n; i++)
{
tasks.Add(SendAsync(api, i));
}
foreach (var task in tasks)
{
sum += await task.ConfigureAwait(false);
}
Console.WriteLine(sum);
Environment.Exit(0);
}
private static async Task SendAsync(string api, int value)
{
var payload = JsonSerializer.Serialize(new Payload { Value = value });
while (true)
{
try
{
var content = new StringContent(payload, Encoding.UTF8);
var response = await s_client.PostAsync(api, content).ConfigureAwait(false);
return int.Parse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
catch (Exception e)
{
#if DEBUG
Console.Error.WriteLine(e);
#endif
}
}
}
private static WebApplication CreateWebApplication(int port)
{
var builder = WebApplication.CreateBuilder();
builder.WebHost.ConfigureLogging((context, logging) =>
{
logging.ClearProviders();
}).UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
options.ListenLocalhost(port, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
listenOptions.UseHttps();
});
});
return builder.Build();
}
}
public struct Payload
{
[JsonPropertyName("value")]
public int Value { get; set; }
}
```
app.csproj
```xml
net7
Exe
latest
true
true
true
true
```
### Exceptions (if any)
_No response_
### .NET Version
7.0.100-preview.1.22110.4
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.