k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/common/slos/network_programming_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package slos
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/prometheus/common/model"
    26  	"github.com/stretchr/testify/assert"
    27  	"k8s.io/perf-tests/clusterloader2/pkg/measurement/common"
    28  	measurementutil "k8s.io/perf-tests/clusterloader2/pkg/measurement/util"
    29  )
    30  
    31  func TestGather(t *testing.T) {
    32  	cases := []struct {
    33  		samples   []*model.Sample
    34  		err       error
    35  		wantData  *measurementutil.PerfData
    36  		wantError error
    37  	}{{
    38  		samples:  []*model.Sample{createSample("0.9", 200.5), createSample("0.5", 100.5), createSample("0.99", 300.5)},
    39  		wantData: createPerfData([]float64{100500, 200500, 300500}),
    40  	}, {
    41  		samples:   []*model.Sample{{Value: 1}},
    42  		wantError: errors.New("got unexpected number of samples: 1"),
    43  	}, {
    44  		samples:   []*model.Sample{{Value: 1}, {Value: 2}, {Value: 3}, {Value: 4}},
    45  		wantError: errors.New("got unexpected number of samples: 4"),
    46  	}}
    47  
    48  	for _, v := range cases {
    49  		fakeExecutor := &fakeExecutor{samples: v.samples, err: v.err}
    50  		testGatherer(t, fakeExecutor, v.wantData, v.wantError)
    51  	}
    52  }
    53  
    54  func testGatherer(t *testing.T, executor common.QueryExecutor, wantData *measurementutil.PerfData, wantError error) {
    55  	g := &netProgGatherer{}
    56  	summaries, err := g.Gather(executor, time.Now(), time.Now(), nil)
    57  	if err != nil {
    58  		if wantError != nil {
    59  			assert.Equal(t, wantError, err)
    60  			return
    61  		}
    62  		t.Errorf("Unexpected error:  %v", err)
    63  	}
    64  	if len(summaries) != 1 {
    65  		t.Errorf("Should have only one summary")
    66  	}
    67  	assert.Equal(t, netProg, summaries[0].SummaryName())
    68  	assert.Equal(t, "json", summaries[0].SummaryExt())
    69  	assert.NotNil(t, summaries[0].SummaryTime())
    70  
    71  	var data measurementutil.PerfData
    72  	if err := json.Unmarshal([]byte(summaries[0].SummaryContent()), &data); err != nil {
    73  		t.Errorf("Error while decoding summary: %v. Summary: %v", err, summaries[0].SummaryContent())
    74  
    75  	}
    76  	assert.Equal(t, wantData, &data)
    77  }
    78  
    79  type fakeExecutor struct {
    80  	samples []*model.Sample
    81  	err     error
    82  }
    83  
    84  func (f *fakeExecutor) Query(query string, queryTime time.Time) ([]*model.Sample, error) {
    85  	if f.err != nil {
    86  		return nil, f.err
    87  	}
    88  	return f.samples, nil
    89  }
    90  
    91  func createSample(p string, l float64) *model.Sample {
    92  	lset := make(model.LabelSet, 1)
    93  	lset["quantile"] = model.LabelValue(p)
    94  	return &model.Sample{
    95  		Value:  model.SampleValue(l),
    96  		Metric: model.Metric(lset),
    97  	}
    98  }
    99  
   100  func createPerfData(p []float64) *measurementutil.PerfData {
   101  	return &measurementutil.PerfData{
   102  		Version: metricVersion,
   103  		DataItems: []measurementutil.DataItem{{
   104  			Data: map[string]float64{
   105  				"Perc50": p[0],
   106  				"Perc90": p[1],
   107  				"Perc99": p[2],
   108  			},
   109  			Unit:   "ms",
   110  			Labels: map[string]string{"Metric": "NetworkProgrammingLatency"},
   111  		}},
   112  	}
   113  }