]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 配置文件添加 运行时性能调试路径
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 11 Mar 2023 08:06:56 +0000 (16:06 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 11 Mar 2023 08:06:56 +0000 (16:06 +0800)
CV/Var.go
README.md
demo/config/config_K_v.json

index 12900be160ec63c13adffe604c35d1f7fc72291b..405becc003473cbea112343c6d6b300b97d07bbd 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -61,6 +61,7 @@ type Common struct {
        Danmu_Main_mq     *mq.Msgq              //消息
        ReqPool           *pool.Buf[reqf.Req]   //请求池
        SerF              *web.WebPath          //web服务处理
+       StartT            time.Time             //启动时间
 }
 
 type LiveQn struct {
@@ -136,6 +137,7 @@ type StreamType struct {
 
 func (t *Common) Init() Common {
        t.PID = os.Getpid()
+       t.StartT = time.Now()
 
        t.AllStreamType = map[string]StreamType{
                `fmp4`: {
@@ -236,33 +238,41 @@ func (t *Common) Init() Common {
                        Addr: serUrl.Host,
                }, t.SerF)
 
-               t.SerF.Store("/", func(w http.ResponseWriter, _ *http.Request) {
-                       var memStats runtime.MemStats
-                       runtime.ReadMemStats(&memStats)
+               if val, ok := t.K_v.LoadV("性能路径").(string); ok && val != "" {
+                       t.SerF.Store(val, func(w http.ResponseWriter, _ *http.Request) {
+                               var memStats runtime.MemStats
+                               runtime.ReadMemStats(&memStats)
 
-                       type s struct {
-                               MenInUse     string `json:"menInUse"`
-                               ReqPoolInUse int    `json:"reqPoolInUse"`
-                               ReqPoolSum   int    `json:"reqPoolSum"`
-                               NumGoroutine int    `json:"numGoroutine"`
-                               GoVersion    string `json:"goVersion"`
-                       }
-                       type j struct {
-                               Stats s `json:"stats"`
-                       }
+                               var gcAvgS float64
+
+                               if memStats.NumGC != 0 {
+                                       gcAvgS = time.Since(t.StartT).Seconds() / float64(memStats.NumGC)
+                               }
 
-                       ResStruct{0, "ok",
-                               j{
-                                       s{
-                                               humanize.Bytes(memStats.HeapInuse + memStats.StackInuse),
-                                               t.ReqPool.PoolInUse(),
-                                               t.ReqPool.PoolSum(),
-                                               runtime.NumGoroutine(),
-                                               runtime.Version(),
+                               ResStruct{0, "ok", map[string]any{
+                                       "startTime":   t.StartT.Format(time.DateTime),
+                                       "currentTime": time.Now().Format(time.DateTime),
+                                       "state": map[string]any{
+                                               "base": map[string]any{
+                                                       "reqPoolInUse": t.ReqPool.PoolInUse(),
+                                                       "reqPoolSum":   t.ReqPool.PoolSum(),
+                                                       "numGoroutine": runtime.NumGoroutine(),
+                                                       "goVersion":    runtime.Version(),
+                                               },
+                                               "mem": map[string]any{
+                                                       "memInUse": humanize.Bytes(memStats.HeapInuse + memStats.StackInuse),
+                                               },
+                                               "gc": map[string]any{
+                                                       "numGC":            memStats.NumGC,
+                                                       "lastGC":           time.UnixMicro(int64(memStats.LastGC / 1000)).Format(time.DateTime),
+                                                       "gcCPUFractionPpm": float64(int(memStats.GCCPUFraction*100000000)) / 100,
+                                                       "gcAvgS":           float64(int(gcAvgS*100)) / 100,
+                                               },
                                        },
                                },
-                       }.Write(w)
-               })
+                               }.Write(w)
+                       })
+               }
 
                t.Stream_url, _ = url.Parse(`http://` + serAdress)
        }
index df1afc0ae4c51c41a9c2f82c63f77854d7768972..6925b9cab9860dadc7e174ae045fb3153fd4bf57 100644 (file)
--- a/README.md
+++ b/README.md
 ### 说明
 本项目使用github action自动构建,构建过程详见[yml](https://github.com/qydysky/bili_danmu/blob/master/.github/workflows/go.yml)
 
+#### 性能检查
+当配置了`Web服务地址`及`性能路径`时,运行中的性能信息将可以通过http获取。  
+例如有如下配置:  
+config_K_v.json
+```json
+{
+  ...
+  "Web服务地址":"0.0.0.0:10000",
+  "性能路径":"/state"
+  ...
+}
+```
+
+此时GET http://127.0.0.1:10000/state
+```json
+{
+  "code": 0,
+  "message": "ok",
+  "data": {
+    "currentTime": "2023-03-11 15:49:06", //当前时间
+    "startTime": "2023-03-11 15:48:26",   //启动时间
+    "state": {
+      "base": {
+        "goVersion": "go1.20.1",          //编译使用的golang版本
+        "numGoroutine": 53,               //goroutine数量
+        "reqPoolInUse": 0,                //全局请求池-正在使用数量
+        "reqPoolSum": 2                   //全局请求池-总数量
+      },
+      "gc": {
+        "gcAvgS": 6.73,                   //平均gc间隔 单位秒
+        "gcCPUFractionPpm": 4.74,         //gc的STC耗时占总CPU时间比值 单位百万分之
+        "lastGC": "2023-03-11 15:49:01",  //最后gc时间
+        "numGC": 6                        //总gc数量
+      },
+      "mem": { "memInUse": "7.2 MB" }     //总使用内存
+    }
+  }
+}
+```
+
+另外,当配置文件中的`debug模式`为`true`时,标准包[net/http/pprof](https://pkg.go.dev/net/http/pprof)将在`/debug/pprof/`路径可用,从而可以使用`go tool pprof`工具进行性能调试。
+
+
 #### 自定义config_K_v.json
 当启动时使用`-ckv 路径`,将从此路径(或http地址)加载config_K_v.json并覆盖默认config_K_v.json中的配置项。
 使用此配置,可以在有新配置项时,默认使用新配置项而保持之前其他的配置。
index 334b9b71d166371f3cd0ab4ece59d4fcf6e3c4aa..f3a83852e8446f32454b47c18ea3eb21d0184312 100644 (file)
     "几秒后重载":60,
     "下播后不记录人气观看人数":true,
     "debug模式-help":"在/debug/pprof/启用调试",
-    "debug模式":false
+    "性能路径-help":"当Web服务地址不为空时, 访问此路径可以获取性能信息,为空时关闭",
+    "性能路径":"/state"
 }
\ No newline at end of file