performance.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package controller
  2. import (
  3. "net/http"
  4. "os"
  5. "runtime"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // PerformanceStats 性能统计信息
  10. type PerformanceStats struct {
  11. // 缓存统计
  12. CacheStats common.DiskCacheStats `json:"cache_stats"`
  13. // 系统内存统计
  14. MemoryStats MemoryStats `json:"memory_stats"`
  15. // 磁盘缓存目录信息
  16. DiskCacheInfo DiskCacheInfo `json:"disk_cache_info"`
  17. // 磁盘空间信息
  18. DiskSpaceInfo DiskSpaceInfo `json:"disk_space_info"`
  19. // 配置信息
  20. Config PerformanceConfig `json:"config"`
  21. }
  22. // MemoryStats 内存统计
  23. type MemoryStats struct {
  24. // 已分配内存(字节)
  25. Alloc uint64 `json:"alloc"`
  26. // 总分配内存(字节)
  27. TotalAlloc uint64 `json:"total_alloc"`
  28. // 系统内存(字节)
  29. Sys uint64 `json:"sys"`
  30. // GC 次数
  31. NumGC uint32 `json:"num_gc"`
  32. // Goroutine 数量
  33. NumGoroutine int `json:"num_goroutine"`
  34. }
  35. // DiskCacheInfo 磁盘缓存目录信息
  36. type DiskCacheInfo struct {
  37. // 缓存目录路径
  38. Path string `json:"path"`
  39. // 目录是否存在
  40. Exists bool `json:"exists"`
  41. // 文件数量
  42. FileCount int `json:"file_count"`
  43. // 总大小(字节)
  44. TotalSize int64 `json:"total_size"`
  45. }
  46. // DiskSpaceInfo 磁盘空间信息
  47. type DiskSpaceInfo struct {
  48. // 总空间(字节)
  49. Total uint64 `json:"total"`
  50. // 可用空间(字节)
  51. Free uint64 `json:"free"`
  52. // 已用空间(字节)
  53. Used uint64 `json:"used"`
  54. // 使用百分比
  55. UsedPercent float64 `json:"used_percent"`
  56. }
  57. // PerformanceConfig 性能配置
  58. type PerformanceConfig struct {
  59. // 是否启用磁盘缓存
  60. DiskCacheEnabled bool `json:"disk_cache_enabled"`
  61. // 磁盘缓存阈值(MB)
  62. DiskCacheThresholdMB int `json:"disk_cache_threshold_mb"`
  63. // 磁盘缓存最大大小(MB)
  64. DiskCacheMaxSizeMB int `json:"disk_cache_max_size_mb"`
  65. // 磁盘缓存路径
  66. DiskCachePath string `json:"disk_cache_path"`
  67. // 是否在容器中运行
  68. IsRunningInContainer bool `json:"is_running_in_container"`
  69. }
  70. // GetPerformanceStats 获取性能统计信息
  71. func GetPerformanceStats(c *gin.Context) {
  72. // 先同步磁盘缓存统计,确保显示准确
  73. common.SyncDiskCacheStats()
  74. // 获取缓存统计
  75. cacheStats := common.GetDiskCacheStats()
  76. // 获取内存统计
  77. var memStats runtime.MemStats
  78. runtime.ReadMemStats(&memStats)
  79. // 获取磁盘缓存目录信息
  80. diskCacheInfo := getDiskCacheInfo()
  81. // 获取配置信息
  82. diskConfig := common.GetDiskCacheConfig()
  83. config := PerformanceConfig{
  84. DiskCacheEnabled: diskConfig.Enabled,
  85. DiskCacheThresholdMB: diskConfig.ThresholdMB,
  86. DiskCacheMaxSizeMB: diskConfig.MaxSizeMB,
  87. DiskCachePath: diskConfig.Path,
  88. IsRunningInContainer: common.IsRunningInContainer(),
  89. }
  90. // 获取磁盘空间信息
  91. diskSpaceInfo := getDiskSpaceInfo()
  92. stats := PerformanceStats{
  93. CacheStats: cacheStats,
  94. MemoryStats: MemoryStats{
  95. Alloc: memStats.Alloc,
  96. TotalAlloc: memStats.TotalAlloc,
  97. Sys: memStats.Sys,
  98. NumGC: memStats.NumGC,
  99. NumGoroutine: runtime.NumGoroutine(),
  100. },
  101. DiskCacheInfo: diskCacheInfo,
  102. DiskSpaceInfo: diskSpaceInfo,
  103. Config: config,
  104. }
  105. c.JSON(http.StatusOK, gin.H{
  106. "success": true,
  107. "data": stats,
  108. })
  109. }
  110. // ClearDiskCache 清理磁盘缓存
  111. func ClearDiskCache(c *gin.Context) {
  112. // 使用统一的缓存目录
  113. dir := common.GetDiskCacheDir()
  114. // 删除缓存目录
  115. err := os.RemoveAll(dir)
  116. if err != nil && !os.IsNotExist(err) {
  117. common.ApiError(c, err)
  118. return
  119. }
  120. // 重置统计(包括命中次数和使用量)
  121. common.ResetDiskCacheStats()
  122. common.ResetDiskCacheUsage()
  123. c.JSON(http.StatusOK, gin.H{
  124. "success": true,
  125. "message": "磁盘缓存已清理",
  126. })
  127. }
  128. // ResetPerformanceStats 重置性能统计
  129. func ResetPerformanceStats(c *gin.Context) {
  130. common.ResetDiskCacheStats()
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": true,
  133. "message": "统计信息已重置",
  134. })
  135. }
  136. // ForceGC 强制执行 GC
  137. func ForceGC(c *gin.Context) {
  138. runtime.GC()
  139. c.JSON(http.StatusOK, gin.H{
  140. "success": true,
  141. "message": "GC 已执行",
  142. })
  143. }
  144. // getDiskCacheInfo 获取磁盘缓存目录信息
  145. func getDiskCacheInfo() DiskCacheInfo {
  146. // 使用统一的缓存目录
  147. dir := common.GetDiskCacheDir()
  148. info := DiskCacheInfo{
  149. Path: dir,
  150. Exists: false,
  151. }
  152. entries, err := os.ReadDir(dir)
  153. if err != nil {
  154. return info
  155. }
  156. info.Exists = true
  157. info.FileCount = 0
  158. info.TotalSize = 0
  159. for _, entry := range entries {
  160. if entry.IsDir() {
  161. continue
  162. }
  163. info.FileCount++
  164. if fileInfo, err := entry.Info(); err == nil {
  165. info.TotalSize += fileInfo.Size()
  166. }
  167. }
  168. return info
  169. }