perf_util.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "fmt"
  16. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  17. "k8s.io/kubernetes/test/e2e/perftype"
  18. )
  19. // TODO(random-liu): Change the tests to actually use PerfData from the beginning instead of
  20. // translating one to the other here.
  21. // currentAPICallMetricsVersion is the current apicall performance metrics version. We should
  22. // bump up the version each time we make incompatible change to the metrics.
  23. const currentAPICallMetricsVersion = "v1"
  24. // APICallToPerfData transforms APIResponsiveness to PerfData.
  25. func APICallToPerfData(apicalls *APIResponsiveness) *perftype.PerfData {
  26. perfData := &perftype.PerfData{Version: currentAPICallMetricsVersion}
  27. for _, apicall := range apicalls.APICalls {
  28. item := perftype.DataItem{
  29. Data: map[string]float64{
  30. "Perc50": float64(apicall.Latency.Perc50) / 1000000, // us -> ms
  31. "Perc90": float64(apicall.Latency.Perc90) / 1000000,
  32. "Perc99": float64(apicall.Latency.Perc99) / 1000000,
  33. },
  34. Unit: "ms",
  35. Labels: map[string]string{
  36. "Verb": apicall.Verb,
  37. "Resource": apicall.Resource,
  38. "Subresource": apicall.Subresource,
  39. "Scope": apicall.Scope,
  40. "Count": fmt.Sprintf("%v", apicall.Count),
  41. },
  42. }
  43. perfData.DataItems = append(perfData.DataItems, item)
  44. }
  45. return perfData
  46. }
  47. func latencyToPerfData(l LatencyMetric, name string) perftype.DataItem {
  48. return perftype.DataItem{
  49. Data: map[string]float64{
  50. "Perc50": float64(l.Perc50) / 1000000, // us -> ms
  51. "Perc90": float64(l.Perc90) / 1000000,
  52. "Perc99": float64(l.Perc99) / 1000000,
  53. "Perc100": float64(l.Perc100) / 1000000,
  54. },
  55. Unit: "ms",
  56. Labels: map[string]string{
  57. "Metric": name,
  58. },
  59. }
  60. }
  61. // PodStartupLatencyToPerfData transforms PodStartupLatency to PerfData.
  62. func PodStartupLatencyToPerfData(latency *PodStartupLatency) *perftype.PerfData {
  63. perfData := &perftype.PerfData{Version: currentAPICallMetricsVersion}
  64. perfData.DataItems = append(perfData.DataItems, latencyToPerfData(latency.CreateToScheduleLatency, "create_to_schedule"))
  65. perfData.DataItems = append(perfData.DataItems, latencyToPerfData(latency.ScheduleToRunLatency, "schedule_to_run"))
  66. perfData.DataItems = append(perfData.DataItems, latencyToPerfData(latency.RunToWatchLatency, "run_to_watch"))
  67. perfData.DataItems = append(perfData.DataItems, latencyToPerfData(latency.ScheduleToWatchLatency, "schedule_to_watch"))
  68. perfData.DataItems = append(perfData.DataItems, latencyToPerfData(latency.E2ELatency, "pod_startup"))
  69. return perfData
  70. }
  71. // CurrentKubeletPerfMetricsVersion is the current kubelet performance metrics
  72. // version. This is used by mutiple perf related data structures. We should
  73. // bump up the version each time we make an incompatible change to the metrics.
  74. const CurrentKubeletPerfMetricsVersion = "v2"
  75. // ResourceUsageToPerfData transforms ResourceUsagePerNode to PerfData. Notice that this function
  76. // only cares about memory usage, because cpu usage information will be extracted from NodesCPUSummary.
  77. func ResourceUsageToPerfData(usagePerNode ResourceUsagePerNode) *perftype.PerfData {
  78. return ResourceUsageToPerfDataWithLabels(usagePerNode, nil)
  79. }
  80. // CPUUsageToPerfData transforms NodesCPUSummary to PerfData.
  81. func CPUUsageToPerfData(usagePerNode NodesCPUSummary) *perftype.PerfData {
  82. return CPUUsageToPerfDataWithLabels(usagePerNode, nil)
  83. }
  84. // PrintPerfData prints the perfdata in json format with PerfResultTag prefix.
  85. // If an error occurs, nothing will be printed.
  86. func PrintPerfData(p *perftype.PerfData) {
  87. // Notice that we must make sure the perftype.PerfResultEnd is in a new line.
  88. if str := PrettyPrintJSON(p); str != "" {
  89. e2elog.Logf("%s %s\n%s", perftype.PerfResultTag, str, perftype.PerfResultEnd)
  90. }
  91. }
  92. // ResourceUsageToPerfDataWithLabels transforms ResourceUsagePerNode to PerfData with additional labels.
  93. // Notice that this function only cares about memory usage, because cpu usage information will be extracted from NodesCPUSummary.
  94. func ResourceUsageToPerfDataWithLabels(usagePerNode ResourceUsagePerNode, labels map[string]string) *perftype.PerfData {
  95. items := []perftype.DataItem{}
  96. for node, usages := range usagePerNode {
  97. for c, usage := range usages {
  98. item := perftype.DataItem{
  99. Data: map[string]float64{
  100. "memory": float64(usage.MemoryUsageInBytes) / (1024 * 1024),
  101. "workingset": float64(usage.MemoryWorkingSetInBytes) / (1024 * 1024),
  102. "rss": float64(usage.MemoryRSSInBytes) / (1024 * 1024),
  103. },
  104. Unit: "MB",
  105. Labels: map[string]string{
  106. "node": node,
  107. "container": c,
  108. "datatype": "resource",
  109. "resource": "memory",
  110. },
  111. }
  112. items = append(items, item)
  113. }
  114. }
  115. return &perftype.PerfData{
  116. Version: CurrentKubeletPerfMetricsVersion,
  117. DataItems: items,
  118. Labels: labels,
  119. }
  120. }
  121. // CPUUsageToPerfDataWithLabels transforms NodesCPUSummary to PerfData with additional labels.
  122. func CPUUsageToPerfDataWithLabels(usagePerNode NodesCPUSummary, labels map[string]string) *perftype.PerfData {
  123. items := []perftype.DataItem{}
  124. for node, usages := range usagePerNode {
  125. for c, usage := range usages {
  126. data := map[string]float64{}
  127. for perc, value := range usage {
  128. data[fmt.Sprintf("Perc%02.0f", perc*100)] = value * 1000
  129. }
  130. item := perftype.DataItem{
  131. Data: data,
  132. Unit: "mCPU",
  133. Labels: map[string]string{
  134. "node": node,
  135. "container": c,
  136. "datatype": "resource",
  137. "resource": "cpu",
  138. },
  139. }
  140. items = append(items, item)
  141. }
  142. }
  143. return &perftype.PerfData{
  144. Version: CurrentKubeletPerfMetricsVersion,
  145. DataItems: items,
  146. Labels: labels,
  147. }
  148. }