[TrueCloudLab#22] datagen: Allocate buffer once

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

View file

@ -43,7 +43,11 @@ func NewGenerator(vu modules.VU, size int) Generator {
if size <= 0 { if size <= 0 {
panic("size should be positive") panic("size should be positive")
} }
return Generator{vu: vu, size: size, buf: nil, offset: 0} return Generator{
vu: vu,
size: size,
buf: make([]byte, size+TailSize),
}
} }
func (g *Generator) GenPayload(calcHash bool) GenPayloadResponse { func (g *Generator) GenPayload(calcHash bool) GenPayloadResponse {
@ -60,9 +64,8 @@ func (g *Generator) GenPayload(calcHash bool) GenPayloadResponse {
} }
func (g *Generator) nextSlice() []byte { func (g *Generator) nextSlice() []byte {
if g.buf == nil { if g.offset >= TailSize {
// Allocate buffer with extra tail for sliding and populate it with random bytes g.offset = 0
g.buf = make([]byte, g.size+TailSize)
rand.Read(g.buf) // Per docs, err is always nil here rand.Read(g.buf) // Per docs, err is always nil here
} }
@ -71,10 +74,5 @@ func (g *Generator) nextSlice() []byte {
// Shift the offset for the next call. If we've used our entire tail, then erase // Shift the offset for the next call. If we've used our entire tail, then erase
// the buffer so that on the next call it is regenerated anew // the buffer so that on the next call it is regenerated anew
g.offset += 1 g.offset += 1
if g.offset >= TailSize {
g.buf = nil
g.offset = 0
}
return result return result
} }