Skip to content

Commit

Permalink
Some optimizations for NewSampleTrail with Failed set
Browse files Browse the repository at this point in the history
name                            old time/op    new time/op    delta
NewSampleFromTrail/no_failed-8    1.02µs ± 6%    1.08µs ± 7%     ~     (p=0.095 n=5+5)
NewSampleFromTrail/failed-8       2.22µs ± 3%    1.43µs ± 5%  -35.61%  (p=0.008 n=5+5)

name                            old alloc/op   new alloc/op   delta
NewSampleFromTrail/no_failed-8      336B ± 0%      336B ± 0%     ~     (all equal)
NewSampleFromTrail/failed-8         752B ± 0%      544B ± 0%  -27.66%  (p=0.008 n=5+5)

name                            old allocs/op  new allocs/op  delta
NewSampleFromTrail/no_failed-8      4.00 ± 0%      4.00 ± 0%     ~     (all equal)
NewSampleFromTrail/failed-8         5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.008 n=5+5)
  • Loading branch information
mstoykov committed Mar 1, 2021
1 parent 7333166 commit 2a8351b
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions stats/cloud/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,22 @@ type SampleDataMap struct {
// NewSampleFromTrail just creates a ready-to-send Sample instance
// directly from a httpext.Trail.
func NewSampleFromTrail(trail *httpext.Trail) *Sample {
var values map[string]float64
length := 8
if trail.Failed.Valid {
length++
}

values := make(map[string]float64, length)
values[metrics.HTTPReqs.Name] = 1
values[metrics.HTTPReqDuration.Name] = stats.D(trail.Duration)
values[metrics.HTTPReqBlocked.Name] = stats.D(trail.Blocked)
values[metrics.HTTPReqConnecting.Name] = stats.D(trail.Connecting)
values[metrics.HTTPReqTLSHandshaking.Name] = stats.D(trail.TLSHandshaking)
values[metrics.HTTPReqSending.Name] = stats.D(trail.Sending)
values[metrics.HTTPReqWaiting.Name] = stats.D(trail.Waiting)
values[metrics.HTTPReqReceiving.Name] = stats.D(trail.Receiving)
if trail.Failed.Valid { // this is done so the adding of 1 map element doesn't reexpand the map as this is a hotpath
values = map[string]float64{
metrics.HTTPReqs.Name: 1,
metrics.HTTPReqDuration.Name: stats.D(trail.Duration),
metrics.HTTPReqBlocked.Name: stats.D(trail.Blocked),
metrics.HTTPReqConnecting.Name: stats.D(trail.Connecting),
metrics.HTTPReqTLSHandshaking.Name: stats.D(trail.TLSHandshaking),
metrics.HTTPReqSending.Name: stats.D(trail.Sending),
metrics.HTTPReqWaiting.Name: stats.D(trail.Waiting),
metrics.HTTPReqReceiving.Name: stats.D(trail.Receiving),

metrics.HTTPReqFailed.Name: stats.B(trail.Failed.Bool),
}
} else {
values = map[string]float64{
metrics.HTTPReqs.Name: 1,
metrics.HTTPReqDuration.Name: stats.D(trail.Duration),
metrics.HTTPReqBlocked.Name: stats.D(trail.Blocked),
metrics.HTTPReqConnecting.Name: stats.D(trail.Connecting),
metrics.HTTPReqTLSHandshaking.Name: stats.D(trail.TLSHandshaking),
metrics.HTTPReqSending.Name: stats.D(trail.Sending),
metrics.HTTPReqWaiting.Name: stats.D(trail.Waiting),
metrics.HTTPReqReceiving.Name: stats.D(trail.Receiving),
}
values[metrics.HTTPReqFailed.Name] = stats.B(trail.Failed.Bool)
}
return &Sample{
Type: DataTypeMap,
Expand Down

0 comments on commit 2a8351b

Please sign in to comment.