Skip to content
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

Moved doc building to async to speed things up. #364

Merged
merged 2 commits into from
Nov 9, 2023
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
2 changes: 1 addition & 1 deletion cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func GetLintCommand() *cobra.Command {
Spec: specBytes,
CustomFunctions: customFunctions,
Base: baseFlag,
AllowLookup: remoteFlag,
AllowLookup: remoteFlag,
SkipDocumentCheck: skipCheckFlag,
})

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require (
github.com/gizak/termui/v3 v3.1.0
github.com/json-iterator/go v1.1.12
github.com/mitchellh/mapstructure v1.5.0
github.com/pb33f/libopenapi v0.13.7
github.com/pb33f/libopenapi-validator v0.0.26
github.com/pb33f/libopenapi v0.13.9
github.com/pb33f/libopenapi-validator v0.0.27
github.com/pterm/pterm v0.12.69
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/spf13/cobra v1.7.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.13.7 h1:5ibvwDDMBR21lr4SQvxfLbiQERxxr8Fb8O2ousw1wxA=
github.com/pb33f/libopenapi v0.13.7/go.mod h1:Lv2eEtsAtbRFlF8hjH82L8SIGoUNgemMVoKoB6A9THk=
github.com/pb33f/libopenapi-validator v0.0.26 h1:sDJ7xXX2C+p9TVaujcV7HaSVpULlr99dVyhZBbSTwoM=
github.com/pb33f/libopenapi-validator v0.0.26/go.mod h1:QChIoPXJX8FkAQ9ufF7YX3KBaeUXyMucb/uajw0MaBs=
github.com/pb33f/libopenapi v0.13.9 h1:LQKTjjhYObuw2RUISbzHx+PH6yueht3mNBx3SBVFjOY=
github.com/pb33f/libopenapi v0.13.9/go.mod h1:Lv2eEtsAtbRFlF8hjH82L8SIGoUNgemMVoKoB6A9THk=
github.com/pb33f/libopenapi-validator v0.0.27 h1:eO4kbEs2y5mILoFuFLFadWOWE1td2eDJ2RmzUWFswNk=
github.com/pb33f/libopenapi-validator v0.0.27/go.mod h1:qXHUgYSewsSCOK30AMiwK39u1ZB0YZm5nff7IzYkckA=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
57 changes: 45 additions & 12 deletions motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,32 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
if docResolved == nil {
var err error
// create a new document.
docResolved, err = libopenapi.NewDocumentWithConfiguration(execution.Spec, docConfig)
docUnresolved, _ = libopenapi.NewDocumentWithConfiguration(execution.Spec, docConfig)

done := make(chan bool)

go func() {
docResolved, err = libopenapi.NewDocumentWithConfiguration(execution.Spec, docConfig)
done <- true
}()

go func() {
docUnresolved, _ = libopenapi.NewDocumentWithConfiguration(execution.Spec, docConfig)
done <- true
}()

complete := 0
for complete < 2 {
<-done
complete++
}

if err != nil {
// Done here, we can't do anything else.
return &RuleSetExecutionResult{Errors: []error{err}}
}

specInfo = docResolved.GetSpecInfo()
specInfoUnresolved = docUnresolved.GetSpecInfo()
indexConfig.SpecInfo = specInfo

} else {
Expand Down Expand Up @@ -235,18 +252,32 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
unresRoloConfig := *indexConfig
resRoloConfig := *indexConfig

// create an index for the unresolved spec.
rolodexResolved, _ = BuildRolodexFromIndexConfig(&resRoloConfig)
rolodexResolved.SetRootNode(resRoloConfig.SpecInfo.RootNode)
completeChan := make(chan bool)

go func() {
// create an index for the unresolved spec.
rolodexResolved, _ = BuildRolodexFromIndexConfig(&resRoloConfig)
rolodexResolved.SetRootNode(resRoloConfig.SpecInfo.RootNode)

unResInfo, _ := datamodel.ExtractSpecInfo(*specInfo.SpecBytes)
rolodexUnresolved, _ = BuildRolodexFromIndexConfig(&unresRoloConfig)
rolodexUnresolved.SetRootNode(unResInfo.RootNode)
_ = rolodexResolved.IndexTheRolodex()
rolodexResolved.Resolve()
completeChan <- true
}()

_ = rolodexResolved.IndexTheRolodex()
_ = rolodexUnresolved.IndexTheRolodex()
go func() {
unResInfo, _ := datamodel.ExtractSpecInfo(*specInfo.SpecBytes)
rolodexUnresolved, _ = BuildRolodexFromIndexConfig(&unresRoloConfig)
rolodexUnresolved.SetRootNode(unResInfo.RootNode)

rolodexResolved.Resolve()
_ = rolodexUnresolved.IndexTheRolodex()
completeChan <- true
}()

completedBuilds := 0
for completedBuilds < 2 {
<-completeChan
completedBuilds++
}

indexResolved = rolodexResolved.GetRootIndex()
indexUnresolved = rolodexUnresolved.GetRootIndex()
Expand Down Expand Up @@ -318,7 +349,9 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
for _, rule := range execution.RuleSet.Rules {
ruleSpec := specResolved
ruleIndex := indexResolved
info := specInfo
if !rule.Resolved {
info = specInfoUnresolved
ruleSpec = specUnresolved
ruleIndex = indexUnresolved
}
Expand All @@ -331,7 +364,7 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
ruleResults: &ruleResults,
wg: &ruleWaitGroup,
errors: &errs,
specInfo: specInfo,
specInfo: info,
index: ruleIndex,
document: docResolved,
customFunctions: execution.CustomFunctions,
Expand Down
14 changes: 14 additions & 0 deletions motor/rule_applicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2171,3 +2171,17 @@ func Benchmark_K8sSpecAgainstDefaultRuleSet(b *testing.B) {
assert.NotNil(b, results)
}
}

func Benchmark_StripeSpecAgainstDefaultRuleSet(b *testing.B) {
m, _ := os.ReadFile("../model/test_files/stripe.yaml")
rs := rulesets.BuildDefaultRuleSets()
for n := 0; n < b.N; n++ {
rse := &RuleSetExecution{
RuleSet: rs.GenerateOpenAPIDefaultRuleSet(),
Spec: m,
}
results := ApplyRulesToRuleSet(rse)
assert.Len(b, results.Errors, 0)
assert.NotNil(b, results)
}
}
Loading