From 1b0f8676533c7f9ec74a0b3c76f965f044e0e994 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofaton Date: Mon, 30 Jun 2025 15:45:41 +0300 Subject: [PATCH] Some latency bug found --- Example.Backend/LoadTestImp.cs | 4 +-- Example.Backend/Program.cs | 2 +- Example.Frontend/Program.cs | 4 +-- Example.Shared/ILoadTest.cs | 33 +------------------ .../ChannelInteractionModule.cs | 12 ++++++- 5 files changed, 17 insertions(+), 38 deletions(-) diff --git a/Example.Backend/LoadTestImp.cs b/Example.Backend/LoadTestImp.cs index d92c7f5..a3752e5 100644 --- a/Example.Backend/LoadTestImp.cs +++ b/Example.Backend/LoadTestImp.cs @@ -9,9 +9,9 @@ namespace Example.Backend [SharedObjectSingleton] public class LoadTestImp : ILoadTest { - public int Next(int last) + public Task Next(int last) { - return last + 1; + return Task.FromResult( last + 1); } public int Last(int next) diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index 513f796..668505e 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -20,7 +20,7 @@ class Program builder.Modules.Add(new BackendIdentityGenerator()); // builder.UseNetworkGateway(new IPEndPoint(IPAddress.Loopback, 4567), typeof(NextGenerationInteractionModule), // builder.GetModule()!); - var listening = new IPEndPoint(IPAddress.Loopback, 4567); + var listening = new IPEndPoint(IPAddress.Any, 4567); builder.UseNetworkGateway(listening, typeof(ChannelInteractionModule), builder.GetModule()!); builder.Modules.Add(new UdpGateway(listening)); diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 14843bd..8cab54c 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -127,12 +127,12 @@ class Program DemoCheck.TaskCancelation = true; #endif - const int iterations = 10000; + const int iterations = 10; var timer = Stopwatch.StartNew(); var x = 0; for (int i = 0; i < iterations; i++) { - x = loadSingleton.Next(x); + x = await loadSingleton.Next(x); } timer.Stop(); diff --git a/Example.Shared/ILoadTest.cs b/Example.Shared/ILoadTest.cs index 31a06b8..4d9bc21 100644 --- a/Example.Shared/ILoadTest.cs +++ b/Example.Shared/ILoadTest.cs @@ -9,42 +9,11 @@ namespace Example.Shared [SharedObjectInterface] public interface ILoadTest : IShared { - int Next(int last); + Task Next(int last); int Last(int next); void C(); void A(); Task AsyncTest(CancellationToken token = default); } - - partial class LoadTestProxy2 - : RemoteObjectBase, ILoadTest - { - public LoadTestProxy2(int id, IRepresentationModule representationModule, IEndPointContext context) - : base(id, representationModule, context) - { - } - - public void A(){ - CallAsync(0).Wait(); - } - - public async System.Threading.Tasks.Task AsyncTest(System.Threading.CancellationToken token){ - await CallAsync(1, cancellationToken : token); - } - - public void C(){ - CallAsync(2).Wait(); - } - - public System.Int32 Last(System.Int32 next){ - return GetResultAsync(3, new System.Object[] { next }).GetAwaiter().GetResult(); - } - - public System.Int32 Next(System.Int32 last){ - return GetResultAsync(4, new System.Object[] { last }).GetAwaiter().GetResult(); - } - - - } } \ No newline at end of file diff --git a/mROA/Implementation/ChannelInteractionModule.cs b/mROA/Implementation/ChannelInteractionModule.cs index 1792e48..5acef43 100644 --- a/mROA/Implementation/ChannelInteractionModule.cs +++ b/mROA/Implementation/ChannelInteractionModule.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Threading; using System.Threading.Channels; @@ -158,6 +159,7 @@ namespace mROA.Implementation public class StreamExtractor { + private static Stopwatch profiler = new Stopwatch(); private readonly Stream _ioStream; private readonly IContextualSerializationToolKit _serializationToolkit; private const int BufferSize = ushort.MaxValue; @@ -171,6 +173,7 @@ namespace mROA.Implementation _ioStream = ioStream; _serializationToolkit = serializationToolkit; _context = context; + profiler.Start(); } public Action MessageReceived = _ => { }; @@ -194,11 +197,14 @@ namespace mROA.Implementation public async Task SingleReceive(CancellationToken token = default) { + Console.WriteLine($"Receive start. Time: {profiler.ElapsedMilliseconds}ms"); var len = ReadMessageLength(); + Console.WriteLine($"Received len {len}. Time: {profiler.ElapsedMilliseconds}ms"); var localSpan = _buffer[..len]; await _ioStream.ReadExactlyAsync(localSpan, cancellationToken: token); - + Console.WriteLine($"Received. Time: {profiler.ElapsedMilliseconds}ms"); + profiler.Restart(); var message = _serializationToolkit.Deserialize(localSpan, _context); MessageReceived(message); } @@ -215,9 +221,13 @@ namespace mROA.Implementation { var rawMessage = _serializationToolkit.Serialize(message, _context); var header = BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort)); + Console.WriteLine($"Send start. Time: {profiler.ElapsedMilliseconds}ms"); await _ioStream.WriteAsync(header, token); await _ioStream.WriteAsync(rawMessage, token); + Console.WriteLine($"Send. Time: {profiler.ElapsedMilliseconds}ms"); + profiler.Restart(); + } public async Task SendFromChannel(ChannelReader channel,