Skip to content

Commit

Permalink
add support for time cost
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Nov 23, 2018
1 parent 70c6d4e commit 38b12ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
14 changes: 13 additions & 1 deletion dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package req
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
Expand Down Expand Up @@ -84,8 +85,10 @@ type dumpBuffer struct {
}

func (b *dumpBuffer) Write(p []byte) {
if b.Len() > 0 {
b.Buffer.WriteString("\r\n\r\n")
}
b.Buffer.Write(p)
b.Buffer.WriteString("\r\n\r\n")
}

func (b *dumpBuffer) WriteString(s string) {
Expand Down Expand Up @@ -189,8 +192,17 @@ func (r *Resp) dumpResponse(dump *dumpBuffer) {
}
}

// Cost return the time cost of the request
func (r *Resp) Cost() time.Duration {
return r.cost
}

// Dump dump the request
func (r *Resp) Dump() string {
dump := new(dumpBuffer)
if r.r.flag&Lcost != 0 {
dump.WriteString(fmt.Sprint(r.cost))
}
r.dumpRequest(dump)
l := dump.Len()
if l > 0 {
Expand Down
10 changes: 9 additions & 1 deletion req.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,15 @@ func (r *Req) Do(method, rawurl string, vs ...interface{}) (resp *Resp, err erro
resp.client = r.Client()
}

response, err := resp.client.Do(req)
var response *http.Response
if r.flag&Lcost != 0 {
before := time.Now()
response, err = resp.client.Do(req)
after := time.Now()
resp.cost = after.Sub(before)
} else {
response, err = resp.client.Do(req)
}
if err != nil {
return nil, err
}
Expand Down
14 changes: 12 additions & 2 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Resp struct {
req *http.Request
resp *http.Response
client *http.Client
cost time.Duration
*multipartHelper
reqBody []byte
respBody []byte
Expand Down Expand Up @@ -149,7 +150,11 @@ var regNewline = regexp.MustCompile(`\n|\r`)

func (r *Resp) autoFormat(s fmt.State) {
req := r.req
fmt.Fprint(s, req.Method, " ", req.URL.String())
if r.r.flag&Lcost != 0 {
fmt.Fprint(s, req.Method, " ", req.URL.String(), " ", r.cost)
} else {
fmt.Fprint(s, req.Method, " ", req.URL.String())
}

// test if it is should be outputed pretty
var pretty bool
Expand Down Expand Up @@ -180,7 +185,11 @@ func (r *Resp) autoFormat(s fmt.State) {

func (r *Resp) miniFormat(s fmt.State) {
req := r.req
fmt.Fprint(s, req.Method, " ", req.URL.String())
if r.r.flag&Lcost != 0 {
fmt.Fprint(s, req.Method, " ", req.URL.String(), " ", r.cost)
} else {
fmt.Fprint(s, req.Method, " ", req.URL.String())
}
if r.r.flag&LreqBody != 0 && len(r.reqBody) > 0 { // request body
str := regNewline.ReplaceAllString(string(r.reqBody), " ")
fmt.Fprint(s, " ", str)
Expand All @@ -191,6 +200,7 @@ func (r *Resp) miniFormat(s fmt.State) {
}
}

// Format fort the response
func (r *Resp) Format(s fmt.State, verb rune) {
if r == nil || r.req == nil {
return
Expand Down

0 comments on commit 38b12ac

Please sign in to comment.