Compare commits

..

No commits in common. "master" and "v1.1.0" have entirely different histories.

5 changed files with 5 additions and 50 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
tags

2
go.mod
View File

@ -1,5 +1,3 @@
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
View File

@ -1,17 +0,0 @@
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,7 +22,6 @@ 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
@ -30,7 +29,7 @@ type TrackedInfo struct {
} }
type TrackReportItem struct { type TrackReportItem struct {
Obj interface{} T reflect.Type
Duration time.Duration Duration time.Duration
Stack string Stack string
} }
@ -43,10 +42,6 @@ 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")
@ -140,6 +135,7 @@ 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)
@ -164,7 +160,7 @@ func FormatReportItems() []TrackReportItem {
} }
} }
items = append(items, TrackReportItem{Obj: k, Duration: time.Now().Sub(info.Time), Stack: stackStr}) items = append(items, TrackReportItem{T: t, Duration: time.Now().Sub(info.Time), Stack: stackStr})
} }
//3. let's sort items by duration descending //3. let's sort items by duration descending
@ -184,7 +180,8 @@ 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 {
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)) //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, duration %v) : %s", idx+1, item.T, item.Duration, item.Stack))
} }
return lines return lines
} }

View File

@ -1,22 +0,0 @@
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())
}