mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #6765 from xiang90/s
etcd-runner: move string generation to pkg/stringutil
This commit is contained in:
commit
72fc5f7d1b
54
pkg/stringutil/stringutil.go
Normal file
54
pkg/stringutil/stringutil.go
Normal file
@ -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)
|
||||||
|
}
|
@ -18,11 +18,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/etcd/clientv3"
|
"github.com/coreos/etcd/clientv3"
|
||||||
|
"github.com/coreos/etcd/pkg/stringutil"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -40,8 +40,8 @@ func performWatchOnPrefixes(ctx context.Context, getClient getClientFunc, round
|
|||||||
reqRate := 30 // put request per second
|
reqRate := 30 // put request per second
|
||||||
keyPrePrefix := 30 // max number of keyPrePrefixs for put operation
|
keyPrePrefix := 30 // max number of keyPrePrefixs for put operation
|
||||||
|
|
||||||
prefixes := generateUniqueKeys(5, noOfPrefixes)
|
prefixes := stringutil.UniqueStrings(5, noOfPrefixes)
|
||||||
keys := generateRandomKeys(10, keyPrePrefix)
|
keys := stringutil.RandomStrings(10, keyPrePrefix)
|
||||||
|
|
||||||
roundPrefix := fmt.Sprintf("%16x", round)
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user