You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageProcessingMqttConnectionContextBenchmark.cs 2.3 KiB

преди 5 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using BenchmarkDotNet.Attributes;
  2. using MQTTnet.Client;
  3. using MQTTnet.AspNetCore;
  4. using Microsoft.AspNetCore;
  5. using Microsoft.AspNetCore.Hosting;
  6. using MQTTnet.Server;
  7. using MQTTnet.Diagnostics;
  8. using MQTTnet.AspNetCore.Client;
  9. using MQTTnet.Client.Options;
  10. namespace MQTTnet.Benchmarks
  11. {
  12. [MemoryDiagnoser]
  13. public class MessageProcessingMqttConnectionContextBenchmark
  14. {
  15. private IWebHost _host;
  16. private IMqttClient _mqttClient;
  17. private MqttApplicationMessage _message;
  18. [GlobalSetup]
  19. public void Setup()
  20. {
  21. _host = WebHost.CreateDefaultBuilder()
  22. .UseKestrel(o => o.ListenAnyIP(1883, l => l.UseMqtt()))
  23. .ConfigureServices(services => {
  24. var mqttServerOptions = new MqttServerOptionsBuilder()
  25. .WithoutDefaultEndpoint()
  26. .Build();
  27. services
  28. .AddHostedMqttServer(mqttServerOptions)
  29. .AddMqttConnectionHandler();
  30. })
  31. .Configure(app => {
  32. app.UseMqttServer(s => {
  33. });
  34. })
  35. .Build();
  36. var factory = new MqttFactory();
  37. _mqttClient = factory.CreateMqttClient(new MqttNetLogger(), new MqttClientConnectionContextFactory());
  38. _host.StartAsync().GetAwaiter().GetResult();
  39. var clientOptions = new MqttClientOptionsBuilder()
  40. .WithTcpServer("localhost").Build();
  41. _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult();
  42. _message = new MqttApplicationMessageBuilder()
  43. .WithTopic("A")
  44. .Build();
  45. }
  46. [GlobalCleanup]
  47. public void Cleanup()
  48. {
  49. _mqttClient.DisconnectAsync().GetAwaiter().GetResult();
  50. _mqttClient.Dispose();
  51. _host.StopAsync().GetAwaiter().GetResult();
  52. _host.Dispose();
  53. }
  54. [Benchmark]
  55. public void Send_10000_Messages()
  56. {
  57. for (var i = 0; i < 10000; i++)
  58. {
  59. _mqttClient.PublishAsync(_message).GetAwaiter().GetResult();
  60. }
  61. }
  62. }
  63. }