No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

75 líneas
2.0 KiB

  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. IDuplexPipe _client;
  14. 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. 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. 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. }