Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
delphinus
committed
May 21, 2017
1 parent
9b86e7b
commit ab1aa63
Showing
3 changed files
with
205 additions
and
0 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
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
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,187 @@ | ||
package randomString | ||
|
||
// http://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-testLength-in-golang | ||
|
||
import ( | ||
"math" | ||
"math/rand" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const testLetterBytes = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789" | ||
const testLength = 256 | ||
|
||
var testLetterRunes = []rune(testLetterBytes) | ||
var testMutex sync.Mutex | ||
var testSrc = rand.NewSource(time.Now().UnixNano()) | ||
var testBits = uint(math.Log2(float64(len(testLetterBytes)))) + 1 | ||
var testMask = int64(1<<testBits - 1) | ||
var testMax = 63 / testBits | ||
|
||
func randStringRunes(n int) string { | ||
by := make([]rune, n) | ||
for i := range by { | ||
by[i] = testLetterRunes[rand.Intn(len(testLetterRunes))] | ||
} | ||
return string(by) | ||
} | ||
|
||
func randStringBytes(n int) string { | ||
by := make([]byte, n) | ||
for i := range by { | ||
by[i] = testLetterBytes[rand.Intn(len(testLetterBytes))] | ||
} | ||
return string(by) | ||
} | ||
|
||
func randStringRemainder(n int) string { | ||
by := make([]byte, n) | ||
for i := range by { | ||
by[i] = testLetterBytes[rand.Int63()%int64(len(testLetterBytes))] | ||
} | ||
return string(by) | ||
} | ||
|
||
func randStringMask(n int) string { | ||
by := make([]byte, n) | ||
for i := 0; i < n; { | ||
if idx := int(rand.Int63() & testMask); idx < len(testLetterBytes) { | ||
by[i] = testLetterBytes[idx] | ||
i++ | ||
} | ||
} | ||
return string(by) | ||
} | ||
|
||
func randStringMaskImproved(n int) string { | ||
by := make([]byte, n) | ||
for i, cache, remain := n-1, rand.Int63(), testMax; i >= 0; { | ||
if remain == 0 { | ||
cache, remain = rand.Int63(), testMax | ||
} | ||
if idx := int(cache & testMask); idx < len(testLetterBytes) { | ||
by[i] = testLetterBytes[idx] | ||
i-- | ||
} | ||
cache >>= testBits | ||
remain-- | ||
} | ||
return string(by) | ||
} | ||
|
||
func randStringSource(n int) string { | ||
by := make([]byte, n) | ||
testMutex.Lock() | ||
cache := testSrc.Int63() | ||
testMutex.Unlock() | ||
for i, remain := n-1, testMax; i >= 0; { | ||
if remain == 0 { | ||
testMutex.Lock() | ||
cache = testSrc.Int63() | ||
testMutex.Unlock() | ||
remain = testMax | ||
} | ||
if idx := int(cache & testMask); idx < len(testLetterBytes) { | ||
by[i] = testLetterBytes[idx] | ||
i-- | ||
} | ||
cache >>= testBits | ||
remain-- | ||
} | ||
return string(by) | ||
} | ||
|
||
func BenchmarkRandomString(b *testing.B) { | ||
b.Run("Runes", func(b *testing.B) { | ||
/* | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = randStringRunes(testLength) | ||
} | ||
}) | ||
*/ | ||
for i := 0; i < b.N; i++ { | ||
_ = randStringRunes(testLength) | ||
} | ||
}) | ||
|
||
b.Run("Bytes", func(b *testing.B) { | ||
/* | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = randStringBytes(testLength) | ||
} | ||
}) | ||
*/ | ||
for i := 0; i < b.N; i++ { | ||
_ = randStringBytes(testLength) | ||
} | ||
}) | ||
|
||
b.Run("Remainder", func(b *testing.B) { | ||
/* | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = randStringRemainder(testLength) | ||
} | ||
}) | ||
*/ | ||
for i := 0; i < b.N; i++ { | ||
_ = randStringRemainder(testLength) | ||
} | ||
}) | ||
|
||
b.Run("Mask", func(b *testing.B) { | ||
/* | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = randStringMask(testLength) | ||
} | ||
}) | ||
*/ | ||
for i := 0; i < b.N; i++ { | ||
_ = randStringMask(testLength) | ||
} | ||
}) | ||
|
||
b.Run("MaskImproved", func(b *testing.B) { | ||
/* | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = randStringMaskImproved(testLength) | ||
} | ||
}) | ||
*/ | ||
for i := 0; i < b.N; i++ { | ||
_ = randStringMaskImproved(testLength) | ||
} | ||
}) | ||
|
||
b.Run("MaskSource", func(b *testing.B) { | ||
/* | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = randStringSource(testLength) | ||
} | ||
}) | ||
*/ | ||
for i := 0; i < b.N; i++ { | ||
_ = randStringSource(testLength) | ||
} | ||
}) | ||
|
||
b.Run("Generate", func(b *testing.B) { | ||
/* | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = Generate(testLength) | ||
} | ||
}) | ||
*/ | ||
for i := 0; i < b.N; i++ { | ||
_ = Generate(testLength) | ||
} | ||
}) | ||
} |