perf_util.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. "k8s.io/kubernetes/test/e2e/perftype"
  17. )
  18. // TODO(random-liu): Change the tests to actually use PerfData from the beginning instead of
  19. // translating one to the other here.
  20. // currentApiCallMetricsVersion is the current apicall performance metrics version. We should
  21. // bump up the version each time we make incompatible change to the metrics.
  22. const currentApiCallMetricsVersion = "v1"
  23. // ApiCallToPerfData transforms APIResponsiveness to PerfData.
  24. func ApiCallToPerfData(apicalls APIResponsiveness) *perftype.PerfData {
  25. perfData := &perftype.PerfData{Version: currentApiCallMetricsVersion}
  26. for _, apicall := range apicalls.APICalls {
  27. item := perftype.DataItem{
  28. Data: map[string]float64{
  29. "Perc50": float64(apicall.Latency.Perc50) / 1000000, // us -> ms
  30. "Perc90": float64(apicall.Latency.Perc90) / 1000000,
  31. "Perc99": float64(apicall.Latency.Perc99) / 1000000,
  32. },
  33. Unit: "ms",
  34. Labels: map[string]string{
  35. "Verb": apicall.Verb,
  36. "Resource": apicall.Resource,
  37. },
  38. }
  39. perfData.DataItems = append(perfData.DataItems, item)
  40. }
  41. return perfData
  42. }
  43. // currentKubeletPerfMetricsVersion is the current kubelet performance metrics version. We should
  44. // bump up the version each time we make incompatible change to the metrics.
  45. const currentKubeletPerfMetricsVersion = "v1"
  46. // ResourceUsageToPerfData transforms ResourceUsagePerNode to PerfData. Notice that this function
  47. // only cares about memory usage, because cpu usage information will be extracted from NodesCPUSummary.
  48. func ResourceUsageToPerfData(usagePerNode ResourceUsagePerNode) *perftype.PerfData {
  49. return ResourceUsageToPerfDataWithLabels(usagePerNode, nil)
  50. }
  51. // CPUUsageToPerfData transforms NodesCPUSummary to PerfData.
  52. func CPUUsageToPerfData(usagePerNode NodesCPUSummary) *perftype.PerfData {
  53. return CPUUsageToPerfDataWithLabels(usagePerNode, nil)
  54. }
  55. // PrintPerfData prints the perfdata in json format with PerfResultTag prefix.
  56. // If an error occurs, nothing will be printed.
  57. func PrintPerfData(p *perftype.PerfData) {
  58. // Notice that we must make sure the perftype.PerfResultEnd is in a new line.
  59. if str := PrettyPrintJSON(p); str != "" {
  60. Logf("%s %s\n%s", perftype.PerfResultTag, str, perftype.PerfResultEnd)
  61. }
  62. }
  63. // ResourceUsageToPerfDataWithLabels transforms ResourceUsagePerNode to PerfData with additional labels.
  64. // Notice that this function only cares about memory usage, because cpu usage information will be extracted from NodesCPUSummary.
  65. func ResourceUsageToPerfDataWithLabels(usagePerNode ResourceUsagePerNode, labels map[string]string) *perftype.PerfData {
  66. items := []perftype.DataItem{}
  67. for _, usages := range usagePerNode {
  68. for c, usage := range usages {
  69. item := perftype.DataItem{
  70. Data: map[string]float64{
  71. "memory": float64(usage.MemoryUsageInBytes) / (1024 * 1024),
  72. "workingset": float64(usage.MemoryWorkingSetInBytes) / (1024 * 1024),
  73. "rss": float64(usage.MemoryRSSInBytes) / (1024 * 1024),
  74. },
  75. Unit: "MB",
  76. Labels: map[string]string{
  77. "container": c,
  78. "datatype": "resource",
  79. "resource": "memory",
  80. },
  81. }
  82. items = append(items, item)
  83. }
  84. }
  85. return &perftype.PerfData{
  86. Version: currentKubeletPerfMetricsVersion,
  87. DataItems: items,
  88. Labels: labels,
  89. }
  90. }
  91. // CPUUsageToPerfDataWithLabels transforms NodesCPUSummary to PerfData with additional labels.
  92. func CPUUsageToPerfDataWithLabels(usagePerNode NodesCPUSummary, labels map[string]string) *perftype.PerfData {
  93. items := []perftype.DataItem{}
  94. for _, usages := range usagePerNode {
  95. for c, usage := range usages {
  96. data := map[string]float64{}
  97. for perc, value := range usage {
  98. data[fmt.Sprintf("Perc%02.0f", perc*100)] = value * 1000
  99. }
  100. item := perftype.DataItem{
  101. Data: data,
  102. Unit: "mCPU",
  103. Labels: map[string]string{
  104. "container": c,
  105. "datatype": "resource",
  106. "resource": "cpu",
  107. },
  108. }
  109. items = append(items, item)
  110. }
  111. }
  112. return &perftype.PerfData{
  113. Version: currentKubeletPerfMetricsVersion,
  114. DataItems: items,
  115. Labels: labels,
  116. }
  117. }