-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
150 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters