util_iperf.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2015 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 network
  14. // Tests network performance using iperf or other containers.
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "strconv"
  20. "strings"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. )
  23. // IPerfResults is a struct that stores some IPerfResult
  24. type IPerfResults struct {
  25. BandwidthMap map[string]int64
  26. }
  27. // IPerfResult struct modelling an iperf record....
  28. // 20160314154239,172.17.0.3,34152,172.17.0.2,5001,3,0.0-10.0,33843707904,27074774092
  29. type IPerfResult struct {
  30. date string // field 1 in the csv
  31. cli string // field 2 in the csv
  32. cliPort int64 // ...
  33. server string
  34. servPort int64
  35. id string
  36. interval string
  37. transferBits int64
  38. bandwidthBits int64
  39. }
  40. // Add adds a new result to the Results struct.
  41. func (i *IPerfResults) Add(ipr *IPerfResult) {
  42. if i.BandwidthMap == nil {
  43. i.BandwidthMap = map[string]int64{}
  44. }
  45. i.BandwidthMap[ipr.cli] = ipr.bandwidthBits
  46. }
  47. // ToTSV exports an easily readable tab delimited format of all IPerfResults.
  48. func (i *IPerfResults) ToTSV() string {
  49. if len(i.BandwidthMap) < 1 {
  50. framework.Logf("Warning: no data in bandwidth map")
  51. }
  52. var buffer bytes.Buffer
  53. for node, bandwidth := range i.BandwidthMap {
  54. asJSON, _ := json.Marshal(node)
  55. buffer.WriteString("\t " + string(asJSON) + "\t " + fmt.Sprintf("%E", float64(bandwidth)))
  56. }
  57. return buffer.String()
  58. }
  59. // NewIPerf parses an IPerf CSV output line into an IPerfResult.
  60. func NewIPerf(csvLine string) *IPerfResult {
  61. csvLine = strings.Trim(csvLine, "\n")
  62. slice := StrSlice(strings.Split(csvLine, ","))
  63. if len(slice) != 9 {
  64. framework.Failf("Incorrect fields in the output: %v (%v out of 9)", slice, len(slice))
  65. }
  66. i := IPerfResult{}
  67. i.date = slice.get(0)
  68. i.cli = slice.get(1)
  69. i.cliPort = intOrFail("client port", slice.get(2))
  70. i.server = slice.get(3)
  71. i.servPort = intOrFail("server port", slice.get(4))
  72. i.id = slice.get(5)
  73. i.interval = slice.get(6)
  74. i.transferBits = intOrFail("transfer port", slice.get(7))
  75. i.bandwidthBits = intOrFail("bandwidth port", slice.get(8))
  76. return &i
  77. }
  78. // StrSlice represents a string slice
  79. type StrSlice []string
  80. func (s StrSlice) get(i int) string {
  81. if i >= 0 && i < len(s) {
  82. return s[i]
  83. }
  84. return ""
  85. }
  86. // intOrFail is a convenience function for parsing integers.
  87. func intOrFail(debugName string, rawValue string) int64 {
  88. value, err := strconv.ParseInt(rawValue, 10, 64)
  89. if err != nil {
  90. framework.Failf("Failed parsing value %v from the string '%v' as an integer", debugName, rawValue)
  91. }
  92. return value
  93. }