Compare commits

...

3 Commits

Author SHA1 Message Date
Pavel Shevaev 7c0133a29d Adding initial tests 2022-11-01 13:22:49 +03:00
Pavel Shevaev cffe643a51 Removing obsolete comment 2022-10-21 10:49:54 +03:00
Pavel Shevaev 9210671236 Adding support for custom formatter and showing the tracked object pointer 2022-10-20 12:05:05 +03:00
5 changed files with 50 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tags

2
go.mod
View File

@ -1,3 +1,5 @@
module git.bit5.ru/backend/res_tracker module git.bit5.ru/backend/res_tracker
go 1.13 go 1.13
require github.com/stretchr/testify v1.8.1

17
go.sum Normal file
View File

@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -22,6 +22,7 @@ const FULL = 2
var mode int32 = OFF var mode int32 = OFF
var res2track = &sync.Map{} var res2track = &sync.Map{}
var customFormatter = func(item *TrackReportItem) string { return "," }
type TrackedInfo struct { type TrackedInfo struct {
Time time.Time Time time.Time
@ -29,7 +30,7 @@ type TrackedInfo struct {
} }
type TrackReportItem struct { type TrackReportItem struct {
T reflect.Type Obj interface{}
Duration time.Duration Duration time.Duration
Stack string Stack string
} }
@ -42,6 +43,10 @@ func InitHandlers() {
http.HandleFunc("/res_tracker/status", statusHandler) http.HandleFunc("/res_tracker/status", statusHandler)
} }
func SetCustomFormatter(f func(item *TrackReportItem) string) {
customFormatter = f
}
func onHandler(w http.ResponseWriter, r *http.Request) { func onHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
m := query.Get("m") m := query.Get("m")
@ -135,7 +140,6 @@ func FormatReportItems() []TrackReportItem {
k := snapshot[i] k := snapshot[i]
v := snapshot[i+1] v := snapshot[i+1]
t := reflect.TypeOf(k)
info, _ := v.(TrackedInfo) info, _ := v.(TrackedInfo)
stackStr := string(info.Stack) stackStr := string(info.Stack)
@ -160,7 +164,7 @@ func FormatReportItems() []TrackReportItem {
} }
} }
items = append(items, TrackReportItem{T: t, Duration: time.Now().Sub(info.Time), Stack: stackStr}) items = append(items, TrackReportItem{Obj: k, Duration: time.Now().Sub(info.Time), Stack: stackStr})
} }
//3. let's sort items by duration descending //3. let's sort items by duration descending
@ -180,8 +184,7 @@ func ReportLines() []string {
lines = append(lines, fmt.Sprintf("=== Tracked connections report %s (total: %d) ===", now.Format("01-02-2006 15:04:05"), len(items))) lines = append(lines, fmt.Sprintf("=== Tracked connections report %s (total: %d) ===", now.Format("01-02-2006 15:04:05"), len(items)))
for idx, item := range items { for idx, item := range items {
//TODO: obtain somehow an actual pointer of the object or provide a hook for a custom formatter lines = append(lines, fmt.Sprintf("#%d Tracked (%v %p%s duration %v) : %s", idx+1, reflect.TypeOf(item.Obj), item.Obj, customFormatter(&item), item.Duration, item.Stack))
lines = append(lines, fmt.Sprintf("#%d Tracked (%v, duration %v) : %s", idx+1, item.T, item.Duration, item.Stack))
} }
return lines return lines
} }

22
res_tracker_test.go Normal file
View File

@ -0,0 +1,22 @@
package res_tracker_test
import (
"testing"
"git.bit5.ru/backend/res_tracker"
"github.com/stretchr/testify/assert"
)
func TestSimpleTrack(t *testing.T) {
var foo interface{}
res_tracker.Enable(res_tracker.LITE)
defer res_tracker.Enable(res_tracker.OFF)
assert.Equal(t, 0, res_tracker.Count())
res_tracker.Track(foo)
assert.Equal(t, 1, res_tracker.Count())
res_tracker.Untrack(foo)
assert.Equal(t, 0, res_tracker.Count())
}