Skip to content

Commit eecf304

Browse files
committed
Make ContentTransfer () and End () work even after go1.8
1 parent ae0d799 commit eecf304

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

go18.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ func (r *Result) End(t time.Time) {
2525
r.total = r.trasferDone.Sub(r.dnsStart)
2626
}
2727

28+
// ContentTransfer returns the duration of content transfer time.
29+
// It is from first response byte to the given time. The time must
30+
// be time after read body (go-httpstat can not detect that time).
31+
func (r *Result) ContentTransfer(t time.Time) time.Duration {
32+
return t.Sub(r.serverDone)
33+
}
34+
35+
// Total returns the duration of total http request.
36+
// It is from dns lookup start time to the given time. The
37+
// time must be time after read body (go-httpstat can not detect that time).
38+
func (r *Result) Total(t time.Time) time.Duration {
39+
return t.Sub(r.dnsStart)
40+
}
41+
2842
func withClientTrace(ctx context.Context, r *Result) context.Context {
2943
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
3044
DNSStart: func(i httptrace.DNSStartInfo) {

httpstat.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,6 @@ func (r *Result) durations() map[string]time.Duration {
6868
}
6969
}
7070

71-
// ContentTransfer returns the duration of content transfer time.
72-
// It is from first response byte to the given time. The time must
73-
// be time after read body (go-httpstat can not detect that time).
74-
func (r *Result) ContentTransfer(t time.Time) time.Duration {
75-
return t.Sub(r.t4)
76-
}
77-
78-
// Total returns the duration of total http request.
79-
// It is from dns lookup start time to the given time. The
80-
// time must be time after read body (go-httpstat can not detect that time).
81-
func (r *Result) Total(t time.Time) time.Duration {
82-
return t.Sub(r.t0)
83-
}
84-
8571
// Format formats stats result.
8672
func (r Result) Format(s fmt.State, verb rune) {
8773
switch verb {
@@ -97,7 +83,7 @@ func (r Result) Format(s fmt.State, verb rune) {
9783
fmt.Fprintf(&buf, "Server processing: %4d ms\n",
9884
int(r.ServerProcessing/time.Millisecond))
9985

100-
if !r.t5.IsZero() {
86+
if r.total > 0 {
10187
fmt.Fprintf(&buf, "Content transfer: %4d ms\n\n",
10288
int(r.contentTransfer/time.Millisecond))
10389
} else {
@@ -113,7 +99,7 @@ func (r Result) Format(s fmt.State, verb rune) {
11399
fmt.Fprintf(&buf, "Start Transfer: %4d ms\n",
114100
int(r.StartTransfer/time.Millisecond))
115101

116-
if !r.t5.IsZero() {
102+
if r.total > 0 {
117103
fmt.Fprintf(&buf, "Total: %4d ms\n",
118104
int(r.total/time.Millisecond))
119105
} else {

pre_go18.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ func (r *Result) End(t time.Time) {
2424
r.total = r.t5.Sub(r.t0)
2525
}
2626

27+
// ContentTransfer returns the duration of content transfer time.
28+
// It is from first response byte to the given time. The time must
29+
// be time after read body (go-httpstat can not detect that time).
30+
func (r *Result) ContentTransfer(t time.Time) time.Duration {
31+
return t.Sub(r.t4)
32+
}
33+
34+
// Total returns the duration of total http request.
35+
// It is from dns lookup start time to the given time. The
36+
// time must be time after read body (go-httpstat can not detect that time).
37+
func (r *Result) Total(t time.Time) time.Duration {
38+
return t.Sub(r.t0)
39+
}
40+
2741
func withClientTrace(ctx context.Context, r *Result) context.Context {
2842
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
2943
GetConn: func(hostPort string) {

0 commit comments

Comments
 (0)