feat: print connected network info

This commit is contained in:
debugtalk
2022-05-05 21:20:20 +08:00
parent 47557a403b
commit f2dbd24124
3 changed files with 39 additions and 12 deletions

View File

@@ -97,6 +97,9 @@ type Stat struct {
// https or http
schema string
// connected network info
network, addr string
}
// Finish sets the time when reading response is done.
@@ -131,6 +134,14 @@ func (s *Stat) Durations() map[string]int64 {
}
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 {
case "https":
printf(colorize(httpsTemplate),
@@ -178,7 +189,10 @@ func WithHTTPStat(req *http.Request, s *Stat) context.Context {
s.NameLookup = s.DNSLookup
},
ConnectStart: func(_, _ string) {
ConnectStart: func(network, addr string) {
s.network = network
s.addr = addr
s.tcpStart = time.Now()
// When connecting to IP (When no DNS lookup)

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"compress/gzip"
"compress/zlib"
"crypto/tls"
"fmt"
"io"
"net/http"
@@ -15,6 +16,7 @@ import (
"time"
"github.com/andybalholm/brotli"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -423,8 +425,22 @@ func printRequest(req *http.Request) error {
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 {
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")
printBody := shouldPrintBody(respContentType)
respDump, err := httputil.DumpResponse(resp, printBody)

View File

@@ -135,34 +135,31 @@ func TestRunRequestStatOn(t *testing.T) {
if !assert.Greater(t, stat["Total"], int64(5)) {
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()
}
// reuse connection
stat = summary.Records[1].HttpStat
if !assert.Equal(t, stat["DNSLookup"], int64(0)) {
if !assert.Equal(t, int64(0), stat["DNSLookup"]) {
t.Fatal()
}
if !assert.Equal(t, stat["TCPConnection"], int64(0)) {
if !assert.Equal(t, int64(0), stat["TCPConnection"]) {
t.Fatal()
}
if !assert.Equal(t, stat["TLSHandshake"], int64(0)) {
if !assert.Equal(t, int64(0), stat["TLSHandshake"]) {
t.Fatal()
}
if !assert.Greater(t, stat["ServerProcessing"], int64(1)) {
t.Fatal()
}
if !assert.Equal(t, stat["ContentTransfer"], int64(0)) {
if !assert.Equal(t, int64(0), stat["NameLookup"]) {
t.Fatal()
}
if !assert.Equal(t, stat["NameLookup"], int64(0)) {
if !assert.Equal(t, int64(0), stat["Connect"]) {
t.Fatal()
}
if !assert.Equal(t, stat["Connect"], int64(0)) {
t.Fatal()
}
if !assert.Equal(t, stat["Pretransfer"], int64(0)) {
if !assert.Equal(t, int64(0), stat["Pretransfer"]) {
t.Fatal()
}
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)) {
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()
}
}