-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_benchmark_test.go
75 lines (61 loc) · 1.06 KB
/
utils_benchmark_test.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
package main
import (
"strings"
"testing"
"math/rand"
)
func BenchmarkSplit(b *testing.B) {
str := "key=value,key1=value1"
for i := 0; i < b.N; i++ {
split(str, ",", 2)
}
}
func BenchmarkStringsSplit(b *testing.B) {
str := "key=value,key1=value1"
for i := 0; i < b.N; i++ {
strings.Split(str, ",")
}
}
func genRandByteSlice() [][]byte {
magicNum := 100
var size int
for {
size = rand.Intn(magicNum)
if size > 0 {
break
}
}
res := make([][]byte, 0, size)
for i := 0; i < size; i++ {
var size1 int
for {
size1 = rand.Intn(magicNum)
if size1 > 0 {
break
}
}
token := make([]byte, size1)
for {
rand.Read(token)
if token[0] > 0 {
break
}
}
res = append(res, token)
}
return res
}
var benchmarkByteData = genRandByteSlice()
func BenchmarkAppendByte(b *testing.B) {
for i := 0; i < b.N; i++ {
appendByte(benchmarkByteData...)
}
}
func BenchmarkBuiltinAppend(b *testing.B) {
tmp := make([]byte, 0)
for i := 0; i < b.N; i++ {
for _, v := range benchmarkByteData {
tmp = append(tmp, v...)
}
}
}