fix: unittest

This commit is contained in:
debugtalk
2022-05-05 20:40:23 +08:00
parent 8e27aae337
commit dd5e375e36
3 changed files with 39 additions and 40 deletions
+28 -30
View File
@@ -7,6 +7,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net/http"
"net/http/httptrace" "net/http/httptrace"
"strconv" "strconv"
"strings" "strings"
@@ -93,6 +94,9 @@ type Stat struct {
// isReused is true when connection is reused (keep-alive) // isReused is true when connection is reused (keep-alive)
isReused bool isReused bool
// https or http
schema string
} }
// Finish sets the time when reading response is done. // Finish sets the time when reading response is done.
@@ -110,11 +114,6 @@ func (s *Stat) Finish() {
s.Total = s.transferDone.Sub(s.dnsStart) s.Total = s.transferDone.Sub(s.dnsStart)
} }
func (s *Stat) assertSchemaHTTP() bool {
// HTTP other than HTTPS
return s.dnsStart.IsZero() && s.tcpStart.IsZero()
}
// Durations returns all durations and timelines of request latencies // Durations returns all durations and timelines of request latencies
func (s *Stat) Durations() map[string]int64 { func (s *Stat) Durations() map[string]int64 {
return map[string]int64{ return map[string]int64{
@@ -132,20 +131,8 @@ func (s *Stat) Durations() map[string]int64 {
} }
func (s *Stat) Print() { func (s *Stat) Print() {
if s.assertSchemaHTTP() { switch s.schema {
// http case "https":
printf(colorize(httpTemplate),
fmta(s.DNSLookup), // dns lookup
fmta(s.TCPConnection), // tcp connection
fmta(s.ServerProcessing), // server processing
fmta(s.ContentTransfer), // content transfer
fmtb(s.NameLookup), // namelookup
fmtb(s.Connect), // connect
fmtb(s.StartTransfer), // starttransfer
fmtb(s.Total), // total
)
} else {
// https
printf(colorize(httpsTemplate), printf(colorize(httpsTemplate),
fmta(s.DNSLookup), // dns lookup fmta(s.DNSLookup), // dns lookup
fmta(s.TCPConnection), // tcp connection fmta(s.TCPConnection), // tcp connection
@@ -158,6 +145,17 @@ func (s *Stat) Print() {
fmtb(s.StartTransfer), // starttransfer fmtb(s.StartTransfer), // starttransfer
fmtb(s.Total), // total fmtb(s.Total), // total
) )
case "http":
printf(colorize(httpTemplate),
fmta(s.DNSLookup), // dns lookup
fmta(s.TCPConnection), // tcp connection
fmta(s.ServerProcessing), // server processing
fmta(s.ContentTransfer), // content transfer
fmtb(s.NameLookup), // namelookup
fmtb(s.Connect), // connect
fmtb(s.StartTransfer), // starttransfer
fmtb(s.Total), // total
)
} }
log.Info(). log.Info().
Interface("httpstat(ms)", s.Durations()). Interface("httpstat(ms)", s.Durations()).
@@ -166,8 +164,9 @@ func (s *Stat) Print() {
// WithHTTPStat is a wrapper of httptrace.WithClientTrace. // WithHTTPStat is a wrapper of httptrace.WithClientTrace.
// It records the time of each httptrace hooks. // It records the time of each httptrace hooks.
func WithHTTPStat(ctx context.Context, s *Stat) context.Context { func WithHTTPStat(req *http.Request, s *Stat) context.Context {
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{ s.schema = req.URL.Scheme
return httptrace.WithClientTrace(req.Context(), &httptrace.ClientTrace{
DNSStart: func(i httptrace.DNSStartInfo) { DNSStart: func(i httptrace.DNSStartInfo) {
s.dnsStart = time.Now() s.dnsStart = time.Now()
}, },
@@ -176,7 +175,7 @@ func WithHTTPStat(ctx context.Context, s *Stat) context.Context {
s.dnsDone = time.Now() s.dnsDone = time.Now()
s.DNSLookup = s.dnsDone.Sub(s.dnsStart) s.DNSLookup = s.dnsDone.Sub(s.dnsStart)
s.NameLookup = s.dnsDone.Sub(s.dnsStart) s.NameLookup = s.DNSLookup
}, },
ConnectStart: func(_, _ string) { ConnectStart: func(_, _ string) {
@@ -215,12 +214,11 @@ func WithHTTPStat(ctx context.Context, s *Stat) context.Context {
}, },
WroteRequest: func(info httptrace.WroteRequestInfo) { WroteRequest: func(info httptrace.WroteRequestInfo) {
s.serverStart = time.Now() now := time.Now()
s.serverStart = now
// When client doesn't use DialContext or using old (before go1.7) `net` // When client doesn't use DialContext, DNS/TCP/TLS hook is not called.
// package, DNS/TCP/TLS hook is not called. if s.dnsStart.IsZero() && s.tcpStart.IsZero() {
if s.assertSchemaHTTP() {
now := s.serverStart
s.dnsStart = now s.dnsStart = now
s.dnsDone = now s.dnsDone = now
s.tcpStart = now s.tcpStart = now
@@ -229,7 +227,6 @@ func WithHTTPStat(ctx context.Context, s *Stat) context.Context {
// When connection is re-used, DNS/TCP/TLS hook is not called. // When connection is re-used, DNS/TCP/TLS hook is not called.
if s.isReused { if s.isReused {
now := s.serverStart
s.dnsStart = now s.dnsStart = now
s.dnsDone = now s.dnsDone = now
s.tcpStart = now s.tcpStart = now
@@ -238,11 +235,12 @@ func WithHTTPStat(ctx context.Context, s *Stat) context.Context {
s.tlsDone = now s.tlsDone = now
} }
if s.isTLS { if s.isTLS { // https
return return
} }
s.TLSHandshake = s.tcpDone.Sub(s.tcpDone) // http
s.TLSHandshake = 0
s.Pretransfer = s.Connect s.Pretransfer = s.Connect
}, },
+8 -7
View File
@@ -315,7 +315,7 @@ func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err
// stat HTTP request // stat HTTP request
var httpStat httpstat.Stat var httpStat httpstat.Stat
if r.HTTPStatOn() { if r.HTTPStatOn() {
ctx := httpstat.WithHTTPStat(rb.req.Context(), &httpStat) ctx := httpstat.WithHTTPStat(rb.req, &httpStat)
rb.req = rb.req.WithContext(ctx) rb.req = rb.req.WithContext(ctx)
} }
@@ -347,12 +347,6 @@ func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err
} }
} }
if r.HTTPStatOn() {
httpStat.Finish()
stepResult.HttpStat = httpStat.Durations()
httpStat.Print()
}
// new response object // new response object
respObj, err := newHttpResponseObject(r.hrpRunner.t, parser, resp) respObj, err := newHttpResponseObject(r.hrpRunner.t, parser, resp)
if err != nil { if err != nil {
@@ -360,6 +354,13 @@ func runStepRequest(r *SessionRunner, step *TStep) (stepResult *StepResult, err
return return
} }
if r.HTTPStatOn() {
// resp.Body has been ReadAll
httpStat.Finish()
stepResult.HttpStat = httpStat.Durations()
httpStat.Print()
}
// add response object to step variables, could be used in teardown hooks // add response object to step variables, could be used in teardown hooks
stepVariables["hrp_step_response"] = respObj.respObjMeta stepVariables["hrp_step_response"] = respObj.respObjMeta
+3 -3
View File
@@ -120,7 +120,7 @@ func TestRunRequestStatOn(t *testing.T) {
if !assert.GreaterOrEqual(t, stat["ContentTransfer"], int64(0)) { if !assert.GreaterOrEqual(t, stat["ContentTransfer"], int64(0)) {
t.Fatal() t.Fatal()
} }
if !assert.Greater(t, stat["NameLookup"], int64(0)) { if !assert.GreaterOrEqual(t, stat["NameLookup"], int64(0)) {
t.Fatal() t.Fatal()
} }
if !assert.Greater(t, stat["Connect"], int64(0)) { if !assert.Greater(t, stat["Connect"], int64(0)) {
@@ -132,7 +132,7 @@ func TestRunRequestStatOn(t *testing.T) {
if !assert.Greater(t, stat["StartTransfer"], int64(0)) { if !assert.Greater(t, stat["StartTransfer"], int64(0)) {
t.Fatal() t.Fatal()
} }
if !assert.Greater(t, stat["Total"], int64(10)) { if !assert.Greater(t, stat["Total"], int64(5)) {
t.Fatal() t.Fatal()
} }
if !assert.Less(t, stat["Total"]-summary.Records[0].Elapsed, int64(2)) { if !assert.Less(t, stat["Total"]-summary.Records[0].Elapsed, int64(2)) {
@@ -168,7 +168,7 @@ func TestRunRequestStatOn(t *testing.T) {
if !assert.Greater(t, stat["StartTransfer"], int64(0)) { if !assert.Greater(t, stat["StartTransfer"], int64(0)) {
t.Fatal() t.Fatal()
} }
if !assert.Greater(t, stat["Total"], int64(10)) { if !assert.Greater(t, stat["Total"], int64(1)) {
t.Fatal() t.Fatal()
} }
if !assert.Less(t, stat["Total"]-summary.Records[0].Elapsed, int64(2)) { if !assert.Less(t, stat["Total"]-summary.Records[0].Elapsed, int64(2)) {