-
Notifications
You must be signed in to change notification settings - Fork 5
/
benchmark_test.go
128 lines (108 loc) · 2.66 KB
/
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
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
package base62
import (
"crypto/rand"
"encoding/base64"
"math"
"math/big"
"testing"
)
var testRandBytes = make([]byte, 16)
var testEncodedBytes []byte
var testEncodedBase64 []byte
var testInteger = uint64(math.MaxInt64)
var testEncodedInteger = []byte("V8qRkBGKRiP")
func init() {
if _, err := rand.Read(testRandBytes); err != nil {
panic(err)
}
testEncodedBytes = Encode(testRandBytes)
testEncodedBase64 = make([]byte, base64.RawStdEncoding.EncodedLen(len(testRandBytes)))
base64.RawStdEncoding.Encode(testEncodedBase64, testRandBytes)
}
func encodeWithBigInt(b []byte) []byte {
base := big.NewInt(base)
num := new(big.Int).SetBytes(b)
mod := new(big.Int)
ret := make([]byte, 0, len(b)*8/5+1)
for num.BitLen() > 0 {
num.DivMod(num, base, mod)
ret = append(ret, encodeStd[mod.Int64()])
}
return ret
}
func Benchmark_Encode_V2(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_ = StdEncoding._encodeV2(testRandBytes)
}
}
func Benchmark_Encode_legacyV1(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_ = StdEncoding._encodeV1(testRandBytes)
}
}
func Benchmark_EncodeToString(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_ = EncodeToString(testRandBytes)
}
}
func Benchmark_EncodeToBuf(bb *testing.B) {
buf := make([]byte, 0, 1000)
for i := 0; i < bb.N; i++ {
_ = EncodeToBuf(buf, testRandBytes)
}
}
func Benchmark_Decode(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_, _ = Decode(testEncodedBytes)
}
}
func Benchmark_DecodeString(bb *testing.B) {
s := string(testEncodedBytes)
for i := 0; i < bb.N; i++ {
_, _ = DecodeString(s)
}
}
func Benchmark_DecodeToBuf(bb *testing.B) {
buf := make([]byte, 0, 1000)
for i := 0; i < bb.N; i++ {
_, _ = DecodeToBuf(buf, testRandBytes)
}
}
func Benchmark_Encode_BigInt(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_ = encodeWithBigInt(testRandBytes)
}
}
func Benchmark_Base64_EncodeToString(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_ = base64.RawStdEncoding.EncodeToString(testRandBytes)
}
}
func Benchmark_Base64_Encode(bb *testing.B) {
buf := make([]byte, 1000)
for i := 0; i < bb.N; i++ {
base64.RawStdEncoding.Encode(buf, testRandBytes)
}
}
func Benchmark_Base64_DecodeString(bb *testing.B) {
s := string(testEncodedBase64)
for i := 0; i < bb.N; i++ {
_, _ = base64.RawStdEncoding.DecodeString(s)
}
}
func Benchmark_Base64_Decode(bb *testing.B) {
buf := make([]byte, 1000)
for i := 0; i < bb.N; i++ {
_, _ = base64.RawStdEncoding.Decode(buf, testEncodedBase64)
}
}
func Benchmark_EncodeInteger(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_ = FormatUint(testInteger)
}
}
func Benchmark_DecodeInteger(bb *testing.B) {
for i := 0; i < bb.N; i++ {
_, _ = ParseUint(testEncodedInteger)
}
}