Skip to content

Commit 520ff28

Browse files
committedJan 21, 2022
add benchmarks for defaults
1 parent a66fffd commit 520ff28

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
 

Diff for: ‎README.md

+16
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,20 @@ BenchmarkDefinitionOpen-16 1404 800391 ns/op
5555
BenchmarkDefinitionClosed-16 1635 770721 ns/op
5656
PASS
5757
ok github.com/harpratap/cue-perf-tests 3.130s
58+
```
59+
60+
## 4. Defaulting
61+
62+
Defaulting allows to provide a value to a type which can be overridden but only once. Adding more defaults also cause slowness
63+
64+
```
65+
go test -bench=BenchmarkDefault -benchtime=5s
66+
goos: darwin
67+
goarch: amd64
68+
pkg: github.com/harpratap/cue-perf-tests
69+
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
70+
BenchmarkDefault0-16 41304 144890 ns/op
71+
BenchmarkDefault10-16 30867 197449 ns/op
72+
PASS
73+
ok github.com/harpratap/cue-perf-tests 20.813s
5874
```

Diff for: ‎defaulting.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"cuelang.org/go/cue"
7+
"cuelang.org/go/cue/cuecontext"
8+
)
9+
10+
const default0Schema = `
11+
#default: {
12+
text1: string
13+
text2: string
14+
text3: string
15+
text4: string
16+
text5: string
17+
text6: string
18+
text7: string
19+
text8: string
20+
text9: string
21+
text10: string
22+
}
23+
`
24+
25+
const default0Val = `
26+
v: #default & {
27+
text1: "hello"
28+
text2: "hello"
29+
text3: "hello"
30+
text4: "hello"
31+
text5: "hello"
32+
text6: "hello"
33+
text7: "hello"
34+
text8: "hello"
35+
text9: "hello"
36+
text10: "hello"
37+
}
38+
`
39+
40+
const default10Schema = `
41+
#default: {
42+
text1: string | *"hello"
43+
text2: string | *"hello"
44+
text3: string | *"hello"
45+
text4: string | *"hello"
46+
text5: string | *"hello"
47+
text6: string | *"hello"
48+
text7: string | *"hello"
49+
text8: string | *"hello"
50+
text9: string | *"hello"
51+
text10: string | *"hello"
52+
}
53+
`
54+
55+
const default10Val = `
56+
v: #default & {
57+
text1: "hello"
58+
text2: "hello"
59+
text3: "hello"
60+
text4: "hello"
61+
text5: "hello"
62+
text6: "hello"
63+
text7: "hello"
64+
text8: "hello"
65+
text9: "hello"
66+
text10: "hello"
67+
}
68+
`
69+
70+
func default0() {
71+
var (
72+
c *cue.Context
73+
s cue.Value
74+
v cue.Value
75+
)
76+
c = cuecontext.New()
77+
s = c.CompileString(default0Schema)
78+
v = c.CompileString(default0Val, cue.Scope(s))
79+
fmt.Sprint(v)
80+
}
81+
82+
func default10() {
83+
var (
84+
c *cue.Context
85+
s cue.Value
86+
v cue.Value
87+
)
88+
c = cuecontext.New()
89+
s = c.CompileString(default10Schema)
90+
v = c.CompileString(default10Val, cue.Scope(s))
91+
fmt.Sprint(v)
92+
}

Diff for: ‎defaulting_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func BenchmarkDefault0(b *testing.B) {
8+
for n := 0; n < b.N; n++ {
9+
default0()
10+
}
11+
}
12+
13+
func BenchmarkDefault10(b *testing.B) {
14+
for n := 0; n < b.N; n++ {
15+
default10()
16+
}
17+
}

Diff for: ‎main.go

+2
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ func main() {
99
conjunctionDouble()
1010
definitionOpen()
1111
defintionClosed()
12+
default0()
13+
default10()
1214
}

0 commit comments

Comments
 (0)
Please sign in to comment.