Skip to content

[#13] Allow to use english text in the payload #15

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.15.5
github.com/aws/aws-sdk-go-v2/service/s3 v1.26.9
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf
github.com/go-loremipsum/loremipsum v1.1.3
github.com/google/uuid v1.3.0
github.com/nspcc-dev/neo-go v0.99.4
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.8.0
go.etcd.io/bbolt v1.3.6
go.k6.io/k6 v0.38.2
Expand Down Expand Up @@ -54,7 +56,6 @@ require (
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-loremipsum/loremipsum v1.1.3 h1:ZRhA0ZmJ49lGe5HhWeMONr+iGftWDsHfrYBl5ktDXso=
github.com/go-loremipsum/loremipsum v1.1.3/go.mod h1:OJQjXdvwlG9hsyhmMQoT4HOm4DG4l62CYywebw0XBoo=
github.com/go-redis/redis v6.10.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible h1:bopx7t9jyUNX1ebhr0G4gtQWmUOgwQRI0QsYhdYLgkU=
Expand Down
6 changes: 4 additions & 2 deletions internal/datagen/datagen.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package datagen

import (
"strings"

"go.k6.io/k6/js/modules"
)

Expand Down Expand Up @@ -36,7 +38,7 @@ func (d *Datagen) Exports() modules.Exports {
return modules.Exports{Default: d}
}

func (d *Datagen) Generator(size int) *Generator {
g := NewGenerator(d.vu, size)
func (d *Datagen) Generator(size int, typ string) *Generator {
g := NewGenerator(d.vu, size, strings.ToLower(typ))
return &g
}
41 changes: 35 additions & 6 deletions internal/datagen/generator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package datagen

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"math/rand"
"time"

"github.com/dop251/goja"
"github.com/go-loremipsum/loremipsum"
"go.k6.io/k6/js/modules"
)

Expand All @@ -23,6 +25,7 @@ type (
vu modules.VU
size int
buf []byte
typ string
offset int
}

Expand All @@ -35,15 +38,30 @@ type (
// TailSize specifies number of extra random bytes in the buffer tail.
const TailSize = 1024

var payloadTypes = []string{
"text",
"random",
"",
}

func init() {
rand.Seed(time.Now().UnixNano())
}

func NewGenerator(vu modules.VU, size int) Generator {
func NewGenerator(vu modules.VU, size int, typ string) Generator {
if size <= 0 {
panic("size should be positive")
}
return Generator{vu: vu, size: size, buf: nil, offset: 0}
var found bool
for i := range payloadTypes {
if payloadTypes[i] == typ {
found = true
}
}
if !found {
vu.InitEnv().Logger.Info("Unknown payload type '%s', random will be used.", typ)
}
return Generator{vu: vu, size: size, buf: nil, typ: typ, offset: 0}
}

func (g *Generator) GenPayload(calcHash bool) GenPayloadResponse {
Expand All @@ -61,17 +79,28 @@ func (g *Generator) GenPayload(calcHash bool) GenPayloadResponse {

func (g *Generator) nextSlice() []byte {
if g.buf == nil {
// Allocate buffer with extra tail for sliding and populate it with random bytes
g.buf = make([]byte, g.size+TailSize)
rand.Read(g.buf) // Per docs, err is always nil here
switch g.typ {
case "text":
li := loremipsum.New()
b := bytes.NewBuffer(nil)
for b.Len() < g.size+TailSize {
b.WriteString(li.Paragraph())
b.WriteRune('\n')
}
g.buf = b.Bytes()
default:
// Allocate buffer with extra tail for sliding and populate it with random bytes
g.buf = make([]byte, g.size+TailSize)
rand.Read(g.buf) // Per docs, err is always nil here
}
}

result := g.buf[g.offset : g.offset+g.size]

// 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
g.offset += 1
if g.offset >= TailSize {
if g.offset+g.size > len(g.buf) {
g.buf = nil
g.offset = 0
}
Expand Down
10 changes: 5 additions & 5 deletions internal/datagen/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ func TestGenerator(t *testing.T) {

t.Run("fails on negative size", func(t *testing.T) {
require.Panics(t, func() {
_ = NewGenerator(vu, -1)
_ = NewGenerator(vu, -1, "")
})
})

t.Run("fails on zero size", func(t *testing.T) {
require.Panics(t, func() {
_ = NewGenerator(vu, 0)
_ = NewGenerator(vu, 0, "")
})
})

t.Run("creates slice of specified size", func(t *testing.T) {
size := 10
g := NewGenerator(vu, size)
g := NewGenerator(vu, size, "")
slice := g.nextSlice()
require.Len(t, slice, size)
})

t.Run("creates a different slice on each call", func(t *testing.T) {
g := NewGenerator(vu, 1000)
g := NewGenerator(vu, 1000, "")
slice1 := g.nextSlice()
slice2 := g.nextSlice()
// Each slice should be unique (assuming that 1000 random bytes will never coincide
Expand All @@ -43,7 +43,7 @@ func TestGenerator(t *testing.T) {
})

t.Run("keeps generating slices after consuming entire tail", func(t *testing.T) {
g := NewGenerator(vu, 1000)
g := NewGenerator(vu, 1000, "")
initialSlice := g.nextSlice()
for i := 0; i < TailSize; i++ {
g.nextSlice()
Expand Down
2 changes: 1 addition & 1 deletion scenarios/grpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if (registry_enabled && delete_age) {
}


const generator = datagen.generator(1024 * parseInt(__ENV.WRITE_OBJ_SIZE));
const generator = datagen.generator(1024 * parseInt(__ENV.WRITE_OBJ_SIZE), __ENV.PAYLOAD_TYPE || "");

const scenarios = {};

Expand Down
2 changes: 1 addition & 1 deletion scenarios/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const obj_registry = registry_enabled ? registry.open(__ENV.REGISTRY_FILE) : und

const duration = __ENV.DURATION;

const generator = datagen.generator(1024 * parseInt(__ENV.WRITE_OBJ_SIZE));
const generator = datagen.generator(1024 * parseInt(__ENV.WRITE_OBJ_SIZE), __ENV.PAYLOAD_TYPE || "");

const scenarios = {};

Expand Down
1 change: 1 addition & 0 deletions scenarios/run_scenarios.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Scenarios `grpc.js`, `http.js` and `s3.js` support the following options:
* `SLEEP_WRITE` - time interval (in seconds) between writing VU iterations.
* `SLEEP_READ` - time interval (in seconds) between reading VU iterations.
* `SELECTION_SIZE` - size of batch to select for deletion (default: 1000).
* `PAYLOAD_TYPE` - type of an object payload ("random" or "text", default: "random").

Examples of how to use these options are provided below for each scenario.

Expand Down
2 changes: 1 addition & 1 deletion scenarios/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (registry_enabled && delete_age) {
);
}

const generator = datagen.generator(1024 * parseInt(__ENV.WRITE_OBJ_SIZE));
const generator = datagen.generator(1024 * parseInt(__ENV.WRITE_OBJ_SIZE), __ENV.PAYLOAD_TYPE || "");

const scenarios = {};

Expand Down