Skip to content

Commit

Permalink
Fix serialization benchmark
Browse files Browse the repository at this point in the history
Made all data generated in such a way that every run
you will get same data.

Changed time.Duration to gocql.Duration for consistency
and set type to SetTypein set testcase
  • Loading branch information
sylwiaszunejko committed Oct 12, 2024
1 parent 06eae1b commit dcfec74
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 19 deletions.
60 changes: 41 additions & 19 deletions tests/bench/bench_marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,58 @@ package bench

import (
"encoding/json"
"io"
"math/rand"
"testing"
"time"

"github.com/brianvoe/gofakeit/v6"
"github.com/gocql/gocql"
)

func generateRandomBinaryData(size int) []byte {
data := make([]byte, size)
rand.Read(data)
return data
rnd := rand.New(rand.NewSource(100))
randomBuffer := make([]byte, size)
io.ReadAtLeast(rnd, randomBuffer, size)
return randomBuffer
}

func generateRandomJSON(size int) string {
data := make(map[string]interface{})
for i := 0; i < size/10; i++ {
data[generateRandomString(5)] = generateRandomString(10)
}
jsonData, _ := json.Marshal(data)
return string(jsonData)
type RandomData struct {
ID string `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Phone string `json:"phone"`
}

func generateRandomString(length int) string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
result := make([]rune, length)
for i := range result {
result[i] = letters[rand.Intn(len(letters))]
func generateRandomJSON(size int) string {
gofakeit.Seed(100)
var jsonData []byte
var randomData []RandomData
currentLength := 0

for currentLength < size {
data := RandomData{
ID: gofakeit.UUID(),
FirstName: gofakeit.FirstName(),
LastName: gofakeit.LastName(),
Email: gofakeit.Email(),
City: gofakeit.City(),
State: gofakeit.State(),
Zip: gofakeit.Zip(),
Phone: gofakeit.Phone(),
}
randomData = append(randomData, data)

tempData, _ := json.Marshal(randomData)
currentLength = len(tempData)
jsonData = tempData
}
return string(result)

return string(jsonData)
}

func BenchmarkSerialization(b *testing.B) {
Expand Down Expand Up @@ -68,7 +91,6 @@ func BenchmarkSerialization(b *testing.B) {
{"Small-100b", 100},
{"Medium-1kb", 1024},
{"Big-1M", 1024 * 1024},
{"Huge-10M", 10 * 1024 * 1024},
}

for _, c := range cases {
Expand Down Expand Up @@ -155,7 +177,7 @@ func BenchmarkSerialization(b *testing.B) {

b.Run("Duration", func(b *testing.B) {
tType := gocql.NewNativeType(4, gocql.TypeDuration, "")
val := time.Duration(5 * time.Minute)
val := gocql.Duration{Nanoseconds: 300000000000}
b.Run("Marshal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := gocql.Marshal(tType, val)
Expand Down Expand Up @@ -268,7 +290,7 @@ func BenchmarkSerialization(b *testing.B) {

b.Run("Set", func(b *testing.B) {
tType := gocql.CollectionType{
NativeType: gocql.NewNativeType(4, gocql.TypeList, ""),
NativeType: gocql.NewNativeType(4, gocql.TypeSet, ""),
Elem: gocql.NewNativeType(4, gocql.TypeInt, ""),
}
val := map[int]struct{}{1: {}, 2: {}}
Expand Down
16 changes: 16 additions & 0 deletions tests/bench/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/gocql/gocql/bench

go 1.22

require (
github.com/brianvoe/gofakeit/v6 v6.28.0
github.com/gocql/gocql v1.7.0
)

require (
github.com/golang/snappy v0.0.3 // indirect
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
)

replace github.com/gocql/gocql => github.com/scylladb/gocql v1.14.4
39 changes: 39 additions & 0 deletions tests/bench/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY=
github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932/go.mod h1:NOuUCSz6Q9T7+igc/hlvDOUdtWKryOrtFyIVABv/p7k=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/scylladb/gocql v1.14.4 h1:MhevwCfyAraQ6RvZYFO3pF4Lt0YhvQlfg8Eo2HEqVQA=
github.com/scylladb/gocql v1.14.4/go.mod h1:ZLEJ0EVE5JhmtxIW2stgHq/v1P4fWap0qyyXSKyV8K0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

0 comments on commit dcfec74

Please sign in to comment.