Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TcpPipesBenchmark.cs 2.1 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.IO.Pipelines;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using BenchmarkDotNet.Attributes;
  7. using MQTTnet.AspNetCore.Client.Tcp;
  8. namespace MQTTnet.Benchmarks
  9. {
  10. [MemoryDiagnoser]
  11. public class TcpPipesBenchmark
  12. {
  13. private IDuplexPipe _client;
  14. private IDuplexPipe _server;
  15. [GlobalSetup]
  16. public void Setup()
  17. {
  18. var server = new TcpListener(IPAddress.Any, 1883);
  19. server.Start(1);
  20. var task = Task.Run(() => server.AcceptSocket());
  21. var clientConnection = new TcpConnection(new IPEndPoint(IPAddress.Loopback, 1883));
  22. clientConnection.StartAsync().GetAwaiter().GetResult();
  23. _client = clientConnection.Transport;
  24. var serverConnection = new TcpConnection(task.GetAwaiter().GetResult());
  25. serverConnection.StartAsync().GetAwaiter().GetResult();
  26. _server = serverConnection.Transport;
  27. }
  28. [Benchmark]
  29. public async Task Send_10000_Chunks_Pipe()
  30. {
  31. var size = 5;
  32. var iterations = 10000;
  33. await Task.WhenAll(WriteAsync(iterations, size), ReadAsync(iterations, size));
  34. }
  35. private async Task ReadAsync(int iterations, int size)
  36. {
  37. await Task.Yield();
  38. var expected = iterations * size;
  39. long read = 0;
  40. var input = _client.Input;
  41. while (read < expected)
  42. {
  43. var readresult = await input.ReadAsync(CancellationToken.None).ConfigureAwait(false);
  44. input.AdvanceTo(readresult.Buffer.End);
  45. read += readresult.Buffer.Length;
  46. }
  47. }
  48. private async Task WriteAsync(int iterations, int size)
  49. {
  50. await Task.Yield();
  51. var output = _server.Output;
  52. for (var i = 0; i < iterations; i++)
  53. {
  54. await output.WriteAsync(new byte[size], CancellationToken.None).ConfigureAwait(false);
  55. }
  56. }
  57. }
  58. }