MultiSourceBenchmarks.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // 每层只有一个配置源,最高层级设为可写(作为该层的主写入源)
  52. var isHighestLevel = i == SourceCount - 1;
  53. builder.AddJson(jsonPath, level: i, writeable: isHighestLevel, isPrimaryWriter: isHighestLevel);
  54. }
  55. _cfg = builder.Build();
  56. }
  57. [GlobalCleanup]
  58. public void Cleanup()
  59. {
  60. Dispose();
  61. }
  62. public void Dispose()
  63. {
  64. _cfg?.Dispose();
  65. if (Directory.Exists(_testDir))
  66. {
  67. Directory.Delete(_testDir, true);
  68. }
  69. }
  70. #region 读取性能测试
  71. /// <summary>
  72. /// 读取最高优先级源的键
  73. /// </summary>
  74. [Benchmark(Baseline = true)]
  75. [BenchmarkCategory("Read")]
  76. public string? Read_HighPriorityKey()
  77. {
  78. return _cfg.Get("Source0:Name");
  79. }
  80. /// <summary>
  81. /// 读取最低优先级源的键
  82. /// </summary>
  83. [Benchmark]
  84. [BenchmarkCategory("Read")]
  85. public string? Read_LowPriorityKey()
  86. {
  87. var key = $"Source{SourceCount - 1}:Name";
  88. return _cfg.Get(key);
  89. }
  90. /// <summary>
  91. /// 读取被覆盖的共享键(测试优先级合并)
  92. /// </summary>
  93. [Benchmark]
  94. [BenchmarkCategory("Read")]
  95. public string? Read_SharedKey()
  96. {
  97. return _cfg.Get("Shared:Key");
  98. }
  99. /// <summary>
  100. /// 批量读取多个键
  101. /// </summary>
  102. [Benchmark]
  103. [BenchmarkCategory("Read")]
  104. public void Read_MultipleKeys()
  105. {
  106. for (int i = 0; i < 100; i++)
  107. {
  108. _ = _cfg.Get("Data:Key1");
  109. _ = _cfg.Get("Data:Key2");
  110. _ = _cfg.Get("Data:Key3");
  111. _ = _cfg.Get("Data:Key4");
  112. _ = _cfg.Get("Data:Key5");
  113. }
  114. }
  115. #endregion
  116. #region Exists 性能测试
  117. [Benchmark]
  118. [BenchmarkCategory("Exists")]
  119. public bool Exists_HighPriorityKey()
  120. {
  121. return _cfg.Exists("Source0:Name");
  122. }
  123. [Benchmark]
  124. [BenchmarkCategory("Exists")]
  125. public bool Exists_LowPriorityKey()
  126. {
  127. var key = $"Source{SourceCount - 1}:Name";
  128. return _cfg.Exists(key);
  129. }
  130. [Benchmark]
  131. [BenchmarkCategory("Exists")]
  132. public bool Exists_NonExistentKey()
  133. {
  134. return _cfg.Exists("NonExistent:Key");
  135. }
  136. [Benchmark]
  137. [BenchmarkCategory("Exists")]
  138. public void Exists_MultipleKeys()
  139. {
  140. for (int i = 0; i < 100; i++)
  141. {
  142. _ = _cfg.Exists("Data:Key1");
  143. _ = _cfg.Exists("Data:Key2");
  144. _ = _cfg.Exists("NonExistent:Key");
  145. }
  146. }
  147. #endregion
  148. #region 写入性能测试
  149. [Benchmark]
  150. [BenchmarkCategory("Write")]
  151. public void Write_NewKey()
  152. {
  153. for (int i = 0; i < 100; i++)
  154. {
  155. _cfg.Set($"NewData:Key{i}", $"Value{i}");
  156. }
  157. }
  158. [Benchmark]
  159. [BenchmarkCategory("Write")]
  160. public void Write_OverrideKey()
  161. {
  162. for (int i = 0; i < 100; i++)
  163. {
  164. _cfg.Set("Shared:Key", $"NewValue{i}");
  165. }
  166. }
  167. #endregion
  168. #region 类型转换性能测试
  169. [Benchmark]
  170. [BenchmarkCategory("TypeConversion")]
  171. public int Get_Int()
  172. {
  173. return _cfg.Get<int>("Shared:Priority");
  174. }
  175. [Benchmark]
  176. [BenchmarkCategory("TypeConversion")]
  177. public void Get_Int_Multiple()
  178. {
  179. for (int i = 0; i < 100; i++)
  180. {
  181. _ = _cfg.Get<int>("Shared:Priority");
  182. }
  183. }
  184. #endregion
  185. }