Danmu_Main_mq *mq.Msgq //消息
ReqPool *pool.Buf[reqf.Req] //请求池
SerF *web.WebPath //web服务处理
+ StartT time.Time //启动时间
}
type LiveQn struct {
func (t *Common) Init() Common {
t.PID = os.Getpid()
+ t.StartT = time.Now()
t.AllStreamType = map[string]StreamType{
`fmp4`: {
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)
}
### 说明
本项目使用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中的配置项。
使用此配置,可以在有新配置项时,默认使用新配置项而保持之前其他的配置。