From efe33803c4878599db958dcb76c7217b9e82c77f Mon Sep 17 00:00:00 2001 From: odubajDT <93584209+odubajDT@users.noreply.github.com> Date: Mon, 21 Aug 2023 15:43:49 +0200 Subject: [PATCH] perf(metrics-operator): improve performance of storing analysis results (#1905) Signed-off-by: odubajDT --- .../controllers/common/analysis/analysis_evaluator.go | 6 +++--- .../controllers/common/analysis/objective_evaluator.go | 4 ++-- .../controllers/common/analysis/objective_evaluator_test.go | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/metrics-operator/controllers/common/analysis/analysis_evaluator.go b/metrics-operator/controllers/common/analysis/analysis_evaluator.go index 7485f96601..7874606a72 100644 --- a/metrics-operator/controllers/common/analysis/analysis_evaluator.go +++ b/metrics-operator/controllers/common/analysis/analysis_evaluator.go @@ -17,14 +17,14 @@ func NewAnalysisEvaluator(o IObjectiveEvaluator) AnalysisEvaluator { func (ae *AnalysisEvaluator) Evaluate(values map[string]string, ad *v1alpha3.AnalysisDefinition) types.AnalysisResult { result := types.AnalysisResult{ - ObjectiveResults: make([]types.ObjectiveResult, 0, len(ad.Spec.Objectives)), + ObjectiveResults: make([]types.ObjectiveResult, len(ad.Spec.Objectives)), } keyObjectiveFailed := false - for _, objective := range ad.Spec.Objectives { + for index, objective := range ad.Spec.Objectives { // evaluate a single objective and store it's result objectiveResult := ae.ObjectiveEvaluator.Evaluate(values, &objective) - result.ObjectiveResults = append(result.ObjectiveResults, objectiveResult) + result.ObjectiveResults[index] = objectiveResult // count scores result.MaximumScore += float64(objective.Weight) diff --git a/metrics-operator/controllers/common/analysis/objective_evaluator.go b/metrics-operator/controllers/common/analysis/objective_evaluator.go index 796f4e1fbc..a48de180d6 100644 --- a/metrics-operator/controllers/common/analysis/objective_evaluator.go +++ b/metrics-operator/controllers/common/analysis/objective_evaluator.go @@ -1,7 +1,7 @@ package analysis import ( - "errors" + "fmt" "strconv" "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" @@ -52,7 +52,7 @@ func (oe *ObjectiveEvaluator) Evaluate(values map[string]string, obj *v1alpha3.O func getValueFromMap(values map[string]string, key string) (float64, error) { val, ok := values[key] if !ok { - return 0.0, errors.New("required value not available") + return 0.0, fmt.Errorf("required value '%s' not available", key) } floatVal, err := strconv.ParseFloat(val, 64) diff --git a/metrics-operator/controllers/common/analysis/objective_evaluator_test.go b/metrics-operator/controllers/common/analysis/objective_evaluator_test.go index 425058858a..19dcd3f47d 100644 --- a/metrics-operator/controllers/common/analysis/objective_evaluator_test.go +++ b/metrics-operator/controllers/common/analysis/objective_evaluator_test.go @@ -1,7 +1,7 @@ package analysis import ( - "errors" + "fmt" "testing" "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha3" @@ -29,7 +29,7 @@ func TestObjectiveEvaluator_Evaluate(t *testing.T) { mockedEvaluator: &fake.ITargetEvaluatorMock{}, want: types.ObjectiveResult{ Score: 0.0, - Error: errors.New("required value not available"), + Error: fmt.Errorf("required value 'name' not available"), }, }, {