Add validation and rendering benchmarks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
@@ -917,6 +918,48 @@ func BenchmarkJSONValidation(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
var basicValidationBenchmarkResult domain.ValidationResult
|
||||
|
||||
func BenchmarkBasicValidation(b *testing.B) {
|
||||
largeText := bytes.Repeat([]byte("x"), 1<<20)
|
||||
largeWhitespace := bytes.Repeat([]byte(" \t\n"), (1<<20)/3)
|
||||
unicodeWhitespace := []byte("\u2003\u00a0\u3000")
|
||||
benchmarks := []struct {
|
||||
name string
|
||||
body []byte
|
||||
wantValid bool
|
||||
}{
|
||||
{name: "short_text", body: []byte("A concise response."), wantValid: true},
|
||||
{name: "1MiB_nonempty_text", body: largeText, wantValid: true},
|
||||
{name: "whitespace_only_text", body: largeWhitespace},
|
||||
{name: "unicode_whitespace", body: unicodeWhitespace},
|
||||
}
|
||||
validator := NewStandardValidator("")
|
||||
contract := domain.OutputContract{ValidationMode: domain.ValidationBasic}
|
||||
ctx := context.Background()
|
||||
|
||||
for _, benchmark := range benchmarks {
|
||||
b.Run(benchmark.name, func(b *testing.B) {
|
||||
artifact := &domain.Artifact{Body: benchmark.body}
|
||||
result, err := validator.Validate(ctx, artifact, contract)
|
||||
if err != nil || result.IsValid != benchmark.wantValid {
|
||||
b.Fatalf("validate basic output: result=%+v error=%v", result, err)
|
||||
}
|
||||
|
||||
b.ReportAllocs()
|
||||
b.SetBytes(int64(len(benchmark.body)))
|
||||
b.ResetTimer()
|
||||
for range b.N {
|
||||
result, err := validator.Validate(ctx, artifact, contract)
|
||||
if err != nil || result.IsValid != benchmark.wantValid {
|
||||
b.Fatalf("validate basic output: result=%+v error=%v", result, err)
|
||||
}
|
||||
basicValidationBenchmarkResult = result
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONSchemaDialectIsDraft2020(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user