test: add unittest for httpstat

This commit is contained in:
debugtalk
2022-05-05 00:29:14 +08:00
parent f19023d52c
commit aa926d53cf
3 changed files with 107 additions and 23 deletions

View File

@@ -62,18 +62,18 @@ func (s *Stat) Finish() {
} }
// Durations returns all durations and timelines of request latencies // Durations returns all durations and timelines of request latencies
func (s *Stat) Durations() map[string]time.Duration { func (s *Stat) Durations() map[string]int64 {
return map[string]time.Duration{ return map[string]int64{
"DNSLookup": s.DNSLookup / time.Millisecond, "DNSLookup": s.DNSLookup.Milliseconds(),
"TCPConnection": s.TCPConnection / time.Millisecond, "TCPConnection": s.TCPConnection.Milliseconds(),
"TLSHandshake": s.TLSHandshake / time.Millisecond, "TLSHandshake": s.TLSHandshake.Milliseconds(),
"ServerProcessing": s.ServerProcessing / time.Millisecond, "ServerProcessing": s.ServerProcessing.Milliseconds(),
"ContentTransfer": s.ContentTransfer / time.Millisecond, "ContentTransfer": s.ContentTransfer.Milliseconds(),
"NameLookup": s.NameLookup / time.Millisecond, "NameLookup": s.NameLookup.Milliseconds(),
"Connect": s.Connect / time.Millisecond, "Connect": s.Connect.Milliseconds(),
"Pretransfer": s.Connect / time.Millisecond, "Pretransfer": s.Connect.Milliseconds(),
"StartTransfer": s.StartTransfer / time.Millisecond, "StartTransfer": s.StartTransfer.Milliseconds(),
"Total": s.Total / time.Millisecond, "Total": s.Total.Milliseconds(),
} }
} }

View File

@@ -1,7 +1,5 @@
package hrp package hrp
import "time"
type StepType string type StepType string
const ( const (
@@ -15,15 +13,15 @@ const (
) )
type StepResult struct { type StepResult struct {
Name string `json:"name" yaml:"name"` // step name Name string `json:"name" yaml:"name"` // step name
StepType StepType `json:"step_type" yaml:"step_type"` // step type, testcase/request/transaction/rendezvous StepType StepType `json:"step_type" yaml:"step_type"` // step type, testcase/request/transaction/rendezvous
Success bool `json:"success" yaml:"success"` // step execution result Success bool `json:"success" yaml:"success"` // step execution result
Elapsed int64 `json:"elapsed_ms" yaml:"elapsed_ms"` // step execution time in millisecond(ms) Elapsed int64 `json:"elapsed_ms" yaml:"elapsed_ms"` // step execution time in millisecond(ms)
HttpStat map[string]time.Duration `json:"httpstat" yaml:"httpstat"` // httpstat in millisecond(ms) HttpStat map[string]int64 `json:"httpstat" yaml:"httpstat"` // httpstat in millisecond(ms)
Data interface{} `json:"data,omitempty" yaml:"data,omitempty"` // session data or slice of step data Data interface{} `json:"data,omitempty" yaml:"data,omitempty"` // session data or slice of step data
ContentSize int64 `json:"content_size" yaml:"content_size"` // response body length ContentSize int64 `json:"content_size" yaml:"content_size"` // response body length
ExportVars map[string]interface{} `json:"export_vars,omitempty" yaml:"export_vars,omitempty"` // extract variables ExportVars map[string]interface{} `json:"export_vars,omitempty" yaml:"export_vars,omitempty"` // extract variables
Attachment string `json:"attachment,omitempty" yaml:"attachment,omitempty"` // step error information Attachment string `json:"attachment,omitempty" yaml:"attachment,omitempty"` // step error information
} }
// TStep represents teststep data structure. // TStep represents teststep data structure.

View File

@@ -2,6 +2,8 @@ package hrp
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
var ( var (
@@ -89,3 +91,87 @@ func TestRunRequestRun(t *testing.T) {
t.Fatalf("stepPOSTData.Run() error: %v", err) t.Fatalf("stepPOSTData.Run() error: %v", err)
} }
} }
func TestRunRequestStatOn(t *testing.T) {
testcase := &TestCase{
Config: NewConfig("test").SetBaseURL("https://postman-echo.com"),
TestSteps: []IStep{stepGET, stepPOSTData},
}
runner := NewRunner(t).SetHTTPStatOn()
sessionRunner, _ := runner.NewSessionRunner(testcase)
if err := sessionRunner.Start(nil); err != nil {
t.Fatal()
}
summary := sessionRunner.GetSummary()
stat := summary.Records[0].HttpStat
if !assert.Greater(t, stat["DNSLookup"], int64(1)) {
t.Fatal()
}
if !assert.Greater(t, stat["TCPConnection"], int64(1)) {
t.Fatal()
}
if !assert.Greater(t, stat["TLSHandshake"], int64(1)) {
t.Fatal()
}
if !assert.Greater(t, stat["ServerProcessing"], int64(1)) {
t.Fatal()
}
if !assert.GreaterOrEqual(t, stat["ContentTransfer"], int64(0)) {
t.Fatal()
}
if !assert.Greater(t, stat["NameLookup"], int64(1)) {
t.Fatal()
}
if !assert.Greater(t, stat["Connect"], int64(1)) {
t.Fatal()
}
if !assert.Greater(t, stat["Pretransfer"], int64(1)) {
t.Fatal()
}
if !assert.Greater(t, stat["StartTransfer"], int64(1)) {
t.Fatal()
}
if !assert.Greater(t, stat["Total"], int64(10)) {
t.Fatal()
}
if !assert.Less(t, stat["Total"]-summary.Records[0].Elapsed, int64(2)) {
t.Fatal()
}
// reuse connection
stat = summary.Records[1].HttpStat
if !assert.Equal(t, stat["DNSLookup"], int64(0)) {
t.Fatal()
}
if !assert.Equal(t, stat["TCPConnection"], int64(0)) {
t.Fatal()
}
if !assert.Equal(t, stat["TLSHandshake"], int64(0)) {
t.Fatal()
}
if !assert.Greater(t, stat["ServerProcessing"], int64(1)) {
t.Fatal()
}
if !assert.Equal(t, stat["ContentTransfer"], int64(0)) {
t.Fatal()
}
if !assert.Equal(t, stat["NameLookup"], int64(0)) {
t.Fatal()
}
if !assert.Equal(t, stat["Connect"], int64(0)) {
t.Fatal()
}
if !assert.Equal(t, stat["Pretransfer"], int64(0)) {
t.Fatal()
}
if !assert.Greater(t, stat["StartTransfer"], int64(10)) {
t.Fatal()
}
if !assert.Greater(t, stat["Total"], int64(10)) {
t.Fatal()
}
if !assert.Less(t, stat["Total"]-summary.Records[0].Elapsed, int64(2)) {
t.Fatal()
}
}