Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }