Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can i get response time through middlerware? #25

Closed
r6c opened this issue Jun 9, 2019 · 5 comments
Closed

how can i get response time through middlerware? #25

r6c opened this issue Jun 9, 2019 · 5 comments

Comments

@r6c
Copy link

r6c commented Jun 9, 2019

i want to get response time, how can i do?

@savsgio
Copy link
Owner

savsgio commented Jun 9, 2019

Do you need the time spent in the handler, middleware or in all?.

@losingle
Copy link

losingle commented Jun 9, 2019

@savsgio Need before and after middleware

@r6c
Copy link
Author

r6c commented Jun 9, 2019

@savsgio Need before and after middleware

yes

@losingle
Copy link

losingle commented Jun 9, 2019

@r6c using master branch

// your_middleware.go
const (
	uvKey = "request.startTime"
	millisecond = float64(time.Millisecond)
)
func BeforeChain(ctx *atreugo.RequestCtx) (int, error) {
    ctx.SetUserValue(uvKey, Nanotime())
    return fasthttp.StatusOK, nil
}

func AfterChain(ctx *atreugo.RequestCtx) (int, error) {
        startTime := *(*int64)(runtimer.GetEfaceDataPtr(ctx.UserValue(uvKey)))
	duration := float64(Nanotime()-startTime) / millisecond
	return fasthttp.StatusOK, nil
}

// main.go
server.UseBefore(yourMiddleware.BeforeChain)

server.UseAfter(yourMiddleware.AfterChain)

@r6c
Copy link
Author

r6c commented Jun 10, 2019

@r6c using master branch

// your_middleware.go
const (
	uvKey = "request.startTime"
	millisecond = float64(time.Millisecond)
)
func BeforeChain(ctx *atreugo.RequestCtx) (int, error) {
    ctx.SetUserValue(uvKey, Nanotime())
    return fasthttp.StatusOK, nil
}

func AfterChain(ctx *atreugo.RequestCtx) (int, error) {
        startTime := *(*int64)(runtimer.GetEfaceDataPtr(ctx.UserValue(uvKey)))
	duration := float64(Nanotime()-startTime) / millisecond
	return fasthttp.StatusOK, nil
}

// main.go
server.UseBefore(yourMiddleware.BeforeChain)

server.UseAfter(yourMiddleware.AfterChain)

thanks

@r6c r6c closed this as completed Jun 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants