util_iperf.go 2.9 KB

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