runBenchmark runs the benchmark for a single engine.
( engine Engine, templates []*unstructured.Unstructured, constraints []*unstructured.Unstructured, reviewObjs []*unstructured.Unstructured, opts *Opts, )
| 129 | |
| 130 | // runBenchmark runs the benchmark for a single engine. |
| 131 | func 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 { |
no test coverage detected
searching dependent graphs…