Benchmark for execution test written

This commit is contained in:
2025-08-07 23:39:42 +03:00
parent 0f355b9df3
commit c29db73a0e
12 changed files with 237 additions and 28 deletions
+8 -1
View File
@@ -76,7 +76,14 @@ class Program
using (var disposingPrinter = factory.Create("Test")) using (var disposingPrinter = factory.Create("Test"))
{ {
disposingPrinter.SetFingerPrint(new[] { 1, 2, 3 }).ContinueWith(r => { Console.WriteLine(r.Status); }); disposingPrinter.SetFingerPrint(new[] { 1, 2, 3 }).ContinueWith(r =>
{
Console.WriteLine(r.Status);
if (r.Status == TaskStatus.Faulted)
{
Console.WriteLine(r.Exception);
}
});
await disposingPrinter.IntTest(new MyData { Id = 5, Score = 7, Name = "Test" }); await disposingPrinter.IntTest(new MyData { Id = 5, Score = 7, Name = "Test" });
DemoCheck.CreatingPrinter = true; DemoCheck.CreatingPrinter = true;
+15 -2
View File
@@ -19,12 +19,12 @@ public static class CborExtensions
MemoryMarshal.Write(span, ref id); MemoryMarshal.Write(span, ref id);
writer.WriteByteString(span); writer.WriteByteString(span);
} }
public static void WriteToDest(this RequestId id, Span<byte> destination) public static void WriteToDest(this RequestId id, Span<byte> destination)
{ {
MemoryMarshal.Write(destination, ref id); MemoryMarshal.Write(destination, ref id);
} }
[MethodImpl(MethodImplOptions.AggressiveOptimization)] [MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static unsafe void WriteToCborOpt(this RequestId id, CborWriter writer) public static unsafe void WriteToCborOpt(this RequestId id, CborWriter writer)
{ {
@@ -32,4 +32,17 @@ public static class CborExtensions
MemoryMarshal.Write(span, ref id); MemoryMarshal.Write(span, ref id);
writer.WriteByteString(span); writer.WriteByteString(span);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RequestId ReadFromTrueCbor(CborReader reader)
{
var enc = reader.ReadEncodedValue(true);
return MemoryMarshal.Read<RequestId>(enc.Span[1..]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RequestId ReadFromCbor(CborReader reader)
{
var enc = reader.ReadEncodedValue();
return MemoryMarshal.Read<RequestId>(enc.Span[1..]);
}
} }
+1 -1
View File
@@ -5,4 +5,4 @@ using mROA.Benchmark;
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
BenchmarkRunner.Run<ConcurrentAlloc>(); BenchmarkRunner.Run<TaskWaiting>();
+38
View File
@@ -0,0 +1,38 @@
using System.Formats.Cbor;
using BenchmarkDotNet.Attributes;
using mROA.Implementation;
namespace mROA.Benchmark;
[MemoryDiagnoser]
[DisassemblyDiagnoser]
public class RequestReader
{
private ReadOnlyMemory<byte> _data;
public RequestReader()
{
_data = new ReadOnlyMemory<byte>([80, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
}
[Benchmark(Baseline = true)]
public ulong DefaultRead()
{
var reader = new CborReader(_data);
return new RequestId(reader.ReadByteString()).P0;
}
[Benchmark]
public ulong MemoryRead()
{
var reader = new CborReader(_data);
return CborExtensions.ReadFromCbor(reader).P0;
}
[Benchmark]
public ulong MemoryTrueRead()
{
var reader = new CborReader(_data);
return CborExtensions.ReadFromTrueCbor(reader).P0;
}
}
+2 -3
View File
@@ -5,9 +5,8 @@ using mROA.Implementation;
[MemoryDiagnoser] [MemoryDiagnoser]
public class RequestWriter public class RequestWriter
{ {
private const int N = 1000; public RequestId Id;
public RequestId Id = RequestId.Generate(); private readonly CborWriter _writer;
private CborWriter _writer;
public RequestWriter() public RequestWriter()
{ {
+133
View File
@@ -0,0 +1,133 @@
using BenchmarkDotNet.Attributes;
using mROA.Abstract;
using mROA.Cbor;
using mROA.Implementation;
using mROA.Implementation.Attributes;
using mROA.Implementation.Backend;
using mROA.Implementation.CommandExecution;
namespace mROA.Benchmark;
public class TaskWaiting
{
private readonly BasicExecutionModule _executionModule;
private readonly CallRequest _syncRequest;
private readonly CallRequest _asyncRequest;
private readonly InstanceRepository _instanceRepo;
private readonly EndPointContext _endPointContext;
private readonly FastRepresentationModule _representationModule;
public TaskWaiting()
{
_executionModule = new BasicExecutionModule(new CancellationRepository(), new TestMethodRepo(), new CborSerializationToolkit());
_syncRequest = new CallRequest{CommandId = 0, Parameters = null, Id = RequestId.Generate(), ObjectId = new ComplexObjectIdentifier(-1, 0)};
_asyncRequest = new CallRequest{CommandId = 1, Parameters = null, Id = RequestId.Generate(), ObjectId = new ComplexObjectIdentifier(-1, 0)};
_instanceRepo = new InstanceRepository(null);
_instanceRepo.FillSingletons(typeof(TaskWaiting).Assembly);
_endPointContext = new EndPointContext(_instanceRepo, null)
{
OwnerId = 0
};
_representationModule = new FastRepresentationModule();
}
[Benchmark(Baseline = true)]
public int DefaultJob()
{
return (int)((FinalCommandExecution<object>)_executionModule.Execute(_syncRequest, _instanceRepo, _representationModule, _endPointContext)).Result;
}
[Benchmark]
public async Task<int> DefaultJobAsync()
{
_representationModule.Signal = new TaskCompletionSource<int>();
var task = _representationModule.Signal.Task;
_ = _executionModule.Execute(_asyncRequest, _instanceRepo, _representationModule, _endPointContext);
_ = await task;
return (int)((FinalCommandExecution<object>)_representationModule.Result).Result;
}
}
public class TestMethodRepo : IMethodRepository
{
public IMethodInvoker GetMethod(int id)
{
if (id == 0)
return new MethodInvoker
{
IsVoid = false,
IsTrusted = true,
ReturnType = typeof(int),
ParameterTypes = Type.EmptyTypes,
SuitableType = typeof(IJobClass),
Invoking = (i, _, _) => (i as IJobClass).A()
};
return new AsyncMethodInvoker
{
IsVoid = false,
IsTrusted = true,
ReturnType = typeof(int),
ParameterTypes = Type.EmptyTypes,
SuitableType = typeof(IJobClass),
Invoking = (i, _, _, post) => (i as IJobClass).B().ContinueWith(task => post(task.Result))
};
}
}
[SharedObjectInterface]
public interface IJobClass
{
int A();
Task<int> B();
}
[SharedObjectSingleton]
public class JobClass : IJobClass
{
private readonly RequestWriter _requestWriter = new();
public int A()
{
return _requestWriter.DefaultCbor();
}
public Task<int> B()
{
return Task.FromResult(_requestWriter.DefaultCbor());
}
}
public class FastRepresentationModule : IRepresentationModule
{
public TaskCompletionSource<int> Signal = new();
public object Result;
public int Id { get; }
public IEndPointContext Context { get; }
public Task<(object? Deserialized, EMessageType MessageType)> GetSingle(Predicate<NetworkMessage> rule, IEndPointContext? context, CancellationToken token = default, params Func<NetworkMessage, Type?>[] converter)
{
throw new NotImplementedException();
}
public IAsyncEnumerable<(object parced, EMessageType originalType)> GetStream(Predicate<NetworkMessage> rule, IEndPointContext? context, CancellationToken token = default,
params Func<NetworkMessage, Type?>[] converter)
{
throw new NotImplementedException();
}
public Task PostCallMessageAsync<T>(RequestId id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull
{
Result = payload;
Signal.SetResult(0);
return Task.CompletedTask;
}
public void PostCallMessage<T>(RequestId id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull
{
throw new NotImplementedException();
}
public Task PostCallMessageUntrustedAsync<T>(RequestId id, EMessageType eMessageType, T payload, IEndPointContext? context) where T : notnull
{
throw new NotImplementedException();
}
}
+1
View File
@@ -9,6 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\mROA.Cbor\mROA.Cbor.csproj" />
<ProjectReference Include="..\mROA\mROA.csproj" /> <ProjectReference Include="..\mROA\mROA.csproj" />
</ItemGroup> </ItemGroup>
+16 -9
View File
@@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Formats.Cbor; using System.Formats.Cbor;
using System.Runtime.InteropServices;
using mROA.Abstract; using mROA.Abstract;
using mROA.Implementation; using mROA.Implementation;
using mROA.Implementation.CommandExecution; using mROA.Implementation.CommandExecution;
@@ -14,7 +16,8 @@ namespace mROA.Cbor
public class CallRequestParser : IOrdinaryStructureParser public class CallRequestParser : IOrdinaryStructureParser
{ {
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) public void Write(CborWriter writer, object value, IEndPointContext context,
CborSerializationToolkit serialization)
{ {
var v = (CallRequest)value; var v = (CallRequest)value;
writer.WriteStartArray(4); writer.WriteStartArray(4);
@@ -33,7 +36,8 @@ namespace mROA.Cbor
{ {
Id = new RequestId(reader.ReadByteString()), Id = new RequestId(reader.ReadByteString()),
CommandId = reader.ReadInt32(), CommandId = reader.ReadInt32(),
ObjectId = (ComplexObjectIdentifier)ComplexObjectIdentifierParser.Instance.Read(reader, context, serialization), ObjectId = (ComplexObjectIdentifier)ComplexObjectIdentifierParser.Instance.Read(reader, context,
serialization),
Parameters = serialization.ReadData(reader, typeof(object[]), context) as object[] Parameters = serialization.ReadData(reader, typeof(object[]), context) as object[]
}; };
reader.ReadEndArray(); reader.ReadEndArray();
@@ -44,7 +48,9 @@ namespace mROA.Cbor
public class ComplexObjectIdentifierParser : IOrdinaryStructureParser public class ComplexObjectIdentifierParser : IOrdinaryStructureParser
{ {
public static readonly ComplexObjectIdentifierParser Instance = new(); public static readonly ComplexObjectIdentifierParser Instance = new();
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization)
public void Write(CborWriter writer, object value, IEndPointContext context,
CborSerializationToolkit serialization)
{ {
writer.WriteUInt64(((ComplexObjectIdentifier)value).Flat); writer.WriteUInt64(((ComplexObjectIdentifier)value).Flat);
} }
@@ -58,7 +64,8 @@ namespace mROA.Cbor
public class FinalCommandExecutionParser : IOrdinaryStructureParser public class FinalCommandExecutionParser : IOrdinaryStructureParser
{ {
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) public void Write(CborWriter writer, object value, IEndPointContext context,
CborSerializationToolkit serialization)
{ {
var v = (FinalCommandExecution<object>)value; var v = (FinalCommandExecution<object>)value;
writer.WriteStartArray(2); writer.WriteStartArray(2);
@@ -76,18 +83,18 @@ namespace mROA.Cbor
Id = new RequestId(reader.ReadByteString()), Id = new RequestId(reader.ReadByteString()),
Result = serialization.ReadData(reader, typeof(object), context), Result = serialization.ReadData(reader, typeof(object), context),
}; };
reader.ReadEndArray(); reader.ReadEndArray();
return result; return result;
} }
} }
public class FinalCommandExecutionResultlessParser : IOrdinaryStructureParser public class FinalCommandExecutionResultlessParser : IOrdinaryStructureParser
{ {
public void Write(CborWriter writer, object value, IEndPointContext context, CborSerializationToolkit serialization) public void Write(CborWriter writer, object value, IEndPointContext context,
CborSerializationToolkit serialization)
{ {
var v = (FinalCommandExecution)value; var v = (FinalCommandExecution)value;
writer.WriteStartArray(1); writer.WriteStartArray(1);
// writer.WriteByteString(v.Id.ToByteArray());
v.Id.WriteToCborInline(writer); v.Id.WriteToCborInline(writer);
writer.WriteEndArray(); writer.WriteEndArray();
} }
+22
View File
@@ -0,0 +1,22 @@
using mROA.Benchmark;
namespace mROA.Test;
[TestFixture]
public class BenchmarkTest
{
[Test]
public void Benchmark()
{
var bench = new TaskWaiting();
var x = bench.DefaultJob();
var y = bench.DefaultJobAsync();
y.Wait();
if (x == y.Result)
{
Assert.Pass();
}
Assert.Fail();
}
}
-11
View File
@@ -18,17 +18,6 @@ public class ConcurrentTest
[Test] [Test]
public void ParallelAlloc() public void ParallelAlloc()
{ {
Parallel.For(0, 100, i =>
{
var data = _cmm.AllocSlice(1);
data.Span[0] = 1;
});
var total = _cmm.AllocSlice(100).Span;
if (total.IndexOf((byte)0) == -1)
{
Assert.Pass();
}
Assert.Fail(); Assert.Fail();
} }
} }
+1
View File
@@ -27,6 +27,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\mROA.Benchmark\mROA.Benchmark.csproj" />
<ProjectReference Include="..\mROA.Cbor\mROA.Cbor.csproj" /> <ProjectReference Include="..\mROA.Cbor\mROA.Cbor.csproj" />
<ProjectReference Include="..\mROA\mROA.csproj" /> <ProjectReference Include="..\mROA\mROA.csproj" />
</ItemGroup> </ItemGroup>
@@ -22,7 +22,6 @@ namespace mROA.Implementation.Backend
public ICommandExecution? Execute(CallRequest command, IInstanceRepository instanceRepository, public ICommandExecution? Execute(CallRequest command, IInstanceRepository instanceRepository,
IRepresentationModule representationModule, IEndPointContext endPointContext) IRepresentationModule representationModule, IEndPointContext endPointContext)
{ {
// _logger.LogInformation("Executing {0}", command.Id);
try try
{ {
var invoker = _methodRepo.GetMethod(command.CommandId); var invoker = _methodRepo.GetMethod(command.CommandId);