k8s.io/kubernetes@v1.29.3/test/e2e/framework/metrics/api.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 metrics
    18  
    19  import (
    20  	"fmt"
    21  
    22  	e2eperftype "k8s.io/kubernetes/test/e2e/perftype"
    23  )
    24  
    25  // APICall is a struct for managing API call.
    26  type APICall struct {
    27  	Resource    string        `json:"resource"`
    28  	Subresource string        `json:"subresource"`
    29  	Verb        string        `json:"verb"`
    30  	Scope       string        `json:"scope"`
    31  	Latency     LatencyMetric `json:"latency"`
    32  	Count       int           `json:"count"`
    33  }
    34  
    35  // APIResponsiveness is a struct for managing multiple API calls.
    36  type APIResponsiveness struct {
    37  	APICalls []APICall `json:"apicalls"`
    38  }
    39  
    40  // SummaryKind returns the summary of API responsiveness.
    41  func (a *APIResponsiveness) SummaryKind() string {
    42  	return "APIResponsiveness"
    43  }
    44  
    45  // PrintHumanReadable returns metrics with JSON format.
    46  func (a *APIResponsiveness) PrintHumanReadable() string {
    47  	return PrettyPrintJSON(a)
    48  }
    49  
    50  // PrintJSON returns metrics of PerfData(50, 90 and 99th percentiles) with JSON format.
    51  func (a *APIResponsiveness) PrintJSON() string {
    52  	return PrettyPrintJSON(APICallToPerfData(a))
    53  }
    54  
    55  func (a *APIResponsiveness) Len() int { return len(a.APICalls) }
    56  func (a *APIResponsiveness) Swap(i, j int) {
    57  	a.APICalls[i], a.APICalls[j] = a.APICalls[j], a.APICalls[i]
    58  }
    59  func (a *APIResponsiveness) Less(i, j int) bool {
    60  	return a.APICalls[i].Latency.Perc99 < a.APICalls[j].Latency.Perc99
    61  }
    62  
    63  // currentAPICallMetricsVersion is the current apicall performance metrics version. We should
    64  // bump up the version each time we make incompatible change to the metrics.
    65  const currentAPICallMetricsVersion = "v1"
    66  
    67  // APICallToPerfData transforms APIResponsiveness to PerfData.
    68  func APICallToPerfData(apicalls *APIResponsiveness) *e2eperftype.PerfData {
    69  	perfData := &e2eperftype.PerfData{Version: currentAPICallMetricsVersion}
    70  	for _, apicall := range apicalls.APICalls {
    71  		item := e2eperftype.DataItem{
    72  			Data: map[string]float64{
    73  				"Perc50": float64(apicall.Latency.Perc50) / 1000000, // us -> ms
    74  				"Perc90": float64(apicall.Latency.Perc90) / 1000000,
    75  				"Perc99": float64(apicall.Latency.Perc99) / 1000000,
    76  			},
    77  			Unit: "ms",
    78  			Labels: map[string]string{
    79  				"Verb":        apicall.Verb,
    80  				"Resource":    apicall.Resource,
    81  				"Subresource": apicall.Subresource,
    82  				"Scope":       apicall.Scope,
    83  				"Count":       fmt.Sprintf("%v", apicall.Count),
    84  			},
    85  		}
    86  		perfData.DataItems = append(perfData.DataItems, item)
    87  	}
    88  	return perfData
    89  }