-
Notifications
You must be signed in to change notification settings - Fork 2
/
macro_benchmark_test.go
104 lines (90 loc) · 1.85 KB
/
macro_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
package specfile
import (
"strings"
"testing"
)
func trim2(str string) string {
str = strings.TrimLeftFunc(str, func(r rune) bool {
return r == '%' || r == '{' || r == '('
})
return strings.TrimRightFunc(str, func(r rune) bool {
return r == '}' || r == ')'
})
}
func BenchmarkTrim(b *testing.B) {
b.ResetTimer()
v := "%{version}"
for i := 0; i < b.N; i++ {
trim(v)
}
}
func BenchmarkTrim2(b *testing.B) {
b.ResetTimer()
v := "%{version}"
for i := 0; i < b.N; i++ {
trim2(v)
}
}
func splitConditionalMacro1(str string) (string, string, bool) {
str = trim(str)
var neg bool
var defaultValue string
// do the ?! and ? judge
if strings.HasPrefix(str, "!?") {
neg = true
str = strings.TrimPrefix(str, "!?")
}
if strings.HasPrefix(str, "?") {
str = strings.TrimPrefix(str, "?")
}
if strings.Contains(str, ":") {
arr := strings.Split(str, ":")
if arr[0] != str {
str = arr[0]
defaultValue = arr[1]
}
}
return str, defaultValue, neg
}
func BenchmarkSplitConditionalMacro(b *testing.B) {
b.ResetTimer()
v := "%{?version}"
for i := 0; i < b.N; i++ {
splitConditionalMacro(v)
}
}
func BenchmarkSplitConditionalMacro_1(b *testing.B) {
b.ResetTimer()
v := "%{!?version:5}"
for i := 0; i < b.N; i++ {
splitConditionalMacro(v)
}
}
func BenchmarkSplitConditionalMacro_2(b *testing.B) {
b.ResetTimer()
v := "%{version}"
for i := 0; i < b.N; i++ {
splitConditionalMacro(v)
}
}
func BenchmarkSplitConditionalMacro1(b *testing.B) {
b.ResetTimer()
v := "%{?version}"
for i := 0; i < b.N; i++ {
splitConditionalMacro1(v)
}
}
func BenchmarkSplitConditionalMacro1_1(b *testing.B) {
b.ResetTimer()
v := "%{!?version:5}"
for i := 0; i < b.N; i++ {
splitConditionalMacro1(v)
}
}
func BenchmarkSplitConditionalMacro1_2(b *testing.B) {
b.ResetTimer()
v := "%{version}"
for i := 0; i < b.N; i++ {
splitConditionalMacro1(v)
}
}