Skip to content

Commit ed9fffb

Browse files
committed
feat: add graphical visualization
1 parent b6b9196 commit ed9fffb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"runtime/debug"
88
"strconv"
99

10+
"github.com/arl/statsviz"
11+
example "github.com/arl/statsviz/_example"
1012
"github.com/docker/go-units"
1113
"github.com/gin-gonic/gin"
1214
)
@@ -22,10 +24,14 @@ func main() {
2224
}
2325

2426
func SetUpRouter(withRoutes bool) *gin.Engine {
27+
// Force the GC to work to make the plots "move".
28+
go example.Work()
29+
2530
r := gin.New()
2631
if withRoutes {
2732
r.GET("/config", readConfigHandler)
2833
r.POST("/config", updateConfigHandler)
34+
r.GET("/debug/statsviz/*filepath", statsvizHandler)
2935
}
3036
return r
3137
}
@@ -75,3 +81,11 @@ func updateConfigHandler(c *gin.Context) {
7581

7682
c.IndentedJSON(http.StatusOK, req)
7783
}
84+
85+
func statsvizHandler(c *gin.Context) {
86+
if c.Param("filepath") == "/ws" {
87+
statsviz.Ws(c.Writer, c.Request)
88+
return
89+
}
90+
statsviz.IndexAtRoot("/debug/statsviz").ServeHTTP(c.Writer, c.Request)
91+
}

main_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,22 @@ func TestUpdateConfigHandler(t *testing.T) {
132132
t.Errorf("TestUpdateConfigHandler: want %q, got %q", test.want, got)
133133
}
134134
}
135+
136+
func TestStatsvizHandler(t *testing.T) {
137+
r := SetUpRouter(false)
138+
r.GET("/debug/statsviz/*action", statsvizHandler)
139+
140+
req, _ := http.NewRequest("GET", "/debug/statsviz/", nil)
141+
w := httptest.NewRecorder()
142+
143+
r.ServeHTTP(w, req)
144+
145+
statusOK := w.Code == http.StatusOK
146+
147+
p := w.Body.Bytes()
148+
pageOK := strings.Index(string(p), "<title>Statsviz</title>") > 0
149+
150+
if !(statusOK && pageOK) {
151+
t.Errorf("TestStatsvizHandler: want %d, got %d", http.StatusOK, w.Code)
152+
}
153+
}

0 commit comments

Comments
 (0)