Skip to content

add benchmarks for defaults #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,20 @@ BenchmarkDefinitionOpen-16 1404 800391 ns/op
BenchmarkDefinitionClosed-16 1635 770721 ns/op
PASS
ok github.com/harpratap/cue-perf-tests 3.130s
```

## 4. Defaulting

Defaulting allows to provide a value to a type which can be overridden but only once. Adding more defaults also cause slowness

```
go test -bench=BenchmarkDefault -benchtime=5s
goos: darwin
goarch: amd64
pkg: github.com/harpratap/cue-perf-tests
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkDefault0-16 41304 144890 ns/op
BenchmarkDefault10-16 30867 197449 ns/op
PASS
ok github.com/harpratap/cue-perf-tests 20.813s
```
92 changes: 92 additions & 0 deletions defaulting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"fmt"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
)

const default0Schema = `
#default: {
text1: string
text2: string
text3: string
text4: string
text5: string
text6: string
text7: string
text8: string
text9: string
text10: string
}
`

const default0Val = `
v: #default & {
text1: "hello"
text2: "hello"
text3: "hello"
text4: "hello"
text5: "hello"
text6: "hello"
text7: "hello"
text8: "hello"
text9: "hello"
text10: "hello"
}
`

const default10Schema = `
#default: {
text1: string | *"hello"
text2: string | *"hello"
text3: string | *"hello"
text4: string | *"hello"
text5: string | *"hello"
text6: string | *"hello"
text7: string | *"hello"
text8: string | *"hello"
text9: string | *"hello"
text10: string | *"hello"
}
`

const default10Val = `
v: #default & {
text1: "hello"
text2: "hello"
text3: "hello"
text4: "hello"
text5: "hello"
text6: "hello"
text7: "hello"
text8: "hello"
text9: "hello"
text10: "hello"
}
`

func default0() {
var (
c *cue.Context
s cue.Value
v cue.Value
)
c = cuecontext.New()
s = c.CompileString(default0Schema)
v = c.CompileString(default0Val, cue.Scope(s))
fmt.Sprint(v)
}

func default10() {
var (
c *cue.Context
s cue.Value
v cue.Value
)
c = cuecontext.New()
s = c.CompileString(default10Schema)
v = c.CompileString(default10Val, cue.Scope(s))
fmt.Sprint(v)
}
17 changes: 17 additions & 0 deletions defaulting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"testing"
)

func BenchmarkDefault0(b *testing.B) {
for n := 0; n < b.N; n++ {
default0()
}
}

func BenchmarkDefault10(b *testing.B) {
for n := 0; n < b.N; n++ {
default10()
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ func main() {
conjunctionDouble()
definitionOpen()
defintionClosed()
default0()
default10()
}