Permalink
Browse files

improve random string generation

  • Loading branch information...
1 parent 9bbe9ad commit b6182ef5c9b0901fb816e71c31685cef4da14891 @pavel-paulau committed Oct 8, 2012
Showing with 13 additions and 8 deletions.
  1. +11 −0 workloads/common.go
  2. +2 −8 workloads/default.go
View
@@ -45,3 +45,14 @@ func Hash(inString string) string {
h.Write([]byte(inString))
return hex.EncodeToString(h.Sum(nil))
}
+
+func RandString(key string, expectedLength int) string {
+ var randString string
+ if expectedLength > 64 {
+ baseString := RandString(key, expectedLength / 2)
+ randString = baseString + baseString
+ } else {
+ randString = (Hash(key) + Hash(key[:len(key)-1]))[:expectedLength]
+ }
+ return randString
+}
View
@@ -1,7 +1,6 @@
package workloads
import (
- "bytes"
"log"
"math"
"math/rand"
@@ -54,13 +53,8 @@ func (w *Default) GenerateValue(key string,
}
// Generate value body in order to meet value size specification
fieldName := "field" + strconv.Itoa(indexableFields)
- buffer := bytes.Buffer{}
- bodyHash := Hash(key)
- iterations := (size - len(fieldName+"-"+key[:10])*indexableFields) / 32
- for i := 0; i < iterations; i++ {
- buffer.WriteString(bodyHash)
- }
- value[fieldName] = buffer.String()
+ expectedLength := size - len(fieldName + "-" + key[:10]) *indexableFields
+ value[fieldName] = RandString(key, expectedLength)
return value
}

0 comments on commit b6182ef

Please sign in to comment.