From 1c63643dfe01a4a016b16503e1e4641e7ca2be82 Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sat, 11 Mar 2023 16:06:56 +0800 Subject: [PATCH] =?utf8?q?Add=20=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?utf8?q?=E6=B7=BB=E5=8A=A0=20=E8=BF=90=E8=A1=8C=E6=97=B6=E6=80=A7?= =?utf8?q?=E8=83=BD=E8=B0=83=E8=AF=95=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Var.go | 56 ++++++++++++++++++++++--------------- README.md | 43 ++++++++++++++++++++++++++++ demo/config/config_K_v.json | 3 +- 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index 12900be..405becc 100644 --- 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) } diff --git a/README.md b/README.md index df1afc0..6925b9c 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,49 @@ ### 说明 本项目使用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中的配置项。 使用此配置,可以在有新配置项时,默认使用新配置项而保持之前其他的配置。 diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index 334b9b7..f3a8385 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -105,5 +105,6 @@ "几秒后重载":60, "下播后不记录人气观看人数":true, "debug模式-help":"在/debug/pprof/启用调试", - "debug模式":false + "性能路径-help":"当Web服务地址不为空时, 访问此路径可以获取性能信息,为空时关闭", + "性能路径":"/state" } \ No newline at end of file -- 2.39.2