Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

81 righe
2.3 KiB

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