|
| 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 | + "time" |
| 22 | + |
| 23 | + e2eperftype "k8s.io/kubernetes/test/e2e/perftype" |
| 24 | +) |
| 25 | + |
| 26 | +// APICall is a struct for managing API call. |
| 27 | +type APICall struct { |
| 28 | + Resource string `json:"resource"` |
| 29 | + Subresource string `json:"subresource"` |
| 30 | + Verb string `json:"verb"` |
| 31 | + Scope string `json:"scope"` |
| 32 | + Latency LatencyMetric `json:"latency"` |
| 33 | + Count int `json:"count"` |
| 34 | +} |
| 35 | + |
| 36 | +// APIResponsiveness is a struct for managing multiple API calls. |
| 37 | +type APIResponsiveness struct { |
| 38 | + APICalls []APICall `json:"apicalls"` |
| 39 | +} |
| 40 | + |
| 41 | +// SummaryKind returns the summary of API responsiveness. |
| 42 | +func (a *APIResponsiveness) SummaryKind() string { |
| 43 | + return "APIResponsiveness" |
| 44 | +} |
| 45 | + |
| 46 | +// PrintHumanReadable returns metrics with JSON format. |
| 47 | +func (a *APIResponsiveness) PrintHumanReadable() string { |
| 48 | + return PrettyPrintJSON(a) |
| 49 | +} |
| 50 | + |
| 51 | +// PrintJSON returns metrics of PerfData(50, 90 and 99th percentiles) with JSON format. |
| 52 | +func (a *APIResponsiveness) PrintJSON() string { |
| 53 | + return PrettyPrintJSON(APICallToPerfData(a)) |
| 54 | +} |
| 55 | + |
| 56 | +func (a *APIResponsiveness) Len() int { return len(a.APICalls) } |
| 57 | +func (a *APIResponsiveness) Swap(i, j int) { |
| 58 | + a.APICalls[i], a.APICalls[j] = a.APICalls[j], a.APICalls[i] |
| 59 | +} |
| 60 | +func (a *APIResponsiveness) Less(i, j int) bool { |
| 61 | + return a.APICalls[i].Latency.Perc99 < a.APICalls[j].Latency.Perc99 |
| 62 | +} |
| 63 | + |
| 64 | +// Set request latency for a particular quantile in the APICall metric entry (creating one if necessary). |
| 65 | +// 0 <= quantile <=1 (e.g. 0.95 is 95%tile, 0.5 is median) |
| 66 | +// Only 0.5, 0.9 and 0.99 quantiles are supported. |
| 67 | +func (a *APIResponsiveness) addMetricRequestLatency(resource, subresource, verb, scope string, quantile float64, latency time.Duration) { |
| 68 | + for i, apicall := range a.APICalls { |
| 69 | + if apicall.Resource == resource && apicall.Subresource == subresource && apicall.Verb == verb && apicall.Scope == scope { |
| 70 | + a.APICalls[i] = setQuantileAPICall(apicall, quantile, latency) |
| 71 | + return |
| 72 | + } |
| 73 | + } |
| 74 | + apicall := setQuantileAPICall(APICall{Resource: resource, Subresource: subresource, Verb: verb, Scope: scope}, quantile, latency) |
| 75 | + a.APICalls = append(a.APICalls, apicall) |
| 76 | +} |
| 77 | + |
| 78 | +// 0 <= quantile <=1 (e.g. 0.95 is 95%tile, 0.5 is median) |
| 79 | +// Only 0.5, 0.9 and 0.99 quantiles are supported. |
| 80 | +func setQuantileAPICall(apicall APICall, quantile float64, latency time.Duration) APICall { |
| 81 | + setQuantile(&apicall.Latency, quantile, latency) |
| 82 | + return apicall |
| 83 | +} |
| 84 | + |
| 85 | +// Only 0.5, 0.9 and 0.99 quantiles are supported. |
| 86 | +func setQuantile(metric *LatencyMetric, quantile float64, latency time.Duration) { |
| 87 | + switch quantile { |
| 88 | + case 0.5: |
| 89 | + metric.Perc50 = latency |
| 90 | + case 0.9: |
| 91 | + metric.Perc90 = latency |
| 92 | + case 0.99: |
| 93 | + metric.Perc99 = latency |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Add request count to the APICall metric entry (creating one if necessary). |
| 98 | +func (a *APIResponsiveness) addMetricRequestCount(resource, subresource, verb, scope string, count int) { |
| 99 | + for i, apicall := range a.APICalls { |
| 100 | + if apicall.Resource == resource && apicall.Subresource == subresource && apicall.Verb == verb && apicall.Scope == scope { |
| 101 | + a.APICalls[i].Count += count |
| 102 | + return |
| 103 | + } |
| 104 | + } |
| 105 | + apicall := APICall{Resource: resource, Subresource: subresource, Verb: verb, Count: count, Scope: scope} |
| 106 | + a.APICalls = append(a.APICalls, apicall) |
| 107 | +} |
| 108 | + |
| 109 | +// currentAPICallMetricsVersion is the current apicall performance metrics version. We should |
| 110 | +// bump up the version each time we make incompatible change to the metrics. |
| 111 | +const currentAPICallMetricsVersion = "v1" |
| 112 | + |
| 113 | +// APICallToPerfData transforms APIResponsiveness to PerfData. |
| 114 | +func APICallToPerfData(apicalls *APIResponsiveness) *e2eperftype.PerfData { |
| 115 | + perfData := &e2eperftype.PerfData{Version: currentAPICallMetricsVersion} |
| 116 | + for _, apicall := range apicalls.APICalls { |
| 117 | + item := e2eperftype.DataItem{ |
| 118 | + Data: map[string]float64{ |
| 119 | + "Perc50": float64(apicall.Latency.Perc50) / 1000000, // us -> ms |
| 120 | + "Perc90": float64(apicall.Latency.Perc90) / 1000000, |
| 121 | + "Perc99": float64(apicall.Latency.Perc99) / 1000000, |
| 122 | + }, |
| 123 | + Unit: "ms", |
| 124 | + Labels: map[string]string{ |
| 125 | + "Verb": apicall.Verb, |
| 126 | + "Resource": apicall.Resource, |
| 127 | + "Subresource": apicall.Subresource, |
| 128 | + "Scope": apicall.Scope, |
| 129 | + "Count": fmt.Sprintf("%v", apicall.Count), |
| 130 | + }, |
| 131 | + } |
| 132 | + perfData.DataItems = append(perfData.DataItems, item) |
| 133 | + } |
| 134 | + return perfData |
| 135 | +} |
0 commit comments