Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to math/rand/v2 #15513

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/compose/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package main

import (
"fmt"
"math/rand"
"math/rand/v2"
"os"
"time"

Expand Down Expand Up @@ -59,7 +59,7 @@ func main() {
fmt.Printf("begin failed: %v\n", err)
os.Exit(1)
}
page := rand.Intn(100) + 1
page := rand.IntN(100) + 1
timeCreated := time.Now().UnixNano()
if _, err := tx.Exec("INSERT INTO messages (page,time_created_ns,message) VALUES (?,?,?)",
page, timeCreated, "V is for speed"); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions go/bucketpool/bucketpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package bucketpool

import (
"math/rand"
"math/rand/v2"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -118,13 +118,13 @@ func TestPoolWeirdMaxSize(t *testing.T) {
func TestFuzz(t *testing.T) {
maxTestSize := 16384
for range 20000 {
minSize := rand.Intn(maxTestSize)
minSize := rand.IntN(maxTestSize)
if minSize == 0 {
minSize = 1
}
maxSize := rand.Intn(maxTestSize-minSize) + minSize
maxSize := rand.IntN(maxTestSize-minSize) + minSize
p := New(minSize, maxSize)
bufSize := rand.Intn(maxTestSize)
bufSize := rand.IntN(maxTestSize)
buf := p.Get(bufSize)
require.Len(t, *buf, bufSize, "unexpected buf length")
sPool := p.findPool(bufSize)
Expand All @@ -143,7 +143,7 @@ func BenchmarkPool(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
randomSize := rand.Intn(pool.maxSize)
randomSize := rand.IntN(pool.maxSize)
data := pool.Get(randomSize)
pool.Put(data)
}
Expand All @@ -156,7 +156,7 @@ func BenchmarkPoolGet(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
randomSize := rand.Intn(pool.maxSize)
randomSize := rand.IntN(pool.maxSize)
data := pool.Get(randomSize)
_ = data
}
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/vtclient/cli/vtclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"os"
"sort"
"sync"
Expand Down Expand Up @@ -174,7 +174,7 @@ func _run(cmd *cobra.Command, args []string) (*results, error) {
go func() {
if useRandom {
for {
seqChan <- rand.Intn(maxSeqID-minSeqID) + minSeqID
seqChan <- rand.IntN(maxSeqID-minSeqID) + minSeqID
}
} else {
for i := minSeqID; i < maxSeqID; i++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"context"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"sync"
"testing"

Expand Down Expand Up @@ -88,7 +88,7 @@ func newTestVDiffEnv(t testing.TB, ctx context.Context, sourceShards, targetShar
env.tmc.testEnv = env

// Generate a unique dialer name.
dialerName := fmt.Sprintf("VDiffTest-%s-%d", t.Name(), rand.Intn(1000000000))
dialerName := fmt.Sprintf("VDiffTest-%s-%d", t.Name(), rand.IntN(1000000000))
tabletconn.RegisterDialer(dialerName, func(tablet *topodatapb.Tablet, failFast grpcclient.FailFast) (queryservice.QueryService, error) {
env.mu.Lock()
defer env.mu.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions go/cmd/vttestserver/cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"fmt"
"io"
"math/rand"
"math/rand/v2"
"os/exec"
"path"
"strings"
Expand Down Expand Up @@ -426,7 +426,7 @@ func resetConfig(conf vttest.Config) {
}

func randomPort() int {
v := rand.Int31n(20000)
v := rand.Int32N(20000)
return int(v + 10000)
}

Expand Down
9 changes: 4 additions & 5 deletions go/mysql/collations/colldata/uca_contraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package colldata
import (
"encoding/json"
"fmt"
"math/rand"
"math/rand/v2"
"os"
"sort"
"testing"
Expand Down Expand Up @@ -203,14 +203,13 @@ func (s *strgen) generate(length int, freq float64) (out []byte) {
return flat[i] < flat[j]
})

gen := rand.New(rand.NewSource(0xDEADBEEF))
out = make([]byte, 0, length)
for len(out) < length {
if gen.Float64() < freq {
cnt := s.contractions[rand.Intn(len(s.contractions))]
if rand.Float64() < freq {
cnt := s.contractions[rand.IntN(len(s.contractions))]
out = append(out, cnt...)
} else {
cp := flat[rand.Intn(len(flat))]
cp := flat[rand.IntN(len(flat))]
out = append(out, string(cp)...)
}
}
Expand Down
4 changes: 2 additions & 2 deletions go/mysql/collations/colldata/uca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package colldata
import (
"bytes"
"fmt"
"math/rand"
"math/rand/v2"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -1002,7 +1002,7 @@ func TestUCACollationOrder(t *testing.T) {

ary := slices.Clone(sorted)
for i := range ary {
j := rand.Intn(i + 1)
j := rand.IntN(i + 1)
ary[i], ary[j] = ary[j], ary[i]
}
slices.SortFunc(ary, func(a, b string) int {
Expand Down
6 changes: 3 additions & 3 deletions go/mysql/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/rand"
"math/rand/v2"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -744,7 +744,7 @@ func TestEOFOrLengthEncodedIntFuzz(t *testing.T) {
}()

for i := 0; i < 100; i++ {
bytes := make([]byte, rand.Intn(16)+1)
bytes := make([]byte, rand.IntN(16)+1)
_, err := crypto_rand.Read(bytes)
require.NoError(t, err, "error doing rand.Read")

Expand Down Expand Up @@ -990,7 +990,7 @@ var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
b[i] = letters[rand.IntN(len(letters))]
}
return string(b)
}
Expand Down
8 changes: 3 additions & 5 deletions go/mysql/decimal/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package decimal

import (
"math"
"math/rand"
"math/rand/v2"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -142,13 +142,12 @@ func TestNewFromFloat(t *testing.T) {

func TestNewFromFloatRandom(t *testing.T) {
n := 0
rng := rand.New(rand.NewSource(0xdead1337))
for {
n++
if n == 10 {
break
}
in := (rng.Float64() - 0.5) * math.MaxFloat64 * 2
in := (rand.Float64() - 0.5) * math.MaxFloat64 * 2
want, err := NewFromString(strconv.FormatFloat(in, 'f', -1, 64))
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -176,13 +175,12 @@ func TestNewFromFloatQuick(t *testing.T) {

func TestNewFromFloat32Random(t *testing.T) {
n := 0
rng := rand.New(rand.NewSource(0xdead1337))
for {
n++
if n == 10 {
break
}
in := float32((rng.Float64() - 0.5) * math.MaxFloat32 * 2)
in := float32((rand.Float64() - 0.5) * math.MaxFloat32 * 2)
want, err := NewFromString(strconv.FormatFloat(float64(in), 'f', -1, 32))
if err != nil {
t.Error(err)
Expand Down
10 changes: 3 additions & 7 deletions go/mysql/decimal/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import (
"encoding/json"
"math"
"math/big"
"math/rand"
"math/rand/v2"
"os"
"path"
"strconv"
"strings"
"testing"
"time"
)

func TestDecimalAdd(t *testing.T) {
Expand Down Expand Up @@ -366,10 +365,8 @@ func TestRoundtripStress(t *testing.T) {
count = 100
}

rng := rand.New(rand.NewSource(time.Now().UnixNano()))

for n := 0; n < count; n++ {
fb := strconv.AppendFloat(nil, rng.NormFloat64(), 'f', -1, 64)
fb := strconv.AppendFloat(nil, rand.NormFloat64(), 'f', -1, 64)
d, err := NewFromMySQL(fb)
if err != nil {
t.Fatalf("failed to parse %q: %v", fb, err)
Expand All @@ -383,10 +380,9 @@ func TestRoundtripStress(t *testing.T) {

func BenchmarkFormatting(b *testing.B) {
const Count = 10000
var rng = rand.New(rand.NewSource(time.Now().UnixNano()))
var parsed = make([]Decimal, 0, Count)
for i := 0; i < Count; i++ {
parsed = append(parsed, NewFromFloat(rng.NormFloat64()))
parsed = append(parsed, NewFromFloat(rand.NormFloat64()))
}

b.Run("StringFixed(8)", func(b *testing.B) {
Expand Down
4 changes: 2 additions & 2 deletions go/mysql/endtoend/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package endtoend
import (
"context"
"fmt"
"math/rand"
"math/rand/v2"
"strings"
"testing"

Expand Down Expand Up @@ -151,7 +151,7 @@ func TestLargeQueries(t *testing.T) {
randString := func(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
b[i] = letterBytes[rand.IntN(len(letterBytes))]
}
return string(b)
}
Expand Down
4 changes: 2 additions & 2 deletions go/mysql/query_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package mysql

import (
"context"
"math/rand"
"math/rand/v2"
"net"
"strings"
"testing"
Expand Down Expand Up @@ -98,7 +98,7 @@ func benchmarkQuery(b *testing.B, threads int, query string, mkCfg mkListenerCfg
execQuery := query
if execQuery == "" {
// generate random query
n := rand.Intn(maxPacketSize-len(benchmarkQueryPrefix)) + 1
n := rand.IntN(maxPacketSize-len(benchmarkQueryPrefix)) + 1
execQuery = benchmarkQueryPrefix + strings.Repeat("x", n)

}
Expand Down
4 changes: 2 additions & 2 deletions go/pools/numbered_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package pools

import (
"math/rand"
"math/rand/v2"
"strings"
"testing"

Expand Down Expand Up @@ -99,7 +99,7 @@ func BenchmarkRegisterUnregisterParallel(b *testing.B) {
b.SetParallelism(200)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
id := rand.Int63()
id := rand.Int64()
p.Register(id, val)
p.Unregister(id, "some reason")
}
Expand Down
4 changes: 2 additions & 2 deletions go/pools/resource_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -419,7 +419,7 @@ func (rp *ResourcePool) extendedMaxLifetime() time.Duration {
if maxLifetime == 0 {
return 0
}
return time.Duration(maxLifetime + rand.Int63n(maxLifetime))
return time.Duration(maxLifetime + rand.Int64N(maxLifetime))
}

// MaxLifetimeClosed returns the count of resources closed due to refresh timeout.
Expand Down
4 changes: 2 additions & 2 deletions go/pools/smartconnpool/benchmarking/legacy/resource_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -576,7 +576,7 @@ func (rp *ResourcePool) extendedMaxLifetime() time.Duration {
if maxLifetime == 0 {
return 0
}
return time.Duration(maxLifetime + rand.Int63n(maxLifetime))
return time.Duration(maxLifetime + rand.Int64N(maxLifetime))
}

// MaxLifetimeClosed returns the count of resources closed due to refresh timeout.
Expand Down
2 changes: 1 addition & 1 deletion go/pools/smartconnpool/benchmarking/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/json"
"fmt"
"math"
"math/rand"
"math/rand/v2"
"os"
"sort"
"sync"
Expand Down
Loading
Loading