Skip to content

Faster C# http-server implementation #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions bench/algorithm/http-server/1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Reflection;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -63,14 +64,13 @@ public static async Task Main(string[] args)
private static async Task<int> SendAsync(string api, int value)
{
// await Task.Yield();
var payload = JsonSerializer.Serialize(new Payload { Value = value });
var payload = 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));
var response = await s_client.PostAsJsonAsync(api, payload).ConfigureAwait(false);
return int.Parse(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
}
catch { }
}
Expand Down Expand Up @@ -102,11 +102,8 @@ private static IHostBuilder CreateWebHostBuilder(int port) =>
public sealed class MyController : Controller
{
[Route("/")]
public async Task<int> PostAsync()
public int Post([FromBody] Payload payload)
{
using var sr = new StreamReader(Request.Body);
var bodyText = await sr.ReadToEndAsync().ConfigureAwait(false);
var payload = JsonSerializer.Deserialize<Payload>(bodyText);
return payload.Value;
}
}
Expand Down
32 changes: 22 additions & 10 deletions bench/algorithm/http-server/2-http2.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
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.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -43,13 +47,22 @@ public static async Task Main(string[] args)

var port = 30000 + new Random().Next(10000);
var app = CreateWebApplication(port);
app.MapPost("/", async ctx =>
app.MapPost("/", async (HttpResponse response, [FromBody] Payload payload) =>
{
using var sr = new StreamReader(ctx.Request.Body);
var bodyText = await sr.ReadToEndAsync().ConfigureAwait(false);
var payload = JsonSerializer.Deserialize<Payload>(bodyText);
ctx.Response.StatusCode = 200;
await ctx.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(payload.Value.ToString())).ConfigureAwait(false);

if (payload.Value.TryFormat(response.BodyWriter.GetSpan(16), out n))
{
response.StatusCode = StatusCodes.Status200OK;

response.BodyWriter.Advance(n);
await response.BodyWriter.FlushAsync().ConfigureAwait(false);
}
else
{
throw new UnreachableException();
}


});

using var serverTask = app.RunAsync();
Expand All @@ -70,14 +83,13 @@ public static async Task Main(string[] args)

private static async Task<int> SendAsync(string api, int value)
{
var payload = JsonSerializer.Serialize(new Payload { Value = value });
var payload = 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));
var response = await s_client.PostAsJsonAsync(api, payload).ConfigureAwait(false);
return int.Parse(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
}
catch (Exception e)
{
Expand Down
32 changes: 22 additions & 10 deletions bench/algorithm/http-server/2-http3.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
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.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -43,13 +47,22 @@ public static async Task Main(string[] args)

var port = 30000 + new Random().Next(10000);
var app = CreateWebApplication(port);
app.MapPost("/", async ctx =>
app.MapPost("/", async (HttpResponse response, [FromBody] Payload payload) =>
{
using var sr = new StreamReader(ctx.Request.Body);
var bodyText = await sr.ReadToEndAsync().ConfigureAwait(false);
var payload = JsonSerializer.Deserialize<Payload>(bodyText);
ctx.Response.StatusCode = 200;
await ctx.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(payload.Value.ToString())).ConfigureAwait(false);

if (payload.Value.TryFormat(response.BodyWriter.GetSpan(16), out n))
{
response.StatusCode = StatusCodes.Status200OK;

response.BodyWriter.Advance(n);
await response.BodyWriter.FlushAsync().ConfigureAwait(false);
}
else
{
throw new UnreachableException();
}


});

using var serverTask = app.RunAsync();
Expand All @@ -70,14 +83,13 @@ public static async Task Main(string[] args)

private static async Task<int> SendAsync(string api, int value)
{
var payload = JsonSerializer.Serialize(new Payload { Value = value });
var payload = 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));
var response = await s_client.PostAsJsonAsync(api, payload).ConfigureAwait(false);
return int.Parse(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
}
catch (Exception e)
{
Expand Down
32 changes: 22 additions & 10 deletions bench/algorithm/http-server/2.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
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.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

static class Program
Expand All @@ -34,13 +38,22 @@ public static async Task Main(string[] args)

var port = 30000 + new Random().Next(10000);
var app = CreateWebApplication(port);
app.MapPost("/", async ctx =>
app.MapPost("/", async(HttpResponse response, [FromBody] Payload payload) =>
{
using var sr = new StreamReader(ctx.Request.Body);
var bodyText = await sr.ReadToEndAsync().ConfigureAwait(false);
var payload = JsonSerializer.Deserialize<Payload>(bodyText);
ctx.Response.StatusCode = 200;
await ctx.Response.BodyWriter.WriteAsync(Encoding.UTF8.GetBytes(payload.Value.ToString())).ConfigureAwait(false);

if (payload.Value.TryFormat(response.BodyWriter.GetSpan(16), out n))
{
response.StatusCode = StatusCodes.Status200OK;

response.BodyWriter.Advance(n);
await response.BodyWriter.FlushAsync().ConfigureAwait(false);
}
else
{
throw new UnreachableException();
}


});

using var serverTask = app.RunAsync();
Expand All @@ -63,14 +76,13 @@ public static async Task Main(string[] args)
private static async Task<int> SendAsync(string api, int value)
{
// await Task.Yield();
var payload = JsonSerializer.Serialize(new Payload { Value = value });
var payload = 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));
var response = await s_client.PostAsJsonAsync(api, payload).ConfigureAwait(false);
return int.Parse(await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false));
}
catch { }
}
Expand Down
Loading