-
Notifications
You must be signed in to change notification settings - Fork 48
/
util.go
132 lines (120 loc) · 3.11 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2017 CoreOS, Inc.
//
// 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 dbtester
import (
"fmt"
"io"
"io/ioutil"
mrand "math/rand"
"net/http"
"os"
"strings"
"time"
)
func toMillisecond(d time.Duration) float64 {
return d.Seconds() * 1000
}
func assignRequest(ranges []int64, total int64) (rs []int64) {
reqEach := int(float64(total) / float64(len(ranges)))
// truncate 10000th digits
if reqEach > 10000 {
reqEach = (reqEach / 10000) * 10000
}
// truncate 1000th digits
if reqEach > 1000 {
reqEach = (reqEach / 1000) * 1000
}
curSum := int64(0)
rs = make([]int64, len(ranges))
for i := range ranges {
if i < len(ranges)-1 {
rs[i] = int64(reqEach)
curSum += int64(reqEach)
} else {
rs[i] = int64(total) - curSum
}
}
return
}
func toFile(txt, fpath string) error {
f, err := os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0777)
if err != nil {
f, err = os.Create(fpath)
if err != nil {
return err
}
}
defer f.Close()
_, err = f.WriteString(txt)
return err
}
// exist returns true if the file or directory exists.
func exist(fpath string) bool {
st, err := os.Stat(fpath)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
if st.IsDir() {
return true
}
if _, err := os.Stat(fpath); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// gracefulClose drains http.Response.Body until it hits EOF
// and closes it. This prevents TCP/TLS connections from closing,
// therefore available for reuse.
func gracefulClose(resp *http.Response) {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
// sequentialKey returns '00012' when size is 5 and num is 12.
func sequentialKey(size, num int64) string {
txt := fmt.Sprintf("%d", num)
if len(txt) > int(size) {
return txt
}
delta := int(size) - len(txt)
return strings.Repeat("0", delta) + txt
}
func sameKey(size int64) string {
return strings.Repeat("a", int(size))
}
func randBytes(bytesN int64) []byte {
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
src := mrand.NewSource(time.Now().UnixNano())
b := make([]byte, bytesN)
for i, cache, remain := bytesN-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return b
}