MCPcopy
hub / github.com/open-policy-agent/gatekeeper / runBenchmark

Function runBenchmark

pkg/gator/bench/bench.go:131–304  ·  view source on GitHub ↗

runBenchmark runs the benchmark for a single engine.

(
	engine Engine,
	templates []*unstructured.Unstructured,
	constraints []*unstructured.Unstructured,
	reviewObjs []*unstructured.Unstructured,
	opts *Opts,
)

Source from the content-addressed store, hash-verified

129
130// runBenchmark runs the benchmark for a single engine.
131func runBenchmark(
132 engine Engine,
133 templates []*unstructured.Unstructured,
134 constraints []*unstructured.Unstructured,
135 reviewObjs []*unstructured.Unstructured,
136 opts *Opts,
137) (*Results, error) {
138 ctx := context.Background()
139 var setupBreakdown SetupBreakdown
140 var skippedTemplates []string
141 var skippedConstraints []string
142 loadedTemplateKinds := make(map[string]bool)
143
144 // Create the client for this engine
145 setupStart := time.Now()
146 clientStart := time.Now()
147 client, err := makeClient(engine, opts.GatherStats)
148 if err != nil {
149 return nil, fmt.Errorf("creating client: %w", err)
150 }
151 setupBreakdown.ClientCreation = time.Since(clientStart)
152
153 // Add templates (with skip support for incompatible templates)
154 templateStart := time.Now()
155 for _, obj := range templates {
156 templ, err := reader.ToTemplate(scheme, obj)
157 if err != nil {
158 return nil, fmt.Errorf("converting template %q: %w", obj.GetName(), err)
159 }
160 _, err = client.AddTemplate(ctx, templ)
161 if err != nil {
162 // Check if this is an engine compatibility issue
163 if errors.Is(err, clienterrors.ErrNoDriver) {
164 skippedTemplates = append(skippedTemplates, obj.GetName())
165 continue
166 }
167 return nil, fmt.Errorf("adding template %q: %w", templ.GetName(), err)
168 }
169 // Track the constraint kind this template creates
170 loadedTemplateKinds[templ.Spec.CRD.Spec.Names.Kind] = true
171 }
172 setupBreakdown.TemplateCompilation = time.Since(templateStart)
173
174 // Check if all templates were skipped
175 loadedTemplateCount := len(templates) - len(skippedTemplates)
176 if loadedTemplateCount == 0 {
177 return nil, fmt.Errorf("no templates compatible with %s engine (all %d templates skipped)", engine, len(templates))
178 }
179
180 // Add constraints (skip those whose template was skipped)
181 constraintStart := time.Now()
182 for _, obj := range constraints {
183 kind := obj.GetKind()
184 if !loadedTemplateKinds[kind] {
185 skippedConstraints = append(skippedConstraints, obj.GetName())
186 continue
187 }
188 if _, err := client.AddConstraint(ctx, obj); err != nil {

Callers 1

RunFunction · 0.85

Calls 13

ToTemplateFunction · 0.92
makeClientFunction · 0.85
runConcurrentBenchmarkFunction · 0.85
runSequentialBenchmarkFunction · 0.85
calculateLatenciesFunction · 0.85
calculateThroughputFunction · 0.85
GetKindMethod · 0.80
ReviewMethod · 0.80
GetNameMethod · 0.65
AddTemplateMethod · 0.65
AddConstraintMethod · 0.65
AddDataMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…