-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
55 lines (45 loc) · 946 Bytes
/
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
package bench
import (
"bytes"
"fmt"
"math/rand"
"strings"
"sync"
)
func assert(flag bool, msgs ...interface{}) {
if !flag {
panic("assertion failed: " + fmt.Sprint(msgs...))
}
}
func must(err error) {
if err != nil {
panic(err)
}
}
func trim(s string) string {
return strings.TrimSpace(s)
}
var alphabet = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandomAlphabetString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(b)
}
func RandomText() string {
n := len(DataSet.Texts)
return DataSet.Texts[rand.Intn(n)] + DataSet.Texts[rand.Intn(n)] + DataSet.Texts[rand.Intn(n)]
}
var bytesBufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func GetBuffer() *bytes.Buffer {
return bytesBufferPool.Get().(*bytes.Buffer)
}
func PutBuffer(buf *bytes.Buffer) {
buf.Reset()
bytesBufferPool.Put(buf)
}