Skip to content

Commit

Permalink
prof: simple profile some mem alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 20, 2021
1 parent 55b0d3a commit 5acbc73
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 20 deletions.
9 changes: 5 additions & 4 deletions _examples/pprof-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
// run serve:
// go run ./_examples/pprof-cli.go
// see prof on cli:
// go tool pprof rux_prof_data.prof
// go tool pprof rux_cpu.prof
// see prof on web:
// go tool pprof -http=:8080 rux_prof_data.prof
// go tool pprof -http=:8080 rux_cpu.prof
func main() {
// rux.Debug(true)
r := rux.New()
Expand All @@ -30,7 +30,7 @@ func main() {
times := 1000000
fmt.Println("start profile, run times:", times)

ruxProfile := "rux_prof_data.prof"
ruxProfile := "rux_cpu.prof"
f, err := os.Create(ruxProfile)
if err != nil {
log.Fatal(err)
Expand All @@ -51,5 +51,6 @@ func main() {
// fmt.Println(ret)
}

fmt.Println("see prof on web:\n go tool pprof -http=:8080 rux_prof_data.prof")
fmt.Println("see prof on cli:\n go tool pprof", ruxProfile)
fmt.Println("see prof on web:\n go tool pprof -http=:8080", ruxProfile)
}
37 changes: 37 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,43 @@ func TestMultiMatchAtOnce(t *testing.T) {
fmt.Println(ret)
}

/*************************************************************
* test allocs
*************************************************************/

func TestAlloc_formatPath(t *testing.T) {
r := New()
r.GET("/page/{id}", emptyHandler)
r.GET("/blog/{id}", emptyHandler)

fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
// r.formatPath("/blog/100")
r.formatPath("/blog/100/")
})))
}

func TestAlloc_match_static(t *testing.T) {
r := New()
r.GET("/page/{id}", emptyHandler)
r.GET("/blog/{id}", emptyHandler)
r.GET("/about", emptyHandler)

// output: 0 times
fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
r.match(GET, "/about")
})))
}

func TestAlloc_match_regular(t *testing.T) {
r := New()
r.GET("/page/{id}", emptyHandler)
r.GET("/blog/{id}", emptyHandler)

fmt.Println("Alloc Times:", int(testing.AllocsPerRun(100, func() {
r.match(GET, "/blog/100")
})))
}

/*************************************************************
* helper methods(ref the gin framework)
*************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestIssue_60(t *testing.T) {

route, _, _ := r.Match("GET", "/blog/100")
is.NotEmpty(route)
dump.P(route.Info())
// dump.P(route.Info())

route, _, _ = r.Match("GET", "/blog/100/")
is.NotEmpty(route)
Expand Down
19 changes: 9 additions & 10 deletions parse_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,34 +145,33 @@ func (r *Router) QuickMatch(method, path string) (route *Route, ps Params, alm [

func (r *Router) match(method, path string) (rt *Route, ps Params) {
// find in stable routes
key := method + path
if route, ok := r.stableRoutes[key]; ok {
if route, ok := r.stableRoutes[method + path]; ok {
// return r.newMatchResult(route, nil)
return route, nil
}

// find in cached routes
if r.enableCaching {
route, ok := r.cachedRoutes.Get(key)
route, ok := r.cachedRoutes.Get(method + path)
if ok {
return route, route.params
}
}

// find in regular routes
if pos := strings.IndexByte(path[1:], '/'); pos > 0 {
key = method + path[1:pos+1]
key := method + path[1:pos+1]

if rs, ok := r.regularRoutes[key]; ok {
for _, route := range rs {
if strings.Index(path, route.start) != 0 {
for i,_ := range rs {
if strings.Index(path, rs[i].start) != 0 {
continue
}

if ps, ok := route.matchRegex(path); ok {
if ps, ok := rs[i].matchRegex(path); ok {
// ret = r.newMatchResult(route, ps)
r.cacheDynamicRoute(key, ps, route)
return route, ps
r.cacheDynamicRoute(key, ps, rs[i])
return rs[i], ps
}
}
}
Expand All @@ -182,7 +181,7 @@ func (r *Router) match(method, path string) (rt *Route, ps Params) {
if rs, ok := r.irregularRoutes[method]; ok {
for _, route := range rs {
if ps, ok := route.matchRegex(path); ok {
r.cacheDynamicRoute(key, ps, route)
r.cacheDynamicRoute(method + path, ps, route)
return route, ps
}
}
Expand Down
4 changes: 2 additions & 2 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type Route struct {
// NewRoute create a new route
func NewRoute(path string, handler HandlerFunc, methods ...string) *Route {
return &Route{
path: strings.TrimSpace(path),
path: simpleFmtPath(path),
// handler
handler: handler,
methods: formatMethodsWithDefault(methods, GET),
Expand All @@ -105,7 +105,7 @@ func NamedRoute(name, path string, handler HandlerFunc, methods ...string) *Rout
func NewNamedRoute(name, path string, handler HandlerFunc, methods ...string) *Route {
return &Route{
name: strings.TrimSpace(name),
path: strings.TrimSpace(path),
path: simpleFmtPath(path),
// handler
handler: handler,
methods: formatMethodsWithDefault(methods, GET),
Expand Down
6 changes: 3 additions & 3 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Router struct {
enableCaching bool
// use encoded path for match route. default is False
useEncodedPath bool
// strict check last slash char('/'). If is True, will strict compare last '/'. default is False
// strict match last slash char('/'). If is True, will strict compare last '/'. default is False
strictLastSlash bool
// the max memory limit for multipart forms
// maxMultipartMemory int64
Expand Down Expand Up @@ -470,9 +470,9 @@ func (r *Router) formatPath(path string) string {
}

path = strings.TrimSpace(path)
// clear last '/'
// clear last slash: '/'
if !r.strictLastSlash && path[len(path)-1] == '/' {
path = strings.TrimRight(path, "/")
path = strings.TrimRight(path, "/") // TODO alloc 1 times
}

if path == "" || path == "/" {
Expand Down
10 changes: 10 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ func isFixedPath(s string) bool {
return strings.IndexByte(s, '{') < 0 && strings.IndexByte(s, '[') < 0
}

func simpleFmtPath(path string) string {
path = strings.TrimSpace(path)

if path == "" {
return "/"
}

return "/" + strings.TrimLeft(path, "/")
}

func resolveAddress(addr []string) (fullAddr string) {
ip := "0.0.0.0"
switch len(addr) {
Expand Down

0 comments on commit 5acbc73

Please sign in to comment.