DependencyInjectionBenchmarks.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using BenchmarkDotNet.Attributes;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Options;
  4. namespace Apq.Cfg.Benchmarks;
  5. /// <summary>
  6. /// 依赖注入集成性能测试
  7. /// </summary>
  8. [MemoryDiagnoser]
  9. [RankColumn]
  10. public class DependencyInjectionBenchmarks
  11. {
  12. private string _testDir = null!;
  13. private string _jsonPath = null!;
  14. private IServiceProvider _provider = null!;
  15. [GlobalSetup]
  16. public void Setup()
  17. {
  18. _testDir = Path.Combine(Path.GetTempPath(), $"ApqCfgDIBench_{Guid.NewGuid():N}");
  19. Directory.CreateDirectory(_testDir);
  20. _jsonPath = Path.Combine(_testDir, "config.json");
  21. File.WriteAllText(_jsonPath, """
  22. {
  23. "Database": {
  24. "Host": "localhost",
  25. "Port": 5432,
  26. "Name": "testdb"
  27. },
  28. "Logging": {
  29. "Level": "Warning",
  30. "Format": "json"
  31. },
  32. "App": {
  33. "Name": "BenchmarkApp",
  34. "Version": "1.0.0",
  35. "Debug": false,
  36. "Endpoints": {
  37. "0": {
  38. "Host": "api1.local",
  39. "Port": 8001
  40. },
  41. "1": {
  42. "Host": "api2.local",
  43. "Port": 8002
  44. }
  45. }
  46. }
  47. }
  48. """);
  49. // 预构建一个 ServiceProvider 用于解析测试
  50. var services = new ServiceCollection();
  51. services.AddApqCfg(cfg => cfg
  52. .AddJson(_jsonPath, level: 0, writeable: false));
  53. services.ConfigureApqCfg<DatabaseOptions>("Database");
  54. services.ConfigureApqCfg<LoggingOptions>("Logging");
  55. services.ConfigureApqCfg<AppOptions>("App");
  56. _provider = services.BuildServiceProvider();
  57. }
  58. [GlobalCleanup]
  59. public void Cleanup()
  60. {
  61. (_provider as IDisposable)?.Dispose();
  62. if (Directory.Exists(_testDir))
  63. {
  64. try { Directory.Delete(_testDir, true); }
  65. catch { }
  66. }
  67. }
  68. // ========== AddApqCfg 注册性能 ==========
  69. [Benchmark(Description = "AddApqCfg_Register")]
  70. public IServiceProvider AddApqCfg_Register()
  71. {
  72. var services = new ServiceCollection();
  73. services.AddApqCfg(cfg => cfg
  74. .AddJson(_jsonPath, level: 0, writeable: false));
  75. var provider = services.BuildServiceProvider();
  76. (provider as IDisposable)?.Dispose();
  77. return provider;
  78. }
  79. [Benchmark(Description = "AddApqCfg_WithOptions_Register")]
  80. public IServiceProvider AddApqCfg_WithOptions_Register()
  81. {
  82. var services = new ServiceCollection();
  83. services.AddApqCfg<DatabaseOptions>(
  84. cfg => cfg.AddJson(_jsonPath, level: 0, writeable: false),
  85. sectionKey: "Database");
  86. var provider = services.BuildServiceProvider();
  87. (provider as IDisposable)?.Dispose();
  88. return provider;
  89. }
  90. // ========== ConfigureApqCfg 注册性能 ==========
  91. [Benchmark(Description = "ConfigureApqCfg_Single")]
  92. public IServiceProvider ConfigureApqCfg_Single()
  93. {
  94. var services = new ServiceCollection();
  95. services.AddApqCfg(cfg => cfg
  96. .AddJson(_jsonPath, level: 0, writeable: false));
  97. services.ConfigureApqCfg<DatabaseOptions>("Database");
  98. var provider = services.BuildServiceProvider();
  99. (provider as IDisposable)?.Dispose();
  100. return provider;
  101. }
  102. [Benchmark(Description = "ConfigureApqCfg_Multiple")]
  103. public IServiceProvider ConfigureApqCfg_Multiple()
  104. {
  105. var services = new ServiceCollection();
  106. services.AddApqCfg(cfg => cfg
  107. .AddJson(_jsonPath, level: 0, writeable: false));
  108. services.ConfigureApqCfg<DatabaseOptions>("Database");
  109. services.ConfigureApqCfg<LoggingOptions>("Logging");
  110. services.ConfigureApqCfg<AppOptions>("App");
  111. var provider = services.BuildServiceProvider();
  112. (provider as IDisposable)?.Dispose();
  113. return provider;
  114. }
  115. // ========== IOptions<T> 解析性能 ==========
  116. [Benchmark(Description = "Resolve_IOptions")]
  117. public DatabaseOptions Resolve_IOptions()
  118. {
  119. return _provider.GetRequiredService<IOptions<DatabaseOptions>>().Value;
  120. }
  121. [Benchmark(Description = "Resolve_IOptions_100")]
  122. public DatabaseOptions Resolve_IOptions_100()
  123. {
  124. DatabaseOptions result = null!;
  125. for (int i = 0; i < 100; i++)
  126. {
  127. result = _provider.GetRequiredService<IOptions<DatabaseOptions>>().Value;
  128. }
  129. return result;
  130. }
  131. // ========== IOptionsMonitor<T> 解析性能 ==========
  132. [Benchmark(Description = "Resolve_IOptionsMonitor")]
  133. public DatabaseOptions Resolve_IOptionsMonitor()
  134. {
  135. return _provider.GetRequiredService<IOptionsMonitor<DatabaseOptions>>().CurrentValue;
  136. }
  137. [Benchmark(Description = "Resolve_IOptionsMonitor_100")]
  138. public DatabaseOptions Resolve_IOptionsMonitor_100()
  139. {
  140. DatabaseOptions result = null!;
  141. var monitor = _provider.GetRequiredService<IOptionsMonitor<DatabaseOptions>>();
  142. for (int i = 0; i < 100; i++)
  143. {
  144. result = monitor.CurrentValue;
  145. }
  146. return result;
  147. }
  148. // ========== IOptionsSnapshot<T> 解析性能 ==========
  149. [Benchmark(Description = "Resolve_IOptionsSnapshot")]
  150. public DatabaseOptions Resolve_IOptionsSnapshot()
  151. {
  152. using var scope = _provider.CreateScope();
  153. return scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<DatabaseOptions>>().Value;
  154. }
  155. [Benchmark(Description = "Resolve_IOptionsSnapshot_100")]
  156. public DatabaseOptions Resolve_IOptionsSnapshot_100()
  157. {
  158. DatabaseOptions result = null!;
  159. for (int i = 0; i < 100; i++)
  160. {
  161. using var scope = _provider.CreateScope();
  162. result = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<DatabaseOptions>>().Value;
  163. }
  164. return result;
  165. }
  166. // ========== ICfgRoot 解析性能 ==========
  167. [Benchmark(Description = "Resolve_ICfgRoot")]
  168. public ICfgRoot Resolve_ICfgRoot()
  169. {
  170. return _provider.GetRequiredService<ICfgRoot>();
  171. }
  172. [Benchmark(Description = "Resolve_ICfgRoot_ThenGet")]
  173. public string? Resolve_ICfgRoot_ThenGet()
  174. {
  175. var cfg = _provider.GetRequiredService<ICfgRoot>();
  176. return cfg.Get("Database:Host");
  177. }
  178. // ========== 复杂对象解析性能 ==========
  179. [Benchmark(Description = "Resolve_ComplexOptions")]
  180. public AppOptions Resolve_ComplexOptions()
  181. {
  182. return _provider.GetRequiredService<IOptions<AppOptions>>().Value;
  183. }
  184. // ========== 多选项解析性能 ==========
  185. [Benchmark(Description = "Resolve_MultipleOptions")]
  186. public (DatabaseOptions, LoggingOptions, AppOptions) Resolve_MultipleOptions()
  187. {
  188. var db = _provider.GetRequiredService<IOptions<DatabaseOptions>>().Value;
  189. var log = _provider.GetRequiredService<IOptions<LoggingOptions>>().Value;
  190. var app = _provider.GetRequiredService<IOptions<AppOptions>>().Value;
  191. return (db, log, app);
  192. }
  193. // ========== 测试用选项类 ==========
  194. public class DatabaseOptions
  195. {
  196. public string? Host { get; set; }
  197. public int Port { get; set; }
  198. public string? Name { get; set; }
  199. }
  200. public class LoggingOptions
  201. {
  202. public string? Level { get; set; }
  203. public string? Format { get; set; }
  204. }
  205. public class AppOptions
  206. {
  207. public string? Name { get; set; }
  208. public string? Version { get; set; }
  209. public bool Debug { get; set; }
  210. public List<EndpointOptions>? Endpoints { get; set; }
  211. }
  212. public class EndpointOptions
  213. {
  214. public string? Host { get; set; }
  215. public int Port { get; set; }
  216. }
  217. }