util_iperf.go 3.1 KB

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