forked from alecthomas/kong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
79 lines (74 loc) · 1.63 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
package kong
import (
"fmt"
"strconv"
"testing"
"github.com/alecthomas/assert/v2"
)
func BenchmarkKong_interpolate(b *testing.B) {
prepareKong := func(t testing.TB, count int) *Kong {
t.Helper()
k := &Kong{
vars: make(Vars, count),
registry: NewRegistry().RegisterDefaults(),
}
for i := 0; i < count; i++ {
helpVar := fmt.Sprintf("help_param%d", i)
k.vars[helpVar] = strconv.Itoa(i)
}
grammar := &struct {
Param0 string `help:"${help_param0}"`
}{}
model, err := build(k, grammar)
assert.NoError(t, err)
for i := 0; i < count; i++ {
model.Node.Flags = append(model.Node.Flags, &Flag{
Value: &Value{
Help: fmt.Sprintf("${help_param%d}", i),
Tag: newEmptyTag(),
},
})
}
k.Model = model
return k
}
for _, count := range []int{5, 500, 5000} {
count := count
b.Run(strconv.Itoa(count), func(b *testing.B) {
var err error
k := prepareKong(b, count)
for i := 0; i < b.N; i++ {
err = k.interpolate(k.Model.Node)
}
assert.NoError(b, err)
b.ReportAllocs()
})
}
}
func Benchmark_interpolateValue(b *testing.B) {
varsLen := 10000
k := &Kong{
vars: make(Vars, 10000),
registry: NewRegistry().RegisterDefaults(),
}
for i := 0; i < varsLen; i++ {
helpVar := fmt.Sprintf("help_param%d", i)
k.vars[helpVar] = strconv.Itoa(i)
}
grammar := struct {
Param9999 string `kong:"cmd,help=${help_param9999}"`
}{}
model, err := build(k, &grammar)
if err != nil {
b.FailNow()
}
k.Model = model
flag := k.Model.Flags[0]
for i := 0; i < b.N; i++ {
err = k.interpolateValue(flag.Value, k.vars)
if err != nil {
b.FailNow()
}
}
b.ReportAllocs()
}