Files
mROA/Example.Load/Program.cs
T

134 lines
5.0 KiB
C#
Raw Normal View History

2026-06-05 09:02:25 +03:00
using System.Diagnostics;
using System.Net;
using System.Runtime;
2025-07-09 12:01:08 +03:00
using Example.Shared;
2025-07-12 20:25:17 +03:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2025-07-09 12:01:08 +03:00
using mROA.Abstract;
using mROA.Cbor;
using mROA.Codegen;
using mROA.Implementation;
using mROA.Implementation.Backend;
using mROA.Implementation.Frontend;
2025-07-12 21:28:37 +03:00
const int C = 100;
2025-07-09 12:01:08 +03:00
var time = TimeSpan.FromSeconds(10);
Console.WriteLine($"Starting bench for {time} from {C} connections");
var cts = new CancellationTokenSource();
new RemoteTypeBinder();
2025-07-12 21:28:37 +03:00
var eps = await GetLoadEndpoints(C);
2026-06-05 09:02:25 +03:00
var tasks = new Task<(int, double[])>[C];
2025-07-09 12:01:08 +03:00
for (int i = 0; i < C; i++)
{
2025-07-12 21:28:37 +03:00
tasks[i] = Requests(cts.Token, i, eps[i]);
2025-07-09 12:01:08 +03:00
}
cts.CancelAfter(time);
2025-07-12 20:25:17 +03:00
Console.WriteLine($"Test end at {DateTime.Now.Add(time)}");
2025-07-09 12:01:08 +03:00
Console.WriteLine("Start waiting");
await Task.WhenAll(tasks);
Console.WriteLine("End waiting");
2026-06-05 09:02:25 +03:00
var totalRequests = tasks.Sum(i => i.Result.Item1);
var totalLatency = tasks.SelectMany(i => i.Result.Item2).ToList();
totalLatency.Sort();
var n = totalLatency.Count;
var p50 = totalLatency[(int)(n * 50f / 100f)];
var p95 = totalLatency[(int)(n * 95f / 100f)];
var p99 = totalLatency[(int)(n * 99f / 100f)];
Console.WriteLine($"Total requests: {totalRequests:N0}");
2025-07-09 12:01:08 +03:00
Console.WriteLine($"Results: {totalRequests / time.TotalSeconds:N} RPS");
2026-06-05 09:02:25 +03:00
Console.WriteLine("Latency (µs): p50={0} p95={1} p99={2}", p50, p95, p99);
2025-07-27 23:46:54 +03:00
File.AppendAllText("results.txt", $"[FAST ID] {totalRequests}\r\n");
2025-07-12 21:28:37 +03:00
async Task<List<ILoadTest>> GetLoadEndpoints(int count)
2025-07-09 12:01:08 +03:00
{
2025-07-27 00:14:15 +03:00
try
2025-07-12 20:25:17 +03:00
{
2025-07-27 00:14:15 +03:00
var loads = new List<ILoadTest>();
for (int i = 0; i < count; i++)
2025-07-12 20:25:17 +03:00
{
2025-07-27 00:14:15 +03:00
var builder = Host.CreateApplicationBuilder(new HostApplicationBuilderSettings { DisableDefaults = true });
builder.Services.AddSingleton<IContextualSerializationToolKit, CborSerializationToolkit>();
builder.Services.AddSingleton<IEndPointContext, EndPointContext>();
builder.Services.AddSingleton<IRealStoreInstanceRepository, InstanceRepository>(provider =>
{
var repo = new InstanceRepository(provider.GetService<IRepresentationModuleProducer>());
repo.FillSingletons(typeof(Program).Assembly);
return repo;
});
builder.Services.AddSingleton<IInstanceRepository, RemoteInstanceRepository>();
builder.Services.AddSingleton<IChannelInteractionModule, ChannelInteractionModule>();
// builder.Services.AddSingleton<IUntrustedInteractionModule, UdpUntrustedInteraction>();
builder.Services.AddSingleton<IRepresentationModule, RepresentationModule>();
var serverEndPoint = new IPEndPoint(IPAddress.Loopback, 4567);
builder.Services.AddSingleton<IFrontendBridge, NetworkFrontendBridge>();
builder.Services.AddOptions();
builder.Services.Configure<GatewayOptions>(options => options.Endpoint = serverEndPoint);
builder.Services.AddSingleton<IRepresentationModuleProducer, StaticRepresentationModuleProducer>();
// builder.Services.AddSingleton<IRequestExtractor, RequestExtractor>();
builder.Services.AddSingleton<IExecuteModule, BasicExecutionModule>();
builder.Services.AddSingleton<IMethodRepository, CollectableMethodRepository>(p =>
{
var methodRepo = new CollectableMethodRepository();
methodRepo.AppendInvokers(new GeneratedInvokersCollection());
return methodRepo;
});
builder.Services.AddSingleton<ICallIndexProvider, GeneratedCallIndexProvider>();
builder.Services.AddSingleton<ICancellationRepository, CancellationRepository>();
var app = builder.Build();
var frontendBridge = app.Services.GetService<IFrontendBridge>()!;
await frontendBridge.Connect();
// _ = app.Services.GetService<IRequestExtractor>()!.StartExtraction();
// _ = app.Services.GetService<IUntrustedInteractionModule>().Start(serverEndPoint);
var context = app.Services.GetService<IInstanceRepository>();
var singletonObject =
context.GetSingletonObject<ILoadTest>(
app.Services.GetService<IEndPointContext>());
loads.Add(singletonObject);
}
return loads;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
2025-07-12 21:28:37 +03:00
}
}
2026-06-05 09:02:25 +03:00
async Task<(int, double[])> Requests(CancellationToken token, int id, ILoadTest load)
2025-07-12 21:28:37 +03:00
{
try
{
2026-06-05 09:02:25 +03:00
var latencyList = new List<double>();
var sw = new Stopwatch();
2025-07-12 20:25:17 +03:00
int count = 0;
2025-07-27 00:14:15 +03:00
while (true)
{
2025-07-12 20:25:17 +03:00
if (token.IsCancellationRequested)
{
break;
}
2025-07-12 21:28:37 +03:00
2026-06-05 09:02:25 +03:00
sw.Restart();
2025-07-12 21:28:37 +03:00
await load.Next(2);
2026-06-05 09:02:25 +03:00
sw.Stop();
latencyList.Add(sw.Elapsed.TotalMicroseconds);
2025-07-12 20:25:17 +03:00
count++;
2025-07-27 00:14:15 +03:00
}
2025-07-30 23:38:03 +03:00
2026-06-05 09:02:25 +03:00
return (count, latencyList.ToArray());
2025-07-12 20:25:17 +03:00
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
2025-07-09 12:01:08 +03:00
}