etcd-runner: move string generation to pkg/stringutil

This commit is contained in:
Xiang Li
2016-10-31 11:50:40 -07:00
parent 30d37b2165
commit 9f0ee53e86
2 changed files with 57 additions and 43 deletions

View File

@ -18,11 +18,11 @@ import (
"context"
"fmt"
"log"
"math/rand"
"sync"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/stringutil"
"golang.org/x/time/rate"
)
@ -40,8 +40,8 @@ func performWatchOnPrefixes(ctx context.Context, getClient getClientFunc, round
reqRate := 30 // put request per second
keyPrePrefix := 30 // max number of keyPrePrefixs for put operation
prefixes := generateUniqueKeys(5, noOfPrefixes)
keys := generateRandomKeys(10, keyPrePrefix)
prefixes := stringutil.UniqueStrings(5, noOfPrefixes)
keys := stringutil.RandomStrings(10, keyPrePrefix)
roundPrefix := fmt.Sprintf("%16x", round)
@ -167,43 +167,3 @@ func getWithRetry(client *clientv3.Client, ctx context.Context, key string) *cli
}
}
}
func generateUniqueKeys(maxstrlen uint, keynos int) []string {
keyMap := make(map[string]bool)
keys := make([]string, 0)
count := 0
key := ""
for {
key = generateRandomKey(maxstrlen)
_, ok := keyMap[key]
if !ok {
keyMap[key] = true
keys = append(keys, key)
count++
if len(keys) == keynos {
break
}
}
}
return keys
}
func generateRandomKeys(maxstrlen uint, keynos int) []string {
keys := make([]string, 0)
key := ""
for i := 0; i < keynos; i++ {
key = generateRandomKey(maxstrlen)
keys = append(keys, key)
}
return keys
}
func generateRandomKey(strlen uint) string {
chars := "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := 0; i < int(strlen); i++ {
result[i] = chars[rand.Intn(len(chars))]
}
key := string(result)
return key
}