Skip to content

Commit

Permalink
schema: Testing data gen functions.
Browse files Browse the repository at this point in the history
The data generation functions are broken into
a separate file and an accompanying test file with
both some unit tests and some benchmarks were added.
  • Loading branch information
Henrik Johansson committed Mar 14, 2019
1 parent 13d308e commit a2e874c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 69 deletions.
76 changes: 76 additions & 0 deletions datautils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gemini

import (
"encoding/base64"
"math/rand"
"strings"
"time"

"github.com/segmentio/ksuid"
)

func randRange(min int, max int) int {
return rand.Intn(max-min) + min
}

func nonEmptyRandRange(min int, max int, def int) int {
if max > min && min > 0 {
return randRange(min, max)
}
return randRange(1, def)
}

func randRange64(min int64, max int64) int64 {
return rand.Int63n(max-min) + min
}

func nonEmptyRandRange64(min int64, max int64, def int64) int64 {
if max > min && min > 0 {
return randRange64(min, max)
}
return randRange64(1, def)
}

func randString(len int) string {
return nonEmptyRandStringWithTime(len, time.Now().UTC())
}

func randStringWithTime(len int, t time.Time) string {
id, _ := ksuid.NewRandomWithTime(t)

var buf strings.Builder
buf.WriteString(id.String())
if buf.Len() >= len {
return buf.String()[:len]
}

// Pad some extra random data
buff := make([]byte, len-buf.Len())
rand.Read(buff)
buf.WriteString(base64.StdEncoding.EncodeToString(buff))

return buf.String()[:len]
}

func nonEmptyRandStringWithTime(len int, t time.Time) string {
if len <= 0 {
len = 1
}
return randStringWithTime(len, t)
}

func randDate() time.Time {
min := time.Date(1970, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
max := time.Date(2024, 1, 0, 0, 0, 0, 0, time.UTC).Unix()

sec := rand.Int63n(max-min) + min
return time.Unix(sec, 0)
}

func randDateNewer(d time.Time) time.Time {
min := time.Date(d.Year()+1, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
max := time.Date(2024, 1, 0, 0, 0, 0, 0, time.UTC).Unix()

sec := rand.Int63n(max-min+1) + min
return time.Unix(sec, 0)
}
74 changes: 74 additions & 0 deletions datautils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package gemini

import (
"testing"
"testing/quick"
"time"
)

func TestNonEmptyRandRange(t *testing.T) {
f := func(x, y int) bool {
r := nonEmptyRandRange(x, y, 10)
return r > 0
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}

func TestNonEmptyRandRange64(t *testing.T) {
f := func(x, y int) bool {
r := nonEmptyRandRange(x, y, 10)
return r > 0
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}

func TestNonEmptyRandString(t *testing.T) {
// TODO: Figure out why this is so horribly slow...
tt := time.Now()
f := func(len int32) bool {
r := nonEmptyRandStringWithTime(int(len), tt)
return r != ""
}
cfg := &quick.Config{MaxCount: 10}
if err := quick.Check(f, cfg); err != nil {
t.Error(err)
}
}

var bench_r string

func BenchmarkNonEmptyRandStringWithTime(b *testing.B) {
tt := time.Now()
for i := 0; i < b.N; i++ {
bench_r = nonEmptyRandStringWithTime(30, tt)
}
}

func BenchmarkNonEmptyRandStringWithTimeParallel(b *testing.B) {
tt := time.Now()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
bench_r = nonEmptyRandStringWithTime(30, tt)
}
})
}

var bench_rr int

func BenchmarkNonEmptyRandRange(b *testing.B) {
for i := 0; i < b.N; i++ {
bench_rr = nonEmptyRandRange(0, 50, 30)
}
}

var bench_rr64 int64

func BenchmarkNonEmptyRandRange64(b *testing.B) {
for i := 0; i < b.N; i++ {
bench_rr64 = nonEmptyRandRange64(0, 50, 30)
}
}
69 changes: 0 additions & 69 deletions schema.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package gemini

import (
"encoding/base64"
"fmt"
"math/rand"
"strings"
"time"

"github.com/gocql/gocql"
"github.com/segmentio/ksuid"
)

type Keyspace struct {
Expand Down Expand Up @@ -42,72 +39,6 @@ type PartitionRange struct {
Max int `default:100`
}

func randRange(min int, max int) int {
return rand.Intn(max-min) + min
}

func nonEmptyRandRange(min int, max int, def int) int {
if min >= max {
return randRange(min, min+def)
}
return randRange(min, max)
}

func randRange64(min int64, max int64) int64 {
return rand.Int63n(max-min) + min
}

func nonEmptyRandRange64(min int64, max int64, def int64) int64 {
if min >= max {
return randRange64(min, min+def)
}
return randRange64(min, max)
}

func randString(len int) string {
return nonEmptyRandStringWithTime(len, time.Now().UTC())
}

func randStringWithTime(len int, t time.Time) string {
id, _ := ksuid.NewRandomWithTime(t)

var buf strings.Builder
buf.WriteString(id.String())
if buf.Len() >= len {
return buf.String()[:len]
}

// Pad some extra random data
buff := make([]byte, len-buf.Len())
rand.Read(buff)
buf.WriteString(base64.StdEncoding.EncodeToString(buff))

return buf.String()[:len]
}

func nonEmptyRandStringWithTime(len int, t time.Time) string {
if len <= 0 {
len = 1
}
return randStringWithTime(len, t)
}

func randDate() time.Time {
min := time.Date(1970, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
max := time.Date(2024, 1, 0, 0, 0, 0, 0, time.UTC).Unix()

sec := rand.Int63n(max-min) + min
return time.Unix(sec, 0)
}

func randDateNewer(d time.Time) time.Time {
min := time.Date(d.Year()+1, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
max := time.Date(2024, 1, 0, 0, 0, 0, 0, time.UTC).Unix()

sec := rand.Int63n(max-min+1) + min
return time.Unix(sec, 0)
}

func (s *Schema) GetDropSchema() []string {
return []string{
fmt.Sprintf("DROP KEYSPACE IF EXISTS %s", s.Keyspace.Name),
Expand Down

0 comments on commit a2e874c

Please sign in to comment.