mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
feat: print connected network info
This commit is contained in:
@@ -97,6 +97,9 @@ type Stat struct {
|
|||||||
|
|
||||||
// https or http
|
// https or http
|
||||||
schema string
|
schema string
|
||||||
|
|
||||||
|
// connected network info
|
||||||
|
network, addr string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finish sets the time when reading response is done.
|
// Finish sets the time when reading response is done.
|
||||||
@@ -131,6 +134,14 @@ func (s *Stat) Durations() map[string]int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stat) Print() {
|
func (s *Stat) Print() {
|
||||||
|
if s.network != "" && s.addr != "" {
|
||||||
|
printf("\n%s %s: %s\n",
|
||||||
|
color.CyanString("Connected to"),
|
||||||
|
color.YellowString(s.network),
|
||||||
|
color.BlueString(s.addr),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
switch s.schema {
|
switch s.schema {
|
||||||
case "https":
|
case "https":
|
||||||
printf(colorize(httpsTemplate),
|
printf(colorize(httpsTemplate),
|
||||||
@@ -178,7 +189,10 @@ func WithHTTPStat(req *http.Request, s *Stat) context.Context {
|
|||||||
s.NameLookup = s.DNSLookup
|
s.NameLookup = s.DNSLookup
|
||||||
},
|
},
|
||||||
|
|
||||||
ConnectStart: func(_, _ string) {
|
ConnectStart: func(network, addr string) {
|
||||||
|
s.network = network
|
||||||
|
s.addr = addr
|
||||||
|
|
||||||
s.tcpStart = time.Now()
|
s.tcpStart = time.Now()
|
||||||
|
|
||||||
// When connecting to IP (When no DNS lookup)
|
// When connecting to IP (When no DNS lookup)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"compress/zlib"
|
"compress/zlib"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -15,6 +16,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/andybalholm/brotli"
|
"github.com/andybalholm/brotli"
|
||||||
|
"github.com/fatih/color"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
@@ -423,8 +425,22 @@ func printRequest(req *http.Request) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printf(format string, a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Fprintf(color.Output, format, a...)
|
||||||
|
}
|
||||||
|
|
||||||
func printResponse(resp *http.Response) error {
|
func printResponse(resp *http.Response) error {
|
||||||
fmt.Println("==================== response ====================")
|
fmt.Println("==================== response ====================")
|
||||||
|
connectedVia := "plaintext"
|
||||||
|
if resp.TLS != nil {
|
||||||
|
switch resp.TLS.Version {
|
||||||
|
case tls.VersionTLS12:
|
||||||
|
connectedVia = "TLSv1.2"
|
||||||
|
case tls.VersionTLS13:
|
||||||
|
connectedVia = "TLSv1.3"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%s %s\n", color.CyanString("Connected via"), color.BlueString("%s", connectedVia))
|
||||||
respContentType := resp.Header.Get("Content-Type")
|
respContentType := resp.Header.Get("Content-Type")
|
||||||
printBody := shouldPrintBody(respContentType)
|
printBody := shouldPrintBody(respContentType)
|
||||||
respDump, err := httputil.DumpResponse(resp, printBody)
|
respDump, err := httputil.DumpResponse(resp, printBody)
|
||||||
|
|||||||
@@ -135,34 +135,31 @@ func TestRunRequestStatOn(t *testing.T) {
|
|||||||
if !assert.Greater(t, stat["Total"], int64(5)) {
|
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(3)) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// reuse connection
|
// reuse connection
|
||||||
stat = summary.Records[1].HttpStat
|
stat = summary.Records[1].HttpStat
|
||||||
if !assert.Equal(t, stat["DNSLookup"], int64(0)) {
|
if !assert.Equal(t, int64(0), stat["DNSLookup"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, stat["TCPConnection"], int64(0)) {
|
if !assert.Equal(t, int64(0), stat["TCPConnection"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, stat["TLSHandshake"], int64(0)) {
|
if !assert.Equal(t, int64(0), stat["TLSHandshake"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Greater(t, stat["ServerProcessing"], int64(1)) {
|
if !assert.Greater(t, stat["ServerProcessing"], int64(1)) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, stat["ContentTransfer"], int64(0)) {
|
if !assert.Equal(t, int64(0), stat["NameLookup"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, stat["NameLookup"], int64(0)) {
|
if !assert.Equal(t, int64(0), stat["Connect"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, stat["Connect"], int64(0)) {
|
if !assert.Equal(t, int64(0), stat["Pretransfer"]) {
|
||||||
t.Fatal()
|
|
||||||
}
|
|
||||||
if !assert.Equal(t, stat["Pretransfer"], int64(0)) {
|
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Greater(t, stat["StartTransfer"], int64(0)) {
|
if !assert.Greater(t, stat["StartTransfer"], int64(0)) {
|
||||||
@@ -171,7 +168,7 @@ func TestRunRequestStatOn(t *testing.T) {
|
|||||||
if !assert.Greater(t, stat["Total"], int64(1)) {
|
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(3)) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user