optimise route perf tracking

expand route perf tracking to more routes
This commit is contained in:
Azareal 2020-02-29 11:10:39 +10:00
parent 03862d1b09
commit 46732286e5
6 changed files with 100 additions and 214 deletions

View File

@ -44,7 +44,8 @@ type Header struct {
GoogSiteVerify string
IsoCode string
LooseCSP bool
StartedAt time.Time
//StartedAt time.Time
StartedAt int64
Elapsed1 string
Writer http.ResponseWriter
ExtData ExtData

View File

@ -13,6 +13,7 @@ import (
"time"
"github.com/Azareal/Gosora/common/phrases"
"github.com/Azareal/Gosora/uutils"
)
// nolint
@ -26,6 +27,7 @@ var SimpleForumUserCheck func(w http.ResponseWriter, r *http.Request, user *User
var ForumUserCheck func(header *Header, w http.ResponseWriter, r *http.Request, user *User, fid int) (err RouteError) = forumUserCheck
var SimpleUserCheck func(w http.ResponseWriter, r *http.Request, user *User) (headerLite *HeaderLite, err RouteError) = simpleUserCheck
var UserCheck func(w http.ResponseWriter, r *http.Request, user *User) (header *Header, err RouteError) = userCheck
var UserCheckNano func(w http.ResponseWriter, r *http.Request, user *User, nano int64) (header *Header, err RouteError) = userCheck2
func simpleForumUserCheck(w http.ResponseWriter, r *http.Request, user *User, fid int) (header *HeaderLite, rerr RouteError) {
header, rerr = SimpleUserCheck(w, r, user)
@ -110,7 +112,8 @@ func panelUserCheck(w http.ResponseWriter, r *http.Request, user *User) (h *Head
Zone: "panel",
Writer: w,
IsoCode: phrases.GetLangPack().IsoCode,
StartedAt: time.Now(),
//StartedAt: time.Now(),
StartedAt: uutils.Nanotime(),
}
// TODO: We should probably initialise header.ExtData
// ? - Should we only show this in debug mode? It might be useful for detecting issues in production, if we show it there as-well
@ -198,9 +201,13 @@ func GetThemeByReq(r *http.Request) *Theme {
return theme
}
func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, rerr RouteError) {
return userCheck2(w,r,user,uutils.Nanotime())
}
// TODO: Add the ability for admins to restrict certain themes to certain groups?
// ! Be careful about firing errors off here as CustomError uses this
func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Header, rerr RouteError) {
func userCheck2(w http.ResponseWriter, r *http.Request, user *User, nano int64) (header *Header, rerr RouteError) {
theme := GetThemeByReq(r)
header = &Header{
Site: Site,
@ -212,6 +219,7 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head
Zone: "frontend",
Writer: w,
IsoCode: phrases.GetLangPack().IsoCode,
StartedAt: nano,
}
// TODO: Optimise this by avoiding accessing a map string index
header.GoogSiteVerify = header.Settings["google_site_verify"].(string)
@ -226,7 +234,7 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (header *Head
// An optimisation so we don't populate StartedAt for users who shouldn't see the stat anyway
// ? - Should we only show this in debug mode? It might be useful for detecting issues in production, if we show it there as-well
//if user.IsAdmin {
header.StartedAt = time.Now()
//header.StartedAt = time.Now()
//}
//PrepResources(user,header,theme)

View File

@ -1104,8 +1104,10 @@ ArgLoop:
leftParam, _ := c.compileIfVarSub(con, leftOp)
// TODO: Refactor this
// TODO: Validate that this is actually a time.Time
litString("time.Since("+leftParam+").String()", false)
//litString("time.Since("+leftParam+").String()", false)
c.importMap["time"] = "time"
c.importMap["github.com/Azareal/Gosora/uutils"] = "github.com/Azareal/Gosora/uutils"
litString("time.Duration(uutils.Nanotime() - "+leftParam+").String()", false)
break ArgLoop
case "dock":
// TODO: Implement string literals properly

File diff suppressed because it is too large Load Diff

View File

@ -78,23 +78,24 @@ func main() {
//out += "\n\t\t\tid = " + strconv.Itoa(allRouteMap[route.Name])
out += runBefore(route.RunBefore, 4)
if !route.Action && !route.NoHead {
out += "\n\t\t\th, err := c.UserCheck(w,req,&user)"
//out += "\n\t\t\th, err := c.UserCheck(w,req,&user)"
out += "\n\t\t\th, err := c.UserCheckNano(w,req,&user,cn)"
out += "\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}"
vcpy := route.Vars
route.Vars = []string{"h"}
route.Vars = append(route.Vars, vcpy...)
} else if route.Name != "common.RouteWebsockets" {
}/* else if route.Name != "common.RouteWebsockets" {
//out += "\n\t\t\tsa := time.Now()"
out += "\n\t\t\tcn := uutils.Nanotime()"
}
//out += "\n\t\t\tcn := uutils.Nanotime()"
}*/
out += "\n\t\t\terr = " + strings.Replace(route.Name, "common.", "c.", -1) + "(w,req,user"
for _, item := range route.Vars {
out += "," + item
}
out += `)`
if !route.Action && !route.NoHead {
/*if !route.Action && !route.NoHead {
out += "\n\t\t\tco.RouteViewCounter.Bump2(" + strconv.Itoa(allRouteMap[route.Name]) + ", h.StartedAt)"
} else if route.Name != "common.RouteWebsockets" {
} else */if route.Name != "common.RouteWebsockets" {
//out += "\n\t\t\tco.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")"
//out += "\n\t\t\tco.RouteViewCounter.Bump2(" + strconv.Itoa(allRouteMap[route.Name]) + ", sa)"
out += "\n\t\t\tco.RouteViewCounter.Bump3(" + strconv.Itoa(allRouteMap[route.Name]) + ", cn)"
@ -149,25 +150,26 @@ func main() {
}
}
if !route.Action && !route.NoHead && !group.NoHead {
out += "\n\t\t\t\th, err := c.UserCheck(w,req,&user)"
//out += "\n\t\t\t\th, err := c.UserCheck(w,req,&user)"
out += "\n\t\t\t\th, err := c.UserCheckNano(w,req,&user,cn)"
out += "\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}"
vcpy := route.Vars
route.Vars = []string{"h"}
route.Vars = append(route.Vars, vcpy...)
} else {
out += "\n\t\t\t\t\tcn := uutils.Nanotime()"
//out += "\n\t\t\t\t\tcn := uutils.Nanotime()"
}
out += "\n\t\t\t\t\terr = " + strings.Replace(route.Name, "common.", "c.", -1) + "(w,req,user"
for _, item := range route.Vars {
out += "," + item
}
out += ")"
if !route.Action && !route.NoHead && !group.NoHead {
/*if !route.Action && !route.NoHead && !group.NoHead {
out += "\n\t\t\t\t\tco.RouteViewCounter.Bump2(" + strconv.Itoa(allRouteMap[route.Name]) + ", h.StartedAt)"
} else {
} else {*/
//out += "\n\t\t\t\t\tco.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[route.Name]) + ")"
out += "\n\t\t\t\t\tco.RouteViewCounter.Bump3(" + strconv.Itoa(allRouteMap[route.Name]) + ", cn)"
}
//}
}
if defaultRoute.Name != "" {
@ -176,7 +178,8 @@ func main() {
//out += "\n\t\t\t\t\tid = " + strconv.Itoa(allRouteMap[defaultRoute.Name])
out += runBefore(defaultRoute.RunBefore, 4)
if !defaultRoute.Action && !defaultRoute.NoHead && !group.NoHead {
out += "\n\t\t\t\t\th, err := c.UserCheck(w,req,&user)"
//out += "\n\t\t\t\t\th, err := c.UserCheck(w,req,&user)"
out += "\n\t\t\t\t\th, err := c.UserCheckNano(w,req,&user,cn)"
out += "\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}"
vcpy := defaultRoute.Vars
defaultRoute.Vars = []string{"h"}
@ -187,11 +190,12 @@ func main() {
out += ", " + item
}
out += ")"
if !defaultRoute.Action && !defaultRoute.NoHead && !group.NoHead {
/*if !defaultRoute.Action && !defaultRoute.NoHead && !group.NoHead {
out += "\n\t\t\t\t\tco.RouteViewCounter.Bump2(" + strconv.Itoa(allRouteMap[defaultRoute.Name]) + ", h.StartedAt)"
} else {
out += "\n\t\t\t\t\tco.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[defaultRoute.Name]) + ")"
}
} else {*/
//out += "\n\t\t\t\t\tco.RouteViewCounter.Bump(" + strconv.Itoa(allRouteMap[defaultRoute.Name]) + ")"
out += "\n\t\t\tco.RouteViewCounter.Bump3(" + strconv.Itoa(allRouteMap[defaultRoute.Name]) + ", cn)"
//}
}
out += `
}`
@ -849,6 +853,7 @@ func (r *GenRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
func (r *GenRouter) routeSwitch(w http.ResponseWriter, req *http.Request, user c.User, prefix, extraData string) /*(id int, orerr */c.RouteError/*)*/ {
var err c.RouteError
cn := uutils.Nanotime()
switch(prefix) {` + out + `
/*case "/sitemaps": // TODO: Count these views
req.URL.Path += extraData

View File

@ -9,6 +9,7 @@ import (
c "github.com/Azareal/Gosora/common"
co "github.com/Azareal/Gosora/common/counters"
"github.com/Azareal/Gosora/uutils"
)
var successJSONBytes = []byte(`{"success":1}`)
@ -136,7 +137,7 @@ func renderTemplate3(tmplName, hookName string, w http.ResponseWriter, r *http.R
FootHeaders(w, h)
if h.Zone != "error" {
since := time.Since(h.StartedAt)
since := time.Duration(uutils.Nanotime() - h.StartedAt)
//if h.CurrentUser.IsAdmin {
h.Elapsed1 = since.String()
//}