Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
aacebo committed Oct 15, 2024
1 parent 003d0fc commit 8e0ec8e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ if err := schema.Validate("..."); err != nil { // nil
| Union ||
| Custom Error Messages ||
| Custom Rules ||

# Benchmarks

![Benchmarks](./assets/benchmarks.png)
32 changes: 32 additions & 0 deletions any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package owl_test
import (
"encoding/json"
"testing"
"time"

"math/rand"

"github.com/aacebo/owl"
)
Expand Down Expand Up @@ -82,6 +85,35 @@ func TestAny(t *testing.T) {
})
}

func BenchmarkAny(b *testing.B) {
b.Run("any", func(b *testing.B) {
schema := owl.Any()

for i := 0; i < b.N; i++ {
err := schema.Validate(1)

if err != nil {
b.Fatal(err)
}
}
})

b.Run("enum", func(b *testing.B) {
enum := []any{"test", 1, true}
schema := owl.Any().Enum(enum...)
s := rand.NewSource(time.Now().Unix())
r := rand.New(s)

for i := 0; i < b.N; i++ {
err := schema.Validate(enum[r.Intn(3)])

if err != nil {
b.Fatal(err)
}
}
})
}

func ExampleAny() {
schema := owl.Any()

Expand Down
41 changes: 41 additions & 0 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,47 @@ func TestArray(t *testing.T) {
})
}

func BenchmarkArray(b *testing.B) {
b.Run("array", func(b *testing.B) {
schema := owl.Array(owl.String())
value := []string{"test"}

for i := 0; i < b.N; i++ {
err := schema.Validate(value)

if err != nil {
b.Fatal(err)
}
}
})

b.Run("min", func(b *testing.B) {
schema := owl.Array(owl.String()).Min(3)
value := []string{"a", "b", "c"}

for i := 0; i < b.N; i++ {
err := schema.Validate(value)

if err != nil {
b.Fatal(err)
}
}
})

b.Run("max", func(b *testing.B) {
schema := owl.Array(owl.String()).Max(3)
value := []string{"a", "b", "c"}

for i := 0; i < b.N; i++ {
err := schema.Validate(value)

if err != nil {
b.Fatal(err)
}
}
})
}

func ExampleArray() {
schema := owl.Array(owl.String().Required())

Expand Down
Binary file added assets/benchmarks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8e0ec8e

Please sign in to comment.