Skip to content

Commit af72cd2

Browse files
committed
refactoring schema performance and clarity
1 parent 65096b4 commit af72cd2

20 files changed

+2432
-1512
lines changed

datamodel/low/base/schema.go

Lines changed: 6 additions & 1467 deletions
Large diffs are not rendered by default.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2022-2026 Princess B33f Heavy Industries / Dave Shanley
2+
// SPDX-License-Identifier: MIT
3+
4+
package base
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/pb33f/libopenapi/datamodel/low"
11+
"go.yaml.in/yaml/v4"
12+
)
13+
14+
func benchmarkSchemaRootNode(b *testing.B) *yaml.Node {
15+
b.Helper()
16+
17+
var rootNode yaml.Node
18+
if err := yaml.Unmarshal([]byte(test_get_schema_blob()), &rootNode); err != nil {
19+
b.Fatalf("failed to unmarshal benchmark schema: %v", err)
20+
}
21+
if len(rootNode.Content) == 0 || rootNode.Content[0] == nil {
22+
b.Fatal("failed to unmarshal benchmark schema: empty root")
23+
}
24+
return rootNode.Content[0]
25+
}
26+
27+
func benchmarkBuiltSchema(b *testing.B) *Schema {
28+
b.Helper()
29+
30+
rootNode := benchmarkSchemaRootNode(b)
31+
schema := new(Schema)
32+
if err := low.BuildModel(rootNode, schema); err != nil {
33+
b.Fatalf("failed to build low-level schema model: %v", err)
34+
}
35+
if err := schema.Build(context.Background(), rootNode, nil); err != nil {
36+
b.Fatalf("failed to build schema: %v", err)
37+
}
38+
return schema
39+
}
40+
41+
func BenchmarkSchema_Build(b *testing.B) {
42+
rootNode := benchmarkSchemaRootNode(b)
43+
ctx := context.Background()
44+
45+
b.ReportAllocs()
46+
b.ResetTimer()
47+
48+
for i := 0; i < b.N; i++ {
49+
var schema Schema
50+
if err := low.BuildModel(rootNode, &schema); err != nil {
51+
b.Fatalf("build model failed: %v", err)
52+
}
53+
if err := schema.Build(ctx, rootNode, nil); err != nil {
54+
b.Fatalf("schema build failed: %v", err)
55+
}
56+
}
57+
}
58+
59+
func BenchmarkSchema_QuickHash(b *testing.B) {
60+
schema := benchmarkBuiltSchema(b)
61+
62+
ClearSchemaQuickHashMap()
63+
if hash := schema.QuickHash(); hash == 0 {
64+
b.Fatal("benchmark setup failed: quick hash returned zero")
65+
}
66+
67+
b.ReportAllocs()
68+
b.ResetTimer()
69+
70+
for i := 0; i < b.N; i++ {
71+
ClearSchemaQuickHashMap()
72+
if hash := schema.QuickHash(); hash == 0 {
73+
b.Fatal("quick hash returned zero")
74+
}
75+
}
76+
}
77+
78+
func BenchmarkSchema_QuickHash_Cached(b *testing.B) {
79+
schema := benchmarkBuiltSchema(b)
80+
if hash := schema.QuickHash(); hash == 0 {
81+
b.Fatal("benchmark setup failed: quick hash returned zero")
82+
}
83+
84+
b.ReportAllocs()
85+
b.ResetTimer()
86+
87+
for i := 0; i < b.N; i++ {
88+
if hash := schema.QuickHash(); hash == 0 {
89+
b.Fatal("quick hash returned zero")
90+
}
91+
}
92+
}
93+
94+
func BenchmarkSchema_HashSingle(b *testing.B) {
95+
schema := benchmarkBuiltSchema(b)
96+
if hash := schema.Hash(); hash == 0 {
97+
b.Fatal("benchmark setup failed: hash returned zero")
98+
}
99+
100+
b.ReportAllocs()
101+
b.ResetTimer()
102+
103+
for i := 0; i < b.N; i++ {
104+
if hash := schema.Hash(); hash == 0 {
105+
b.Fatal("hash returned zero")
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)