From 9999f0db266131bae324bb7dcd71ffe11129ff9f Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Wed, 9 Oct 2019 11:00:26 +0300 Subject: [PATCH] Add benchmark for getPlannedRateChanges and perf improvement The results are: benchmark old ns/op new ns/op delta BenchmarkGetPlannedRateChanges-4 4140227 2710011 -34.54% BenchmarkGetPlannedRateChanges-4 3531805 2397757 -32.11% BenchmarkGetPlannedRateChanges-4 3337196 2277776 -31.75% BenchmarkGetPlannedRateChanges-4 2984674 2192676 -26.54% BenchmarkGetPlannedRateChanges-4 2900849 2492187 -14.09% benchmark old allocs new allocs delta BenchmarkGetPlannedRateChanges-4 72977 56205 -22.98% BenchmarkGetPlannedRateChanges-4 72977 56205 -22.98% BenchmarkGetPlannedRateChanges-4 72977 56205 -22.98% BenchmarkGetPlannedRateChanges-4 72977 56205 -22.98% BenchmarkGetPlannedRateChanges-4 72977 56205 -22.98% benchmark old bytes new bytes delta BenchmarkGetPlannedRateChanges-4 1503784 1254588 -16.57% BenchmarkGetPlannedRateChanges-4 1503780 1254585 -16.57% BenchmarkGetPlannedRateChanges-4 1503780 1254588 -16.57% BenchmarkGetPlannedRateChanges-4 1503771 1254584 -16.57% BenchmarkGetPlannedRateChanges-4 1503775 1254588 -16.57% --- lib/executor/variable_arrival_rate.go | 4 +-- lib/executor/variable_arrival_rate_test.go | 31 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/executor/variable_arrival_rate.go b/lib/executor/variable_arrival_rate.go index ca458bbe5cd..a6b94412997 100644 --- a/lib/executor/variable_arrival_rate.go +++ b/lib/executor/variable_arrival_rate.go @@ -212,16 +212,16 @@ func (varc VariableArrivalRateConfig) getPlannedRateChanges(segment *lib.Executi // nanosecond precision, so there isn't any actual loss of precision... stepNumber := (stageDuration / minIntervalBetweenRateAdjustments) if stepNumber > 1 { + rateDiff := new(big.Rat).Sub(stageTargetRate, currentRate) stepInterval := stageDuration / stepNumber for t := stepInterval; ; t += stepInterval { if stageDuration-t < minIntervalBetweenRateAdjustments { break } - rateDiff := new(big.Rat).Sub(stageTargetRate, currentRate) tArrivalRate := new(big.Rat).Add( currentRate, - rateDiff.Mul(rateDiff, big.NewRat(int64(t), int64(stageDuration))), + new(big.Rat).Mul(rateDiff, big.NewRat(int64(t), int64(stageDuration))), ) rateChanges = append(rateChanges, rateChange{ diff --git a/lib/executor/variable_arrival_rate_test.go b/lib/executor/variable_arrival_rate_test.go index bc244e59bd7..e7ec602a1b8 100644 --- a/lib/executor/variable_arrival_rate_test.go +++ b/lib/executor/variable_arrival_rate_test.go @@ -103,6 +103,37 @@ func TestGetPlannedRateChanges(t *testing.T) { } } +func BenchmarkGetPlannedRateChanges(b *testing.B) { + var config = VariableArrivalRateConfig{ + TimeUnit: types.NullDurationFrom(time.Second), + StartRate: null.IntFrom(0), + Stages: []Stage{ + { + Duration: types.NullDurationFrom(5 * time.Minute), + Target: null.IntFrom(5000), + }, + { + Duration: types.NullDurationFrom(50 * time.Minute), + Target: null.IntFrom(5000), + }, + { + Duration: types.NullDurationFrom(5 * time.Minute), + Target: null.IntFrom(0), + }, + }, + } + + var v *lib.ExecutionSegment + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + changes := config.getPlannedRateChanges(v) + + require.Equal(b, time.Duration(0), + changes[0].timeOffset%minIntervalBetweenRateAdjustments, "%+v", changes[0]) + } + }) +} + func initializeVUs( ctx context.Context, t testing.TB, logEntry *logrus.Entry, es *lib.ExecutionState, number int, ) {