Browse Source

更新运行时监控库,开启 debug 后,在 /debug/statsviz/ url查看

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
1ab2e3f274
4 changed files with 19 additions and 12 deletions
  1. 0 12
      cmd/chinesesubfinder/main.go
  2. 2 0
      go.mod
  3. 3 0
      go.sum
  4. 14 0
      internal/backend/base_router.go

+ 0 - 12
cmd/chinesesubfinder/main.go

@@ -3,8 +3,6 @@ package main
 import (
 	"flag"
 	"fmt"
-	"net/http"
-	_ "net/http/pprof"
 	"path/filepath"
 	"strings"
 	"time"
@@ -136,16 +134,6 @@ func main() {
 	} else {
 		loggerBase.Infoln("Speed Dev Mode is Off")
 	}
-	if settings.Get().AdvancedSettings.DebugMode == true {
-		// 如果是 DebugMode 那么开启性能监控
-		go func() {
-			// 开启pprof,监听请求
-			ip := "0.0.0.0:8080"
-			if err := http.ListenAndServe(ip, nil); err != nil {
-				fmt.Printf("start pprof failed on %s\n", ip)
-			}
-		}()
-	}
 	// ------------------------------------------------------------------------
 	// 前置的任务,热修复、字幕修改文件名格式、提前下载好浏览器
 	if settings.Get().SpeedDevMode == false {

+ 2 - 0
go.mod

@@ -85,6 +85,7 @@ require (
 	github.com/acomagu/bufpipe v1.0.3 // indirect
 	github.com/andybalholm/brotli v1.0.0 // indirect
 	github.com/andybalholm/cascadia v1.2.0 // indirect
+	github.com/arl/statsviz v0.5.1 // indirect
 	github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
 	github.com/bodgit/plumbing v1.1.0 // indirect
 	github.com/bodgit/windows v1.0.0 // indirect
@@ -101,6 +102,7 @@ require (
 	github.com/golang/protobuf v1.5.2 // indirect
 	github.com/golang/snappy v0.0.4 // indirect
 	github.com/google/go-cmp v0.5.6 // indirect
+	github.com/gorilla/websocket v1.4.2 // indirect
 	github.com/hashicorp/errwrap v1.0.0 // indirect
 	github.com/hashicorp/go-multierror v1.1.0 // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect

+ 3 - 0
go.sum

@@ -73,6 +73,8 @@ github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb
 github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
 github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA=
 github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw=
+github.com/arl/statsviz v0.5.1 h1:3HY0ZEB738JtguWsD1Tf1pFJZiCcWUmYRq/3OTYKaSI=
+github.com/arl/statsviz v0.5.1/go.mod h1:zDnjgRblGm1Dyd7J5YlbH7gM1/+HRC+SfkhZhQb5AnM=
 github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
 github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
 github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 h1:BUAU3CGlLvorLI26FmByPp2eC2qla6E1Tw+scpcg/to=
@@ -275,6 +277,7 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
 github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
 github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
 github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
+github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 github.com/grd/stat v0.0.0-20130623202159-138af3fd5012 h1:TVY1GBBIAAph4RWO9Y3p1wU+7n6khY1jxPKjDphzznA=
 github.com/grd/stat v0.0.0-20130623202159-138af3fd5012/go.mod h1:hHyH5N67TF4tD4PBbqMlyuIu5Lq5QwKSgNyyG31trzY=

+ 14 - 0
internal/backend/base_router.go

@@ -4,6 +4,8 @@ import (
 	"fmt"
 	"net/http"
 
+	"github.com/arl/statsviz"
+
 	"github.com/allanpk716/ChineseSubFinder/pkg/tmdb_api"
 
 	"github.com/allanpk716/ChineseSubFinder/pkg/settings"
@@ -59,6 +61,18 @@ func InitRouter(
 		router.StaticFS(nowUrl, http.Dir(path))
 	}
 	// --------------------------------------------------
+	// 性能监视
+	if settings.Get().AdvancedSettings.DebugMode == true {
+		// 如果是 DebugMode 那么开启性能监控
+		router.GET("/debug/statsviz/*filepath", func(context *gin.Context) {
+			if context.Param("filepath") == "/ws" {
+				statsviz.Ws(context.Writer, context.Request)
+				return
+			}
+			statsviz.IndexAtRoot("/debug/statsviz").ServeHTTP(context.Writer, context.Request)
+		})
+	}
+	// --------------------------------------------------
 	// 基础的路由
 	router.GET("/system-status", cbBase.SystemStatusHandler)