-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dot/sync): sync benchmark (#2234)
- only keep 30 latest samples in a ring buffer - Inject current time in benchmark methods - Remove force setting blocks to `1` when `0` blocks were processed
- Loading branch information
Showing
3 changed files
with
276 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
// Copyright 2022 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package sync | ||
|
||
import ( | ||
"container/ring" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_newSyncBenchmarker(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("10 samples to keep", func(t *testing.T) { | ||
const samplesToKeep = 10 | ||
actual := newSyncBenchmarker(samplesToKeep) | ||
|
||
expected := &syncBenchmarker{ | ||
blocksPerSecond: ring.New(samplesToKeep), | ||
samplesToKeep: samplesToKeep, | ||
} | ||
|
||
assert.Equal(t, expected, actual) | ||
}) | ||
|
||
t.Run("panics on 0 sample to keep", func(t *testing.T) { | ||
const samplesToKeep = 0 | ||
assert.PanicsWithValue(t, "cannot have 0 samples to keep", func() { | ||
newSyncBenchmarker(samplesToKeep) | ||
}) | ||
}) | ||
} | ||
|
||
func Test_syncBenchmarker_begin(t *testing.T) { | ||
t.Parallel() | ||
|
||
const startSec = 1000 | ||
start := time.Unix(startSec, 0) | ||
const startBlock = 10 | ||
|
||
b := syncBenchmarker{} | ||
b.begin(start, startBlock) | ||
|
||
expected := syncBenchmarker{ | ||
start: start, | ||
startBlock: startBlock, | ||
} | ||
|
||
assert.Equal(t, expected, b) | ||
} | ||
|
||
func Test_syncBenchmarker_end(t *testing.T) { | ||
t.Parallel() | ||
|
||
const startSec = 1000 | ||
start := time.Unix(startSec, 0) | ||
|
||
const nowSec = 1010 | ||
now := time.Unix(nowSec, 0) | ||
|
||
const ( | ||
startBlock = 10 | ||
endBlock = 12 | ||
) | ||
|
||
const ringCap = 3 | ||
|
||
blocksPerSecond := ring.New(ringCap) | ||
blocksPerSecond.Value = 1.00 | ||
blocksPerSecond = blocksPerSecond.Next() | ||
|
||
b := syncBenchmarker{ | ||
start: start, | ||
startBlock: startBlock, | ||
blocksPerSecond: blocksPerSecond, | ||
} | ||
b.end(now, endBlock) | ||
|
||
expectedBlocksPerSecond := ring.New(ringCap) | ||
expectedBlocksPerSecond.Value = 1.00 | ||
expectedBlocksPerSecond = expectedBlocksPerSecond.Next() | ||
expectedBlocksPerSecond.Value = 0.2 | ||
expectedBlocksPerSecond = expectedBlocksPerSecond.Next() | ||
|
||
expected := syncBenchmarker{ | ||
start: start, | ||
startBlock: startBlock, | ||
blocksPerSecond: expectedBlocksPerSecond, | ||
} | ||
|
||
assert.Equal(t, expected, b) | ||
} | ||
|
||
func Test_syncBenchmarker_average(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
values []float64 | ||
ringCap int | ||
average float64 | ||
}{ | ||
// zero size ring is not possible due to constructor check | ||
"empty ring": { | ||
ringCap: 1, | ||
}, | ||
"single element in one-size ring": { | ||
values: []float64{1.1}, | ||
ringCap: 1, | ||
average: 1.1, | ||
}, | ||
"single element in two-size ring": { | ||
values: []float64{1.1}, | ||
ringCap: 2, | ||
average: 1.1, | ||
}, | ||
"two elements in two-size ring": { | ||
values: []float64{1.0, 2.0}, | ||
ringCap: 2, | ||
average: 1.5, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
blocksPerSecond := ring.New(testCase.ringCap) | ||
for _, value := range testCase.values { | ||
blocksPerSecond.Value = value | ||
blocksPerSecond = blocksPerSecond.Next() | ||
} | ||
|
||
benchmarker := syncBenchmarker{ | ||
blocksPerSecond: blocksPerSecond, | ||
samplesToKeep: testCase.ringCap, | ||
} | ||
|
||
avg := benchmarker.average() | ||
|
||
assert.Equal(t, testCase.average, avg) | ||
}) | ||
} | ||
} | ||
|
||
func Test_syncBenchmarker_mostRecentAverage(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
values []float64 | ||
ringCap int | ||
average float64 | ||
}{ | ||
// zero size ring is not possible due to constructor check | ||
"empty ring": { | ||
ringCap: 1, | ||
}, | ||
"single element in one-size ring": { | ||
values: []float64{1.1}, | ||
ringCap: 1, | ||
average: 1.1, | ||
}, | ||
"single element in two-size ring": { | ||
values: []float64{1.1}, | ||
ringCap: 2, | ||
average: 1.1, | ||
}, | ||
"two elements in two-size ring": { | ||
values: []float64{1.0, 2.0}, | ||
ringCap: 2, | ||
average: 2.0, | ||
}, | ||
"three elements in two-size ring": { | ||
values: []float64{1.0, 2.0, 3.0}, | ||
ringCap: 2, | ||
average: 3.0, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
blocksPerSecond := ring.New(testCase.ringCap) | ||
for _, value := range testCase.values { | ||
blocksPerSecond.Value = value | ||
blocksPerSecond = blocksPerSecond.Next() | ||
} | ||
|
||
benchmarker := syncBenchmarker{ | ||
blocksPerSecond: blocksPerSecond, | ||
} | ||
|
||
avg := benchmarker.mostRecentAverage() | ||
|
||
assert.Equal(t, testCase.average, avg) | ||
}) | ||
} | ||
} | ||
|
||
func Test_syncBenchmarker(t *testing.T) { | ||
t.Parallel() | ||
|
||
const samplesToKeep = 5 | ||
benchmarker := newSyncBenchmarker(samplesToKeep) | ||
|
||
const initialBlock = 10 | ||
timeZero := time.Unix(0, 0) | ||
const timeIncrement = time.Second | ||
const baseBlocksIncrement uint64 = 1 | ||
|
||
startTime := timeZero | ||
endTime := startTime.Add(timeIncrement) | ||
var block uint64 = initialBlock | ||
|
||
const samples = 10 | ||
for i := 0; i < samples; i++ { | ||
benchmarker.begin(startTime, block) | ||
block += baseBlocksIncrement + uint64(i) | ||
benchmarker.end(endTime, block) | ||
|
||
startTime = startTime.Add(timeIncrement) | ||
endTime = startTime.Add(timeIncrement) | ||
} | ||
|
||
avg := benchmarker.average() | ||
const expectedAvg = 8.0 | ||
assert.Equal(t, expectedAvg, avg) | ||
|
||
mostRecentAvg := benchmarker.mostRecentAverage() | ||
const expectedMostRecentAvg = 10.0 | ||
assert.Equal(t, expectedMostRecentAvg, mostRecentAvg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters