Skip to content

Commit 059a936

Browse files
committed
fix random string used in test allocating 50MB in Cortex binary (#5903)
Signed-off-by: Ben Ye <[email protected]>
1 parent cd3f7c6 commit 059a936

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

pkg/distributor/distributor_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ var (
5757
emptyResponse = &cortexpb.WriteResponse{}
5858
)
5959

60+
var (
61+
randomStrings = []string{}
62+
)
63+
64+
func init() {
65+
randomStrings = util.GenerateRandomStrings()
66+
}
67+
6068
func TestConfig_Validate(t *testing.T) {
6169
t.Parallel()
6270
tests := map[string]struct {
@@ -2466,8 +2474,8 @@ func prepare(tb testing.TB, cfg prepConfig) ([]*Distributor, []*mockIngester, []
24662474
// Strings to be used for get labels values/Names
24672475
var unusedStrings []string
24682476
if cfg.lblValuesPerIngester > 0 {
2469-
unusedStrings = make([]string, min(len(util.RandomStrings), cfg.numIngesters*cfg.lblValuesPerIngester))
2470-
copy(unusedStrings, util.RandomStrings)
2477+
unusedStrings = make([]string, min(len(randomStrings), cfg.numIngesters*cfg.lblValuesPerIngester))
2478+
copy(unusedStrings, randomStrings)
24712479
}
24722480
s := &prepState{
24732481
unusedStrings: unusedStrings,

pkg/util/strings_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func BenchmarkMergeSlicesParallel(b *testing.B) {
4848
},
4949
}
5050

51+
randomStrings := GenerateRandomStrings()
5152
type ParallelismType int
5253

5354
const (
@@ -58,9 +59,9 @@ func BenchmarkMergeSlicesParallel(b *testing.B) {
5859

5960
for _, tc := range testCases {
6061
input := make([][]string, tc.inputSize)
61-
unusedStrings := make([]string, min(len(RandomStrings), tc.inputSize*tc.stringsPerInput))
62+
unusedStrings := make([]string, min(len(randomStrings), tc.inputSize*tc.stringsPerInput))
6263
usedStrings := make([]string, 0, len(unusedStrings))
63-
copy(unusedStrings, RandomStrings)
64+
copy(unusedStrings, randomStrings)
6465

6566
for i := 0; i < tc.inputSize; i++ {
6667
stringsToBeReused := make([]string, len(usedStrings))

pkg/util/test_util.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@ import (
55
"strings"
66
)
77

8-
var (
9-
randomChar = "0123456789abcdef"
10-
RandomStrings = []string{}
11-
)
12-
13-
func init() {
8+
func GenerateRandomStrings() []string {
9+
randomChar := "0123456789abcdef"
10+
randomStrings := make([]string, 0, 1000000)
1411
sb := strings.Builder{}
1512
for i := 0; i < 1000000; i++ {
1613
sb.Reset()
1714
sb.WriteString("pod://")
1815
for j := 0; j < 14; j++ {
1916
sb.WriteByte(randomChar[rand.Int()%len(randomChar)])
2017
}
21-
RandomStrings = append(RandomStrings, sb.String())
18+
randomStrings = append(randomStrings, sb.String())
2219
}
20+
return randomStrings
2321
}

0 commit comments

Comments
 (0)