-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c2477d
commit 64b67c9
Showing
7 changed files
with
234 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Paramore.Brighter.Perf | ||
{ | ||
public class Benchmark | ||
{ | ||
private readonly JsonMessageMapper<TestCommand> _jsonMessageMapper; | ||
private readonly JsonMessageMapper2<TestCommand> _mapper2; | ||
private readonly Message _message; | ||
private readonly TestCommand _testCommand; | ||
|
||
public Benchmark() | ||
{ | ||
RequestContext requestContext = new RequestContext(); | ||
_jsonMessageMapper = new JsonMessageMapper<TestCommand>(requestContext); | ||
_mapper2 = new JsonMessageMapper2<TestCommand>(requestContext); | ||
|
||
DateTime dateTime = DateTime.UtcNow; | ||
_testCommand = new TestCommand | ||
{ | ||
Message = "This is a message", | ||
Number = 999, | ||
DateNow = dateTime | ||
}; | ||
|
||
string body = | ||
"{\"message\":\"This is a message\",\"number\":999,\"dateNow\":\"2019-04-09T15:06:56.7623017Z\",\"id\":\"7d9120b9-a18e-43ac-a63e-8201a43ea623\"}"; | ||
Guid correlationId = Guid.NewGuid(); | ||
_message = new Message( | ||
new MessageHeader(new Guid("7d9120b9-a18e-43ac-a63e-8201a43ea623"), "Blah", MessageType.MT_COMMAND, | ||
correlationId), new MessageBody(body)); | ||
} | ||
|
||
[Benchmark] | ||
public void MapToMessageJsonByte() | ||
{ | ||
_jsonMessageMapper.MapToMessage(_testCommand); | ||
} | ||
|
||
[Benchmark] | ||
public void MapFromMessageJsonByte() | ||
{ | ||
_jsonMessageMapper.MapToRequest(_message); | ||
} | ||
|
||
[Benchmark] | ||
public void MapToMessageJsonString() | ||
{ | ||
_mapper2.MapToMessage(_testCommand); | ||
} | ||
|
||
[Benchmark] | ||
public void MapFromMessageJsonString() | ||
{ | ||
_mapper2.MapToRequest(_message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.2</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\Paramore.Brighter\Paramore.Brighter.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace Paramore.Brighter.Perf | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var summary = BenchmarkRunner.Run<Benchmark>(); | ||
Console.WriteLine(summary); | ||
} | ||
} | ||
|
||
|
||
public class TestCommand : Command | ||
{ | ||
public TestCommand() : base(Guid.NewGuid()) | ||
{ | ||
} | ||
|
||
public string Message { get; set; } | ||
public int Number { get; set; } | ||
public DateTime DateNow { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
|
||
namespace Paramore.Brighter | ||
{ | ||
public abstract class BaseMessageMapper<T> : IAmAMessageMapper<T> where T : class, IRequest | ||
{ | ||
private readonly IRequestContext _requestContext; | ||
private readonly Func<T, string> _routingAction; | ||
private readonly RoutingKey _routingKey; | ||
|
||
protected BaseMessageMapper(IRequestContext requestContext, RoutingKey routingKey = null, Func<T, string> routingKeyFunc = null) | ||
{ | ||
_requestContext = requestContext; | ||
_routingKey = routingKey; | ||
_routingAction = routingKeyFunc; | ||
} | ||
|
||
public Message MapToMessage(T request) | ||
{ | ||
MessageType messageType; | ||
if (request is Command) | ||
messageType = MessageType.MT_COMMAND; | ||
else if (request is Event) | ||
messageType = MessageType.MT_EVENT; | ||
else | ||
{ | ||
throw new ArgumentException("This message mapper can only map Commands and Events", nameof(request)); | ||
} | ||
|
||
var topic = _routingAction?.Invoke(request) ?? _routingKey ?? request.GetType().Name; | ||
|
||
var messageHeader = new MessageHeader(request.Id, topic, messageType, _requestContext.Header.CorrelationId, contentType: "application/json"); | ||
|
||
return new Message(messageHeader, CreateMessageBody(request)); | ||
} | ||
|
||
protected abstract MessageBody CreateMessageBody(T request); | ||
|
||
public T MapToRequest(Message message) | ||
{ | ||
_requestContext.Header.CorrelationId = message.Header.CorrelationId; | ||
|
||
return CreateType(message); | ||
} | ||
|
||
protected abstract T CreateType(Message message); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace Paramore.Brighter | ||
{ | ||
public class JsonMessageMapper<T> : BaseMessageMapper<T> where T : class, IRequest | ||
{ | ||
private readonly JsonSerializer _serializer = new JsonSerializer(); | ||
|
||
public JsonMessageMapper(IRequestContext requestContext, RoutingKey routingKey = null, | ||
Func<T, string> routingKeyFunc = null) : base(requestContext, routingKey, routingKeyFunc) | ||
{ | ||
} | ||
|
||
protected override MessageBody CreateMessageBody(T request) | ||
{ | ||
using (MemoryStream memoryStream = new MemoryStream()) | ||
{ | ||
using (StreamWriter streamWriter = new StreamWriter(memoryStream, new UTF8Encoding(false))) | ||
using (JsonTextWriter jsonTextWriter = new JsonTextWriter(streamWriter)) | ||
{ | ||
_serializer.Serialize(jsonTextWriter, request, typeof(T)); | ||
} | ||
|
||
return new MessageBody(memoryStream.ToArray(), "JSON"); | ||
} | ||
} | ||
|
||
protected override T CreateType(Message message) | ||
{ | ||
using (MemoryStream memoryStream = new MemoryStream(message.Body.Bytes)) | ||
using (StreamReader streamReader = new StreamReader(memoryStream, Encoding.UTF8)) | ||
using (JsonReader reader = new JsonTextReader(streamReader)) | ||
{ | ||
return _serializer.Deserialize<T>(reader); | ||
} | ||
} | ||
} | ||
|
||
public class JsonMessageMapper2<T> : BaseMessageMapper<T> where T : class, IRequest | ||
{ | ||
public JsonMessageMapper2(IRequestContext requestContext, RoutingKey routingKey = null, | ||
Func<T, string> routingKeyFunc = null) : base(requestContext, routingKey, routingKeyFunc) | ||
{ | ||
} | ||
|
||
protected override MessageBody CreateMessageBody(T request) | ||
{ | ||
return new MessageBody(JsonConvert.SerializeObject(request), "JSON"); | ||
} | ||
|
||
protected override T CreateType(Message message) | ||
{ | ||
return JsonConvert.DeserializeObject<T>(message.Body.Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters