SysCommonService.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  7. using Org.BouncyCastle.Crypto.Parameters;
  8. using Org.BouncyCastle.Utilities.Encoders;
  9. using Swashbuckle.AspNetCore.SwaggerGen;
  10. namespace Admin.NET.Core.Service;
  11. /// <summary>
  12. /// 系统通用服务 🧩
  13. /// </summary>
  14. [ApiDescriptionSettings(Order = 101)]
  15. [AllowAnonymous]
  16. public class SysCommonService : IDynamicApiController, ITransient
  17. {
  18. private readonly IApiDescriptionGroupCollectionProvider _apiProvider;
  19. private readonly SqlSugarRepository<SysUser> _sysUserRep;
  20. private readonly CDConfigOptions _cdConfigOptions;
  21. private readonly UserManager _userManager;
  22. private readonly HttpClient _httpClient;
  23. public SysCommonService(IApiDescriptionGroupCollectionProvider apiProvider,
  24. SqlSugarRepository<SysUser> sysUserRep,
  25. IOptions<CDConfigOptions> giteeOptions,
  26. IHttpClientFactory httpClientFactory,
  27. UserManager userManager)
  28. {
  29. _sysUserRep = sysUserRep;
  30. _apiProvider = apiProvider;
  31. _userManager = userManager;
  32. _cdConfigOptions = giteeOptions.Value;
  33. _httpClient = httpClientFactory.CreateClient();
  34. }
  35. /// <summary>
  36. /// 获取国密公钥私钥对 🏆
  37. /// </summary>
  38. /// <returns></returns>
  39. [DisplayName("获取国密公钥私钥对")]
  40. public SmKeyPairOutput GetSmKeyPair()
  41. {
  42. var kp = GM.GenerateKeyPair();
  43. var privateKey = Hex.ToHexString(((ECPrivateKeyParameters)kp.Private).D.ToByteArray()).ToUpper();
  44. var publicKey = Hex.ToHexString(((ECPublicKeyParameters)kp.Public).Q.GetEncoded()).ToUpper();
  45. return new SmKeyPairOutput
  46. {
  47. PrivateKey = privateKey,
  48. PublicKey = publicKey,
  49. };
  50. }
  51. /// <summary>
  52. /// 获取所有接口/动态API 🔖
  53. /// </summary>
  54. /// <returns></returns>
  55. [DisplayName("获取所有接口/动态API")]
  56. public List<ApiOutput> GetApiList()
  57. {
  58. var apiList = new List<ApiOutput>();
  59. foreach (var item in _apiProvider.ApiDescriptionGroups.Items)
  60. {
  61. foreach (var apiDescription in item.Items)
  62. {
  63. var displayName = apiDescription.TryGetMethodInfo(out MethodInfo apiMethodInfo) ? apiMethodInfo.GetCustomAttribute<DisplayNameAttribute>(true)?.DisplayName : "";
  64. apiList.Add(new ApiOutput
  65. {
  66. GroupName = item.GroupName,
  67. DisplayName = displayName,
  68. RouteName = apiDescription.RelativePath
  69. });
  70. }
  71. }
  72. return apiList;
  73. }
  74. /// <summary>
  75. /// 下载标记错误的临时Excel(全局)
  76. /// </summary>
  77. /// <returns></returns>
  78. [DisplayName("下载标记错误的临时Excel(全局)")]
  79. public async Task<IActionResult> DownloadErrorExcelTemp([FromQuery] string fileName = null)
  80. {
  81. var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
  82. var resultStream = App.GetRequiredService<SysCacheService>().Get<MemoryStream>(CacheConst.KeyExcelTemp + userId);
  83. if (resultStream == null) throw Oops.Oh("错误标记文件已过期。");
  84. return await Task.FromResult(new FileStreamResult(resultStream, "application/octet-stream")
  85. {
  86. FileDownloadName = $"{(string.IsNullOrEmpty(fileName) ? "错误标记_" + DateTime.Now.ToString("yyyyMMddhhmmss") : fileName)}.xlsx"
  87. });
  88. }
  89. /// <summary>
  90. /// 加密字符串 🔖
  91. /// </summary>
  92. /// <returns></returns>
  93. [SuppressMonitor]
  94. [DisplayName("加密字符串")]
  95. public dynamic EncryptPlainText([Required] string plainText)
  96. {
  97. return CryptogramUtil.Encrypt(plainText);
  98. }
  99. /// <summary>
  100. /// 接口压测 🔖
  101. /// </summary>
  102. /// <returns></returns>
  103. [DisplayName("接口压测")]
  104. public async Task<StressTestOutput> StressTest(StressTestInput input)
  105. {
  106. // 限制仅超管用户才能使用此功能
  107. if (!_userManager.SuperAdmin) throw Oops.Oh(ErrorCodeEnum.SA001);
  108. var stopwatch = new Stopwatch();
  109. var responseTimes = new List<double>();
  110. long totalRequests = 0, successfulRequests = 0, failedRequests = 0;
  111. stopwatch.Start();
  112. var semaphore = new SemaphoreSlim(input.MaxDegreeOfParallelism!.Value > 0 ? input.MaxDegreeOfParallelism.Value : Environment.ProcessorCount);
  113. var tasks = Enumerable.Range(0, input.NumberOfRounds!.Value * input.NumberOfRequests!.Value).Select(async _ =>
  114. {
  115. await semaphore.WaitAsync();
  116. try
  117. {
  118. var requestStopwatch = new Stopwatch();
  119. requestStopwatch.Start();
  120. // 构建完整的请求URI,包括路径参数和查询参数
  121. var uriBuilder = new UriBuilder(input.RequestUri);
  122. var queryString = HttpUtility.ParseQueryString(uriBuilder.Query);
  123. foreach (var param in input.PathParameters)
  124. {
  125. uriBuilder.Path = uriBuilder.Path.Replace($"{{{param.Key}}}", param.Value, StringComparison.OrdinalIgnoreCase);
  126. }
  127. foreach (var param in input.QueryParameters)
  128. {
  129. queryString[param.Key] = param.Value;
  130. }
  131. uriBuilder.Query = queryString.ToString() ?? string.Empty;
  132. var fullUri = uriBuilder.Uri.ToString();
  133. HttpRequestMessage request = new(input.RequestMethod!, fullUri);
  134. // 设置请求头
  135. foreach (var header in input.Headers)
  136. {
  137. request.Headers.Add(header.Key, header.Value);
  138. }
  139. if (input.RequestMethod != HttpMethod.Get && input.RequestParameters.Any())
  140. {
  141. var content = new FormUrlEncodedContent(input.RequestParameters);
  142. request.Content = content;
  143. }
  144. var response = await _httpClient.SendAsync(request);
  145. response.EnsureSuccessStatusCode(); // 抛出错误状态码异常
  146. requestStopwatch.Stop();
  147. responseTimes.Add(requestStopwatch.Elapsed.TotalMilliseconds);
  148. Interlocked.Increment(ref successfulRequests);
  149. }
  150. catch
  151. {
  152. Interlocked.Increment(ref failedRequests);
  153. }
  154. finally
  155. {
  156. Interlocked.Increment(ref totalRequests);
  157. semaphore.Release();
  158. }
  159. });
  160. await Task.WhenAll(tasks);
  161. stopwatch.Stop();
  162. var totalTimeInSeconds = stopwatch.Elapsed.TotalSeconds;
  163. var qps = totalTimeInSeconds > 0 ? totalRequests / totalTimeInSeconds : 0;
  164. var averageResponseTime = responseTimes.Any() ? responseTimes.Average() : 0;
  165. var minResponseTime = responseTimes.Any() ? responseTimes.Min() : 0;
  166. var maxResponseTime = responseTimes.Any() ? responseTimes.Max() : 0;
  167. return new StressTestOutput
  168. {
  169. TotalRequests = totalRequests,
  170. TotalTimeInSeconds = totalTimeInSeconds,
  171. SuccessfulRequests = successfulRequests,
  172. FailedRequests = failedRequests,
  173. QueriesPerSecond = qps,
  174. MinResponseTime = minResponseTime,
  175. MaxResponseTime = maxResponseTime,
  176. AverageResponseTime = averageResponseTime,
  177. Percentile10ResponseTime = CalculatePercentile(responseTimes, 0.1),
  178. Percentile25ResponseTime = CalculatePercentile(responseTimes, 0.25),
  179. Percentile50ResponseTime = CalculatePercentile(responseTimes, 0.5),
  180. Percentile75ResponseTime = CalculatePercentile(responseTimes, 0.75),
  181. Percentile90ResponseTime = CalculatePercentile(responseTimes, 0.9),
  182. Percentile99ResponseTime = CalculatePercentile(responseTimes, 0.99),
  183. Percentile999ResponseTime = CalculatePercentile(responseTimes, 0.999)
  184. };
  185. }
  186. /// <summary>
  187. /// 计算百分位请求耗时
  188. /// </summary>
  189. /// <param name="times">请求耗时列表</param>
  190. /// <param name="percentile">百分位</param>
  191. /// <returns></returns>
  192. private double CalculatePercentile(List<double> times, double percentile)
  193. {
  194. if (!times.Any()) return 0;
  195. var sortedTimes = times.OrderBy(t => t).ToList();
  196. var index = (int)Math.Ceiling(percentile * sortedTimes.Count) - 1;
  197. return sortedTimes[index < sortedTimes.Count ? index : sortedTimes.Count - 1];
  198. }
  199. }