diff --git a/pkg/stringutil/stringutil.go b/pkg/stringutil/stringutil.go new file mode 100644 index 000000000..7f6aeb8a6 --- /dev/null +++ b/pkg/stringutil/stringutil.go @@ -0,0 +1,54 @@ +// Copyright 2016 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package stringutil + +import "math/rand" + +const ( + chars = "abcdefghijklmnopqrstuvwxyz0123456789" +) + +// UniqueStrings retruns a slice of randomly generated unique strings. +func UniqueStrings(maxlen uint, n int) []string { + exist := make(map[string]bool) + ss := make([]string, 0) + + for len(ss) < n { + s := randomString(maxlen) + if !exist[s] { + exist[s] = true + ss = append(ss, s) + } + } + + return ss +} + +// RandomStrings retruns a slice of randomly generated strings. +func RandomStrings(maxlen uint, n int) []string { + ss := make([]string, 0) + for i := 0; i < n; i++ { + ss = append(ss, randomString(maxlen)) + } + return ss +} + +func randomString(l uint) string { + s := make([]byte, l) + for i := 0; i < int(l); i++ { + s[i] = chars[rand.Intn(len(chars))] + } + return string(s) +} diff --git a/tools/functional-tester/etcd-runner/watcher.go b/tools/functional-tester/etcd-runner/watcher.go index 492838384..39dc856cb 100644 --- a/tools/functional-tester/etcd-runner/watcher.go +++ b/tools/functional-tester/etcd-runner/watcher.go @@ -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 -}