mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
* Make use of language features line AsyncDispose, init etc. * Fix build errors * Improve Unit Tests * Improve Unit Tests * Improve Unit Tests * Update samples * Remove obsolete benchmarks * Code cleanup * Improve unit tests * Improve Unit Tests * Fix Unit Tests * Improve Unit Tests * Improve Unit Tests * Restore unit tests * Exclude load tests from unit tests
67 lines
1.7 KiB
C#
67 lines
1.7 KiB
C#
using BenchmarkDotNet.Attributes;
|
|
using BenchmarkDotNet.Jobs;
|
|
using MQTTnet.Formatter;
|
|
using System;
|
|
using System.Buffers;
|
|
using System.IO;
|
|
using System.IO.Pipelines;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MQTTnet.Benchmarks;
|
|
|
|
[SimpleJob(RuntimeMoniker.Net60)]
|
|
[RPlotExporter, RankColumn]
|
|
[MemoryDiagnoser]
|
|
public class SendPacketAsyncBenchmark : BaseBenchmark
|
|
{
|
|
MemoryStream _stream;
|
|
MqttPacketBuffer _buffer;
|
|
|
|
[GlobalSetup]
|
|
public void GlobalSetup()
|
|
{
|
|
_stream = new MemoryStream(1024);
|
|
var packet = new ArraySegment<byte>(new byte[10]);
|
|
_buffer = new MqttPacketBuffer(packet);
|
|
}
|
|
|
|
[Benchmark(Baseline = true)]
|
|
public async ValueTask Before()
|
|
{
|
|
_stream.Position = 0;
|
|
var output = PipeWriter.Create(_stream);
|
|
|
|
WritePacketBuffer(output, _buffer);
|
|
await output.FlushAsync();
|
|
}
|
|
|
|
[Benchmark]
|
|
public async ValueTask After()
|
|
{
|
|
_stream.Position = 0;
|
|
var output = PipeWriter.Create(_stream);
|
|
|
|
if (_buffer.Payload.Length == 0)
|
|
{
|
|
await output.WriteAsync(_buffer.Packet).ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
WritePacketBuffer(output, _buffer);
|
|
await output.FlushAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
static void WritePacketBuffer(PipeWriter output, MqttPacketBuffer buffer)
|
|
{
|
|
// copy MqttPacketBuffer's Packet and Payload to the same buffer block of PipeWriter
|
|
// MqttPacket will be transmitted within the bounds of a WebSocket frame after PipeWriter.FlushAsync
|
|
|
|
var span = output.GetSpan(buffer.Length);
|
|
|
|
buffer.Packet.AsSpan().CopyTo(span);
|
|
buffer.Payload.CopyTo(span[buffer.Packet.Count..]);
|
|
|
|
output.Advance(buffer.Length);
|
|
}
|
|
} |