@@ -2,7 +2,7 @@ | |||||
namespace MQTTnet.Internal | namespace MQTTnet.Internal | ||||
{ | { | ||||
internal static class MqttApplicationMessageExtensions | |||||
public static class MqttApplicationMessageExtensions | |||||
{ | { | ||||
public static MqttApplicationMessage ToApplicationMessage(this MqttPublishPacket publishPacket) | public static MqttApplicationMessage ToApplicationMessage(this MqttPublishPacket publishPacket) | ||||
{ | { | ||||
@@ -3,7 +3,7 @@ using System.Threading; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using MQTTnet.Channel; | using MQTTnet.Channel; | ||||
namespace MQTTnet.Core.Tests | |||||
namespace MQTTnet.Core.Internal | |||||
{ | { | ||||
public class TestMqttChannel : IMqttChannel | public class TestMqttChannel : IMqttChannel | ||||
{ | { |
@@ -150,6 +150,7 @@ | |||||
<Compile Include="MessageProcessingBenchmark.cs" /> | <Compile Include="MessageProcessingBenchmark.cs" /> | ||||
<Compile Include="Program.cs" /> | <Compile Include="Program.cs" /> | ||||
<Compile Include="Properties\AssemblyInfo.cs" /> | <Compile Include="Properties\AssemblyInfo.cs" /> | ||||
<Compile Include="SerializerBenchmark.cs" /> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<None Include="App.config" /> | <None Include="App.config" /> | ||||
@@ -1,5 +1,7 @@ | |||||
using System; | using System; | ||||
using System.Threading; | |||||
using BenchmarkDotNet.Running; | using BenchmarkDotNet.Running; | ||||
using MQTTnet.Diagnostics; | |||||
namespace MQTTnet.Benchmarks | namespace MQTTnet.Benchmarks | ||||
{ | { | ||||
@@ -7,7 +9,23 @@ namespace MQTTnet.Benchmarks | |||||
{ | { | ||||
public static void Main(string[] args) | public static void Main(string[] args) | ||||
{ | { | ||||
var summary = BenchmarkRunner.Run<MessageProcessingBenchmark>(); | |||||
Console.WriteLine($"MQTTnet - BenchmarkApp.{TargetFrameworkInfoProvider.TargetFramework}"); | |||||
Console.WriteLine("1 = MessageProcessingBenchmark"); | |||||
Console.WriteLine("2 = SerializerBenchmark"); | |||||
var pressedKey = Console.ReadKey(true); | |||||
switch (pressedKey.KeyChar) | |||||
{ | |||||
case '1': | |||||
BenchmarkRunner.Run<MessageProcessingBenchmark>(); | |||||
break; | |||||
case '2': | |||||
BenchmarkRunner.Run<SerializerBenchmark>(); | |||||
break; | |||||
default: | |||||
break; | |||||
} | |||||
Console.ReadLine(); | Console.ReadLine(); | ||||
} | } | ||||
} | } | ||||
@@ -0,0 +1,75 @@ | |||||
using BenchmarkDotNet.Attributes; | |||||
using MQTTnet.Client; | |||||
using MQTTnet.Packets; | |||||
using MQTTnet.Serializer; | |||||
using MQTTnet.Internal; | |||||
using MQTTnet.Server; | |||||
using BenchmarkDotNet.Attributes.Jobs; | |||||
using BenchmarkDotNet.Attributes.Exporters; | |||||
using System; | |||||
using System.Threading; | |||||
using System.IO; | |||||
using MQTTnet.Core.Internal; | |||||
namespace MQTTnet.Benchmarks | |||||
{ | |||||
[ClrJob] | |||||
[RPlotExporter] | |||||
[MemoryDiagnoser] | |||||
public class SerializerBenchmark | |||||
{ | |||||
private MqttApplicationMessage _message; | |||||
private MqttBasePacket _packet; | |||||
private ArraySegment<byte> _serializedPacket; | |||||
private MqttPacketSerializer _serializer; | |||||
[GlobalSetup] | |||||
public void Setup() | |||||
{ | |||||
_message = new MqttApplicationMessageBuilder() | |||||
.WithTopic("A") | |||||
.Build(); | |||||
_packet = _message.ToPublishPacket(); | |||||
_serializer = new MqttPacketSerializer(); | |||||
_serializedPacket = _serializer.Serialize(_packet); | |||||
} | |||||
[Benchmark] | |||||
public void Serialize_10000_Messages() | |||||
{ | |||||
for (var i = 0; i < 10000; i++) | |||||
{ | |||||
_serializer.Serialize(_packet); | |||||
} | |||||
} | |||||
[Benchmark] | |||||
public void Deserialize_10000_Messages() | |||||
{ | |||||
for (var i = 0; i < 10000; i++) | |||||
{ | |||||
using (var headerStream = new MemoryStream(Join(_serializedPacket))) | |||||
{ | |||||
var header = MqttPacketReader.ReadHeaderAsync(new TestMqttChannel(headerStream), CancellationToken.None).GetAwaiter().GetResult(); | |||||
using (var bodyStream = new MemoryStream(Join(_serializedPacket), (int)headerStream.Position, header.BodyLength)) | |||||
{ | |||||
var deserializedPacket = _serializer.Deserialize(header, bodyStream); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
private static byte[] Join(params ArraySegment<byte>[] chunks) | |||||
{ | |||||
var buffer = new MemoryStream(); | |||||
foreach (var chunk in chunks) | |||||
{ | |||||
buffer.Write(chunk.Array, chunk.Offset, chunk.Count); | |||||
} | |||||
return buffer.ToArray(); | |||||
} | |||||
} | |||||
} |
@@ -1,6 +1,7 @@ | |||||
using System.IO; | using System.IO; | ||||
using System.Threading; | using System.Threading; | ||||
using Microsoft.VisualStudio.TestTools.UnitTesting; | using Microsoft.VisualStudio.TestTools.UnitTesting; | ||||
using MQTTnet.Core.Internal; | |||||
using MQTTnet.Serializer; | using MQTTnet.Serializer; | ||||
namespace MQTTnet.Core.Tests | namespace MQTTnet.Core.Tests | ||||