[TrueCloudLab#22] datagen: Use local randomness source

`math/rand.Read` is deprecated.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-03-01 17:10:30 +03:00 committed by Gitea
parent e96d83549b
commit b1f7b29848

View file

@ -22,6 +22,7 @@ type (
Generator struct { Generator struct {
vu modules.VU vu modules.VU
size int size int
rand *rand.Rand
buf []byte buf []byte
offset int offset int
} }
@ -35,10 +36,6 @@ type (
// TailSize specifies number of extra random bytes in the buffer tail. // TailSize specifies number of extra random bytes in the buffer tail.
const TailSize = 1024 const TailSize = 1024
func init() {
rand.Seed(time.Now().UnixNano())
}
func NewGenerator(vu modules.VU, size int) Generator { func NewGenerator(vu modules.VU, size int) Generator {
if size <= 0 { if size <= 0 {
panic("size should be positive") panic("size should be positive")
@ -46,6 +43,7 @@ func NewGenerator(vu modules.VU, size int) Generator {
return Generator{ return Generator{
vu: vu, vu: vu,
size: size, size: size,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
buf: make([]byte, size+TailSize), buf: make([]byte, size+TailSize),
} }
} }
@ -66,7 +64,7 @@ func (g *Generator) GenPayload(calcHash bool) GenPayloadResponse {
func (g *Generator) nextSlice() []byte { func (g *Generator) nextSlice() []byte {
if g.offset >= TailSize { if g.offset >= TailSize {
g.offset = 0 g.offset = 0
rand.Read(g.buf) // Per docs, err is always nil here g.rand.Read(g.buf) // Per docs, err is always nil here
} }
result := g.buf[g.offset : g.offset+g.size] result := g.buf[g.offset : g.offset+g.size]