|
| 1 | +// Copyright 2022 ChainSafe Systems (ON) |
| 2 | +// SPDX-License-Identifier: LGPL-3.0-only |
| 3 | + |
| 4 | +package sync |
| 5 | + |
| 6 | +import ( |
| 7 | + "container/ring" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_newSyncBenchmarker(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + t.Run("10 samples to keep", func(t *testing.T) { |
| 18 | + const samplesToKeep = 10 |
| 19 | + actual := newSyncBenchmarker(samplesToKeep) |
| 20 | + |
| 21 | + expected := &syncBenchmarker{ |
| 22 | + blocksPerSecond: ring.New(samplesToKeep), |
| 23 | + samplesToKeep: samplesToKeep, |
| 24 | + } |
| 25 | + |
| 26 | + assert.Equal(t, expected, actual) |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("panics on 0 sample to keep", func(t *testing.T) { |
| 30 | + const samplesToKeep = 0 |
| 31 | + assert.PanicsWithValue(t, "cannot have 0 samples to keep", func() { |
| 32 | + newSyncBenchmarker(samplesToKeep) |
| 33 | + }) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func Test_syncBenchmarker_begin(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + const startSec = 1000 |
| 41 | + start := time.Unix(startSec, 0) |
| 42 | + const startBlock = 10 |
| 43 | + |
| 44 | + b := syncBenchmarker{} |
| 45 | + b.begin(start, startBlock) |
| 46 | + |
| 47 | + expected := syncBenchmarker{ |
| 48 | + start: start, |
| 49 | + startBlock: startBlock, |
| 50 | + } |
| 51 | + |
| 52 | + assert.Equal(t, expected, b) |
| 53 | +} |
| 54 | + |
| 55 | +func Test_syncBenchmarker_end(t *testing.T) { |
| 56 | + t.Parallel() |
| 57 | + |
| 58 | + const startSec = 1000 |
| 59 | + start := time.Unix(startSec, 0) |
| 60 | + |
| 61 | + const nowSec = 1010 |
| 62 | + now := time.Unix(nowSec, 0) |
| 63 | + |
| 64 | + const ( |
| 65 | + startBlock = 10 |
| 66 | + endBlock = 12 |
| 67 | + ) |
| 68 | + |
| 69 | + const ringCap = 3 |
| 70 | + |
| 71 | + blocksPerSecond := ring.New(ringCap) |
| 72 | + blocksPerSecond.Value = 1.00 |
| 73 | + blocksPerSecond = blocksPerSecond.Next() |
| 74 | + |
| 75 | + b := syncBenchmarker{ |
| 76 | + start: start, |
| 77 | + startBlock: startBlock, |
| 78 | + blocksPerSecond: blocksPerSecond, |
| 79 | + } |
| 80 | + b.end(now, endBlock) |
| 81 | + |
| 82 | + expectedBlocksPerSecond := ring.New(ringCap) |
| 83 | + expectedBlocksPerSecond.Value = 1.00 |
| 84 | + expectedBlocksPerSecond = expectedBlocksPerSecond.Next() |
| 85 | + expectedBlocksPerSecond.Value = 0.2 |
| 86 | + expectedBlocksPerSecond = expectedBlocksPerSecond.Next() |
| 87 | + |
| 88 | + expected := syncBenchmarker{ |
| 89 | + start: start, |
| 90 | + startBlock: startBlock, |
| 91 | + blocksPerSecond: expectedBlocksPerSecond, |
| 92 | + } |
| 93 | + |
| 94 | + assert.Equal(t, expected, b) |
| 95 | +} |
| 96 | + |
| 97 | +func Test_syncBenchmarker_average(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + testCases := map[string]struct { |
| 101 | + values []float64 |
| 102 | + ringCap int |
| 103 | + average float64 |
| 104 | + }{ |
| 105 | + // zero size ring is not possible due to constructor check |
| 106 | + "empty ring": { |
| 107 | + ringCap: 1, |
| 108 | + }, |
| 109 | + "single element in one-size ring": { |
| 110 | + values: []float64{1.1}, |
| 111 | + ringCap: 1, |
| 112 | + average: 1.1, |
| 113 | + }, |
| 114 | + "single element in two-size ring": { |
| 115 | + values: []float64{1.1}, |
| 116 | + ringCap: 2, |
| 117 | + average: 1.1, |
| 118 | + }, |
| 119 | + "two elements in two-size ring": { |
| 120 | + values: []float64{1.0, 2.0}, |
| 121 | + ringCap: 2, |
| 122 | + average: 1.5, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + for name, testCase := range testCases { |
| 127 | + testCase := testCase |
| 128 | + t.Run(name, func(t *testing.T) { |
| 129 | + t.Parallel() |
| 130 | + |
| 131 | + blocksPerSecond := ring.New(testCase.ringCap) |
| 132 | + for _, value := range testCase.values { |
| 133 | + blocksPerSecond.Value = value |
| 134 | + blocksPerSecond = blocksPerSecond.Next() |
| 135 | + } |
| 136 | + |
| 137 | + benchmarker := syncBenchmarker{ |
| 138 | + blocksPerSecond: blocksPerSecond, |
| 139 | + samplesToKeep: testCase.ringCap, |
| 140 | + } |
| 141 | + |
| 142 | + avg := benchmarker.average() |
| 143 | + |
| 144 | + assert.Equal(t, testCase.average, avg) |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func Test_syncBenchmarker_mostRecentAverage(t *testing.T) { |
| 150 | + t.Parallel() |
| 151 | + |
| 152 | + testCases := map[string]struct { |
| 153 | + values []float64 |
| 154 | + ringCap int |
| 155 | + average float64 |
| 156 | + }{ |
| 157 | + // zero size ring is not possible due to constructor check |
| 158 | + "empty ring": { |
| 159 | + ringCap: 1, |
| 160 | + }, |
| 161 | + "single element in one-size ring": { |
| 162 | + values: []float64{1.1}, |
| 163 | + ringCap: 1, |
| 164 | + average: 1.1, |
| 165 | + }, |
| 166 | + "single element in two-size ring": { |
| 167 | + values: []float64{1.1}, |
| 168 | + ringCap: 2, |
| 169 | + average: 1.1, |
| 170 | + }, |
| 171 | + "two elements in two-size ring": { |
| 172 | + values: []float64{1.0, 2.0}, |
| 173 | + ringCap: 2, |
| 174 | + average: 2.0, |
| 175 | + }, |
| 176 | + "three elements in two-size ring": { |
| 177 | + values: []float64{1.0, 2.0, 3.0}, |
| 178 | + ringCap: 2, |
| 179 | + average: 3.0, |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + for name, testCase := range testCases { |
| 184 | + testCase := testCase |
| 185 | + t.Run(name, func(t *testing.T) { |
| 186 | + t.Parallel() |
| 187 | + |
| 188 | + blocksPerSecond := ring.New(testCase.ringCap) |
| 189 | + for _, value := range testCase.values { |
| 190 | + blocksPerSecond.Value = value |
| 191 | + blocksPerSecond = blocksPerSecond.Next() |
| 192 | + } |
| 193 | + |
| 194 | + benchmarker := syncBenchmarker{ |
| 195 | + blocksPerSecond: blocksPerSecond, |
| 196 | + } |
| 197 | + |
| 198 | + avg := benchmarker.mostRecentAverage() |
| 199 | + |
| 200 | + assert.Equal(t, testCase.average, avg) |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func Test_syncBenchmarker(t *testing.T) { |
| 206 | + t.Parallel() |
| 207 | + |
| 208 | + const samplesToKeep = 5 |
| 209 | + benchmarker := newSyncBenchmarker(samplesToKeep) |
| 210 | + |
| 211 | + const initialBlock = 10 |
| 212 | + timeZero := time.Unix(0, 0) |
| 213 | + const timeIncrement = time.Second |
| 214 | + const baseBlocksIncrement uint64 = 1 |
| 215 | + |
| 216 | + startTime := timeZero |
| 217 | + endTime := startTime.Add(timeIncrement) |
| 218 | + var block uint64 = initialBlock |
| 219 | + |
| 220 | + const samples = 10 |
| 221 | + for i := 0; i < samples; i++ { |
| 222 | + benchmarker.begin(startTime, block) |
| 223 | + block += baseBlocksIncrement + uint64(i) |
| 224 | + benchmarker.end(endTime, block) |
| 225 | + |
| 226 | + startTime = startTime.Add(timeIncrement) |
| 227 | + endTime = startTime.Add(timeIncrement) |
| 228 | + } |
| 229 | + |
| 230 | + avg := benchmarker.average() |
| 231 | + const expectedAvg = 8.0 |
| 232 | + assert.Equal(t, expectedAvg, avg) |
| 233 | + |
| 234 | + mostRecentAvg := benchmarker.mostRecentAverage() |
| 235 | + const expectedMostRecentAvg = 10.0 |
| 236 | + assert.Equal(t, expectedMostRecentAvg, mostRecentAvg) |
| 237 | +} |
0 commit comments