25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

73 satır
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.Benchmarks.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. _client = clientConnection.StartAsync().GetAwaiter().GetResult();
  23. var serverConnection = new TcpConnection(task.GetAwaiter().GetResult());
  24. _server = serverConnection.StartAsync().GetAwaiter().GetResult();
  25. }
  26. [Benchmark]
  27. public async Task Send_10000_Chunks_Pipe()
  28. {
  29. var size = 5;
  30. var iterations = 10000;
  31. await Task.WhenAll(WriteAsync(iterations, size), ReadAsync(iterations, size));
  32. }
  33. private async Task ReadAsync(int iterations, int size)
  34. {
  35. await Task.Yield();
  36. var expected = iterations * size;
  37. long read = 0;
  38. var input = _client.Input;
  39. while (read < expected)
  40. {
  41. var readresult = await input.ReadAsync(CancellationToken.None).ConfigureAwait(false);
  42. input.AdvanceTo(readresult.Buffer.End);
  43. read += readresult.Buffer.Length;
  44. }
  45. }
  46. private async Task WriteAsync(int iterations, int size)
  47. {
  48. await Task.Yield();
  49. var output = _server.Output;
  50. for (var i = 0; i < iterations; i++)
  51. {
  52. await output.WriteAsync(new byte[size], CancellationToken.None).ConfigureAwait(false);
  53. }
  54. }
  55. }
  56. }