Benchmark for execution test written
This commit is contained in:
@@ -19,12 +19,12 @@ public static class CborExtensions
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
writer.WriteByteString(span);
|
||||
}
|
||||
|
||||
|
||||
public static void WriteToDest(this RequestId id, Span<byte> destination)
|
||||
{
|
||||
MemoryMarshal.Write(destination, ref id);
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public static unsafe void WriteToCborOpt(this RequestId id, CborWriter writer)
|
||||
{
|
||||
@@ -32,4 +32,17 @@ public static class CborExtensions
|
||||
MemoryMarshal.Write(span, ref id);
|
||||
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..]);
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,4 @@ using mROA.Benchmark;
|
||||
|
||||
Console.WriteLine("Hello, World!");
|
||||
|
||||
BenchmarkRunner.Run<ConcurrentAlloc>();
|
||||
BenchmarkRunner.Run<TaskWaiting>();
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,8 @@ using mROA.Implementation;
|
||||
[MemoryDiagnoser]
|
||||
public class RequestWriter
|
||||
{
|
||||
private const int N = 1000;
|
||||
public RequestId Id = RequestId.Generate();
|
||||
private CborWriter _writer;
|
||||
public RequestId Id;
|
||||
private readonly CborWriter _writer;
|
||||
|
||||
public RequestWriter()
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\mROA.Cbor\mROA.Cbor.csproj" />
|
||||
<ProjectReference Include="..\mROA\mROA.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user