-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refact(validators) Readability improvements: * Introduced private constructors for all validators * All private constructors can use a *SchemaValidatorOptions (generalize options passing pattern) * All exported methods use private constructors (hide the implementation of options) * Readability: reduced the level of indentation with early exit conditions than nested conditions * Object validator: refactored Validate in smaller functions * Removed dead code/commented out code Minor efficiency improvements * In Applies method: reflect replaced by type switch whenever possible * Removed (mostly unusable) debugLog * In object validator: * pattern properties now use the regexp cache readily available * split path may be performed only once * In schema props: keep arrays of pointers []*SchemaValidator and avoid copying a new schema * In default & example validators: * map[string]bool -> map[string]struct{} * clear, don't reallocate the visited map * []validator slice -> [n]validator array * introduced a benchmark follow-up document, based on the existing Kubernetes API fixture Signed-off-by: Frederic BIDON <fredbi@yahoo.com> * fixed typo Signed-off-by: Frederic BIDON <fredbi@yahoo.com> --------- Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
- Loading branch information
Showing
24 changed files
with
1,342 additions
and
658 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Benchmark | ||
|
||
Validating the Kubernetes Swagger API | ||
|
||
v0.22.6: 60,000,000 allocs | ||
``` | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/go-openapi/validate | ||
cpu: AMD Ryzen 7 5800X 8-Core Processor | ||
Benchmark_KubernetesSpec/validating_kubernetes_API-16 1 8549863982 ns/op 7067424936 B/op 59583275 allocs/op | ||
``` | ||
|
||
After refact PR: minor but noticable improvements: 25,000,000 allocs | ||
``` | ||
go test -bench Spec | ||
goos: linux | ||
goarch: amd64 | ||
pkg: github.com/go-openapi/validate | ||
cpu: AMD Ryzen 7 5800X 8-Core Processor | ||
Benchmark_KubernetesSpec/validating_kubernetes_API-16 1 4064535557 ns/op 3379715592 B/op 25320330 allocs/op | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package validate | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/go-openapi/loads" | ||
"github.com/go-openapi/strfmt" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Benchmark_KubernetesSpec(b *testing.B) { | ||
fp := filepath.Join("fixtures", "go-swagger", "canary", "kubernetes", "swagger.json") | ||
doc, err := loads.Spec(fp) | ||
require.NoError(b, err) | ||
require.NotNil(b, doc) | ||
|
||
b.Run("validating kubernetes API", func(b *testing.B) { | ||
b.ResetTimer() | ||
b.ReportAllocs() | ||
|
||
for i := 0; i < b.N; i++ { | ||
validator := NewSpecValidator(doc.Schema(), strfmt.Default) | ||
res, _ := validator.Validate(doc) | ||
if res == nil || !res.IsValid() { | ||
b.FailNow() | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.