Skip to content

Commit

Permalink
Merge pull request #141 from scylladb/generate_zero_numbers
Browse files Browse the repository at this point in the history
Generate zero numbers
  • Loading branch information
Henrik Johansson authored Jun 18, 2019
2 parents 2c84815 + db5bc60 commit 21daa36
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 108 deletions.
40 changes: 12 additions & 28 deletions datautils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,31 @@ import (
)

func randIntRange(rnd *rand.Rand, min int, max int) int {
return rnd.Intn(max-min) + min
}

func nonEmptyRandIntRange(rnd *rand.Rand, min int, max int, def int) int {
if max > min && min > 0 {
return randIntRange(rnd, min, max)
if max <= min {
return min
}
return randIntRange(rnd, 1, def)
return rnd.Intn(max-min) + min
}

func randInt64Range(rnd *rand.Rand, min int64, max int64) int64 {
return rnd.Int63n(max-min) + min
}

func nonEmptyRandInt64Range(rnd *rand.Rand, min int64, max int64, def int64) int64 {
if max > min && min > 0 {
return randInt64Range(rnd, min, max)
if max <= min {
return min
}
return randInt64Range(rnd, 1, def)
return rnd.Int63n(max-min) + min
}

func randFloat32Range(rnd *rand.Rand, min float32, max float32) float32 {
return rnd.Float32() * (max - min)
}

func nonEmptyRandFloat32Range(rnd *rand.Rand, min float32, max float32, def float32) float32 {
if max > min && min > 0 {
return randFloat32Range(rnd, min, max)
if max <= min {
return min
}
return randFloat32Range(rnd, 1, def)
return rnd.Float32() * (max - min)
}

func randFloat64Range(rnd *rand.Rand, min float64, max float64) float64 {
return rnd.Float64() * (max - min)
}

func nonEmptyRandFloat64Range(rnd *rand.Rand, min float64, max float64, def float64) float64 {
if max > min && min > 0 {
return randFloat64Range(rnd, min, max)
if max <= min {
return min
}
return randFloat64Range(rnd, 1, def)
return rnd.Float64() * (max - min)
}

func randBlobWithTime(rnd *rand.Rand, len int, t time.Time) []byte {
Expand Down
57 changes: 0 additions & 57 deletions datautils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,11 @@ package gemini
import (
"math/rand"
"testing"
"testing/quick"
"time"
)

var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

func TestNonEmptyRandRange(t *testing.T) {
f := func(x, y int) bool {
r := nonEmptyRandIntRange(rnd, 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 := nonEmptyRandIntRange(rnd, x, y, 10)
return r > 0
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}

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

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

var bench_r string

func BenchmarkNonEmptyRandStringWithTime(b *testing.B) {
Expand All @@ -66,19 +25,3 @@ func BenchmarkNonEmptyRandStringWithTimeParallel(b *testing.B) {
}
})
}

var bench_rr int

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

var bench_rr64 int64

func BenchmarkNonEmptyRandRange64(b *testing.B) {
for i := 0; i < b.N; i++ {
bench_rr64 = nonEmptyRandInt64Range(rnd, 0, 50, 30)
}
}
46 changes: 23 additions & 23 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ func (st SimpleType) GenValue(p *PartitionRange) []interface{} {
case TYPE_INET:
val = net.ParseIP(randIpV4Address(p.Rand, p.Rand.Intn(255), 2))
case TYPE_INT:
val = nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10)
val = randIntRange(p.Rand, p.Min, p.Max)
case TYPE_SMALLINT:
val = int16(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
val = int16(randIntRange(p.Rand, p.Min, p.Max))
case TYPE_TIMEUUID, TYPE_UUID:
r := gocql.UUIDFromTime(randTime(p.Rand))
val = r.String()
case TYPE_TINYINT:
val = int8(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
val = int8(randIntRange(p.Rand, p.Min, p.Max))
case TYPE_VARINT:
val = big.NewInt(randInt64Range(p.Rand, int64(p.Min), int64(p.Max)))
default:
Expand All @@ -167,19 +167,19 @@ func (st SimpleType) GenValueRange(p *PartitionRange) ([]interface{}, []interfac
switch st {
case TYPE_ASCII, TYPE_TEXT, TYPE_VARCHAR:
startTime := randTime(p.Rand)
start := nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10)
end := start + nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10)
start := randIntRange(p.Rand, p.Min, p.Max)
end := start + randIntRange(p.Rand, p.Min, p.Max)
left = nonEmptyRandStringWithTime(p.Rand, start, startTime)
right = nonEmptyRandStringWithTime(p.Rand, end, randTimeNewer(p.Rand, startTime))
case TYPE_BLOB:
startTime := randTime(p.Rand)
start := nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10)
end := start + nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10)
start := randIntRange(p.Rand, p.Min, p.Max)
end := start + randIntRange(p.Rand, p.Min, p.Max)
left = hex.EncodeToString(nonEmptyRandBlobWithTime(p.Rand, start, startTime))
right = hex.EncodeToString(nonEmptyRandBlobWithTime(p.Rand, end, randTimeNewer(p.Rand, startTime)))
case TYPE_BIGINT:
start := nonEmptyRandInt64Range(p.Rand, int64(p.Min), int64(p.Max), 10)
end := start + nonEmptyRandInt64Range(p.Rand, int64(p.Min), int64(p.Max), 10)
start := randInt64Range(p.Rand, int64(p.Min), int64(p.Max))
end := start + randInt64Range(p.Rand, int64(p.Min), int64(p.Max))
left = start
right = end
case TYPE_DATE, TYPE_TIME, TYPE_TIMESTAMP:
Expand All @@ -188,23 +188,23 @@ func (st SimpleType) GenValueRange(p *PartitionRange) ([]interface{}, []interfac
left = start
right = end
case TYPE_DECIMAL:
start := nonEmptyRandInt64Range(p.Rand, int64(p.Min), int64(p.Max), 10)
end := start + nonEmptyRandInt64Range(p.Rand, int64(p.Min), int64(p.Max), 10)
start := randInt64Range(p.Rand, int64(p.Min), int64(p.Max))
end := start + randInt64Range(p.Rand, int64(p.Min), int64(p.Max))
left = inf.NewDec(start, 3)
right = inf.NewDec(end, 3)
case TYPE_DOUBLE:
start := nonEmptyRandFloat64Range(p.Rand, float64(p.Min), float64(p.Max), 10)
end := start + nonEmptyRandFloat64Range(p.Rand, float64(p.Min), float64(p.Max), 10)
start := randFloat64Range(p.Rand, float64(p.Min), float64(p.Max))
end := start + randFloat64Range(p.Rand, float64(p.Min), float64(p.Max))
left = start
right = end
case TYPE_DURATION:
start := time.Minute * time.Duration(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
end := start + time.Minute*time.Duration(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
start := time.Minute * time.Duration(randIntRange(p.Rand, p.Min, p.Max))
end := start + time.Minute*time.Duration(randIntRange(p.Rand, p.Min, p.Max))
left = start
right = end
case TYPE_FLOAT:
start := nonEmptyRandFloat32Range(p.Rand, float32(p.Min), float32(p.Max), 10)
end := start + nonEmptyRandFloat32Range(p.Rand, float32(p.Min), float32(p.Max), 10)
start := randFloat32Range(p.Rand, float32(p.Min), float32(p.Max))
end := start + randFloat32Range(p.Rand, float32(p.Min), float32(p.Max))
left = start
right = end
case TYPE_INET:
Expand All @@ -213,13 +213,13 @@ func (st SimpleType) GenValueRange(p *PartitionRange) ([]interface{}, []interfac
left = net.ParseIP(start)
right = net.ParseIP(end)
case TYPE_INT:
start := nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10)
end := start + nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10)
start := randIntRange(p.Rand, p.Min, p.Max)
end := start + randIntRange(p.Rand, p.Min, p.Max)
left = start
right = end
case TYPE_SMALLINT:
start := int16(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
end := start + int16(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
start := int16(randIntRange(p.Rand, p.Min, p.Max))
end := start + int16(randIntRange(p.Rand, p.Min, p.Max))
left = start
right = end
case TYPE_TIMEUUID, TYPE_UUID:
Expand All @@ -228,8 +228,8 @@ func (st SimpleType) GenValueRange(p *PartitionRange) ([]interface{}, []interfac
left = gocql.UUIDFromTime(start).String()
right = gocql.UUIDFromTime(end).String()
case TYPE_TINYINT:
start := int8(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
end := start + int8(nonEmptyRandIntRange(p.Rand, p.Min, p.Max, 10))
start := int8(randIntRange(p.Rand, p.Min, p.Max))
end := start + int8(randIntRange(p.Rand, p.Min, p.Max))
left = start
right = end
case TYPE_VARINT:
Expand Down

0 comments on commit 21daa36

Please sign in to comment.