-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy path2.cs
98 lines (90 loc) · 3.12 KB
/
2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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.Extensions.Logging;
static class Program
{
private static readonly HttpClient s_client = new HttpClient() { Timeout = TimeSpan.FromSeconds(1) };
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<Payload>(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 = $"http://localhost:{port}/";
var tasks = new List<Task<int>>(n);
for (var i = 1; i <= n; i++)
{
tasks.Add(SendAsync(api, i));
}
// await Task.WhenAll(tasks).ConfigureAwait(false);
foreach (var task in tasks)
{
sum += await task.ConfigureAwait(false);
}
Console.WriteLine(sum);
Environment.Exit(0);
}
private static async Task<int> SendAsync(string api, int value)
{
// await Task.Yield();
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 { }
}
}
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);
});
return builder.Build();
}
}
public struct Payload
{
[JsonPropertyName("value")]
public int Value { get; set; }
}