MultiSourceBenchmarks.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using BenchmarkDotNet.Attributes;
  2. using Apq.Cfg.Ini;
  3. using Apq.Cfg.Xml;
  4. using Apq.Cfg.Yaml;
  5. using Apq.Cfg.Toml;
  6. namespace Apq.Cfg.Benchmarks;
  7. /// <summary>
  8. /// 多源合并性能基准测试
  9. /// 测试多个配置源叠加时的查询性能
  10. /// </summary>
  11. [Config(typeof(BenchmarkConfig))]
  12. public class MultiSourceBenchmarks : IDisposable
  13. {
  14. private readonly string _testDir;
  15. private ICfgRoot _cfg = null!;
  16. [Params(3)]
  17. public int SourceCount { get; set; }
  18. public MultiSourceBenchmarks()
  19. {
  20. _testDir = Path.Combine(Path.GetTempPath(), $"ApqCfgBench_{Guid.NewGuid():N}");
  21. Directory.CreateDirectory(_testDir);
  22. }
  23. [GlobalSetup]
  24. public void Setup()
  25. {
  26. var builder = new CfgBuilder();
  27. // 根据 SourceCount 添加不同数量的配置源
  28. for (int i = 0; i < SourceCount; i++)
  29. {
  30. var jsonPath = Path.Combine(_testDir, $"config{i}.json");
  31. var content = $$"""
  32. {
  33. "Source{{i}}": {
  34. "Name": "Source{{i}}",
  35. "Level": {{i}}
  36. },
  37. "Shared": {
  38. "Key": "ValueFromSource{{i}}",
  39. "Priority": {{i}}
  40. },
  41. "Data": {
  42. "Key1": "Value1_Source{{i}}",
  43. "Key2": "Value2_Source{{i}}",
  44. "Key3": "Value3_Source{{i}}",
  45. "Key4": "Value4_Source{{i}}",
  46. "Key5": "Value5_Source{{i}}"
  47. }
  48. }
  49. """;
  50. File.WriteAllText(jsonPath, content);
  51. builder.AddJson(jsonPath, level: i, writeable: i == 0, isPrimaryWriter: i == 0);
  52. }
  53. _cfg = builder.Build();
  54. }
  55. [GlobalCleanup]
  56. public void Cleanup()
  57. {
  58. Dispose();
  59. }
  60. public void Dispose()
  61. {
  62. _cfg?.Dispose();
  63. if (Directory.Exists(_testDir))
  64. {
  65. Directory.Delete(_testDir, true);
  66. }
  67. }
  68. #region 读取性能测试
  69. /// <summary>
  70. /// 读取最高优先级源的键
  71. /// </summary>
  72. [Benchmark(Baseline = true)]
  73. [BenchmarkCategory("Read")]
  74. public string? Read_HighPriorityKey()
  75. {
  76. return _cfg.Get("Source0:Name");
  77. }
  78. /// <summary>
  79. /// 读取最低优先级源的键
  80. /// </summary>
  81. [Benchmark]
  82. [BenchmarkCategory("Read")]
  83. public string? Read_LowPriorityKey()
  84. {
  85. var key = $"Source{SourceCount - 1}:Name";
  86. return _cfg.Get(key);
  87. }
  88. /// <summary>
  89. /// 读取被覆盖的共享键(测试优先级合并)
  90. /// </summary>
  91. [Benchmark]
  92. [BenchmarkCategory("Read")]
  93. public string? Read_SharedKey()
  94. {
  95. return _cfg.Get("Shared:Key");
  96. }
  97. /// <summary>
  98. /// 批量读取多个键
  99. /// </summary>
  100. [Benchmark]
  101. [BenchmarkCategory("Read")]
  102. public void Read_MultipleKeys()
  103. {
  104. for (int i = 0; i < 100; i++)
  105. {
  106. _ = _cfg.Get("Data:Key1");
  107. _ = _cfg.Get("Data:Key2");
  108. _ = _cfg.Get("Data:Key3");
  109. _ = _cfg.Get("Data:Key4");
  110. _ = _cfg.Get("Data:Key5");
  111. }
  112. }
  113. #endregion
  114. #region Exists 性能测试
  115. [Benchmark]
  116. [BenchmarkCategory("Exists")]
  117. public bool Exists_HighPriorityKey()
  118. {
  119. return _cfg.Exists("Source0:Name");
  120. }
  121. [Benchmark]
  122. [BenchmarkCategory("Exists")]
  123. public bool Exists_LowPriorityKey()
  124. {
  125. var key = $"Source{SourceCount - 1}:Name";
  126. return _cfg.Exists(key);
  127. }
  128. [Benchmark]
  129. [BenchmarkCategory("Exists")]
  130. public bool Exists_NonExistentKey()
  131. {
  132. return _cfg.Exists("NonExistent:Key");
  133. }
  134. [Benchmark]
  135. [BenchmarkCategory("Exists")]
  136. public void Exists_MultipleKeys()
  137. {
  138. for (int i = 0; i < 100; i++)
  139. {
  140. _ = _cfg.Exists("Data:Key1");
  141. _ = _cfg.Exists("Data:Key2");
  142. _ = _cfg.Exists("NonExistent:Key");
  143. }
  144. }
  145. #endregion
  146. #region 写入性能测试
  147. [Benchmark]
  148. [BenchmarkCategory("Write")]
  149. public void Write_NewKey()
  150. {
  151. for (int i = 0; i < 100; i++)
  152. {
  153. _cfg.Set($"NewData:Key{i}", $"Value{i}");
  154. }
  155. }
  156. [Benchmark]
  157. [BenchmarkCategory("Write")]
  158. public void Write_OverrideKey()
  159. {
  160. for (int i = 0; i < 100; i++)
  161. {
  162. _cfg.Set("Shared:Key", $"NewValue{i}");
  163. }
  164. }
  165. #endregion
  166. #region 类型转换性能测试
  167. [Benchmark]
  168. [BenchmarkCategory("TypeConversion")]
  169. public int Get_Int()
  170. {
  171. return _cfg.Get<int>("Shared:Priority");
  172. }
  173. [Benchmark]
  174. [BenchmarkCategory("TypeConversion")]
  175. public void Get_Int_Multiple()
  176. {
  177. for (int i = 0; i < 100; i++)
  178. {
  179. _ = _cfg.Get<int>("Shared:Priority");
  180. }
  181. }
  182. #endregion
  183. }