From 2a8351bfd35a9eb5d78c4c90acde8d3feda19184 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Mon, 1 Mar 2021 13:10:44 +0200 Subject: [PATCH] Some optimizations for NewSampleTrail with Failed set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- stats/cloud/data.go | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/stats/cloud/data.go b/stats/cloud/data.go index 660524376143..ee6ce33e8f84 100644 --- a/stats/cloud/data.go +++ b/stats/cloud/data.go @@ -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,