Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

92 rindas
3.2 KiB

  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Attributes.Exporters;
  3. using BenchmarkDotNet.Attributes.Jobs;
  4. using MQTTnet.Server;
  5. using System;
  6. namespace MQTTnet.Benchmarks
  7. {
  8. [ClrJob]
  9. [RPlotExporter]
  10. [MemoryDiagnoser]
  11. public class TopicFilterComparerBenchmark
  12. {
  13. private static readonly char[] TopicLevelSeparator = { '/' };
  14. [GlobalSetup]
  15. public void Setup()
  16. {
  17. }
  18. [Benchmark]
  19. public void MqttTopicFilterComparer_10000_StringSplitMethod()
  20. {
  21. for (var i = 0; i < 10000; i++)
  22. {
  23. LegacyMethodByStringSplit("sport/tennis/player1", "sport/#");
  24. LegacyMethodByStringSplit("sport/tennis/player1/ranking", "sport/#/ranking");
  25. LegacyMethodByStringSplit("sport/tennis/player1/score/wimbledon", "sport/+/player1/#");
  26. LegacyMethodByStringSplit("sport/tennis/player1", "sport/tennis/+");
  27. LegacyMethodByStringSplit("/finance", "+/+");
  28. LegacyMethodByStringSplit("/finance", "/+");
  29. LegacyMethodByStringSplit("/finance", "+");
  30. }
  31. }
  32. [Benchmark]
  33. public void MqttTopicFilterComparer_10000_LoopMethod()
  34. {
  35. for (var i = 0; i < 10000; i++)
  36. {
  37. MqttTopicFilterComparer.IsMatch("sport/tennis/player1", "sport/#");
  38. MqttTopicFilterComparer.IsMatch("sport/tennis/player1/ranking", "sport/#/ranking");
  39. MqttTopicFilterComparer.IsMatch("sport/tennis/player1/score/wimbledon", "sport/+/player1/#");
  40. MqttTopicFilterComparer.IsMatch("sport/tennis/player1", "sport/tennis/+");
  41. MqttTopicFilterComparer.IsMatch("/finance", "+/+");
  42. MqttTopicFilterComparer.IsMatch("/finance", "/+");
  43. MqttTopicFilterComparer.IsMatch("/finance", "+");
  44. }
  45. }
  46. private static bool LegacyMethodByStringSplit(string topic, string filter)
  47. {
  48. if (topic == null) throw new ArgumentNullException(nameof(topic));
  49. if (filter == null) throw new ArgumentNullException(nameof(filter));
  50. if (string.Equals(topic, filter, StringComparison.Ordinal))
  51. {
  52. return true;
  53. }
  54. var fragmentsTopic = topic.Split(TopicLevelSeparator, StringSplitOptions.None);
  55. var fragmentsFilter = filter.Split(TopicLevelSeparator, StringSplitOptions.None);
  56. // # > In either case it MUST be the last character specified in the Topic Filter [MQTT-4.7.1-2].
  57. for (var i = 0; i < fragmentsFilter.Length; i++)
  58. {
  59. if (fragmentsFilter[i] == "+")
  60. {
  61. continue;
  62. }
  63. if (fragmentsFilter[i] == "#")
  64. {
  65. return true;
  66. }
  67. if (i >= fragmentsTopic.Length)
  68. {
  69. return false;
  70. }
  71. if (!string.Equals(fragmentsFilter[i], fragmentsTopic[i], StringComparison.Ordinal))
  72. {
  73. return false;
  74. }
  75. }
  76. return fragmentsTopic.Length == fragmentsFilter.Length;
  77. }
  78. }
  79. }