...
1
16
17 package network
18
19
20 import (
21 "bytes"
22 "encoding/json"
23 "fmt"
24 "math"
25 "strconv"
26 "strings"
27
28 "k8s.io/kubernetes/test/e2e/framework"
29 )
30
31 const (
32 megabyte = 1024 * 1024
33 )
34
35
36 type IPerfResults struct {
37 BandwidthMap map[string]int64
38 }
39
40
41
42 type IPerfCSVResult struct {
43 date string
44 cli string
45 cliPort int64
46 server string
47 servPort int64
48 id string
49 interval string
50 transferBits int64
51 bandwidthBits int64
52 }
53
54 func (i *IPerfCSVResult) bandwidthMB() int64 {
55 return int64(math.Round(float64(i.bandwidthBits) / float64(megabyte) / 8))
56 }
57
58
59 func (i *IPerfResults) Add(ipr *IPerfCSVResult) {
60 if i.BandwidthMap == nil {
61 i.BandwidthMap = map[string]int64{}
62 }
63 i.BandwidthMap[ipr.cli] = ipr.bandwidthBits
64 }
65
66
67 func (i *IPerfResults) ToTSV() string {
68 if len(i.BandwidthMap) < 1 {
69 framework.Logf("Warning: no data in bandwidth map")
70 }
71
72 var buffer bytes.Buffer
73 for node, bandwidth := range i.BandwidthMap {
74 asJSON, _ := json.Marshal(node)
75 buffer.WriteString("\t " + string(asJSON) + "\t " + fmt.Sprintf("%E", float64(bandwidth)))
76 }
77 return buffer.String()
78 }
79
80
81 func NewIPerf(csvLine string) (*IPerfCSVResult, error) {
82 if len(csvLine) == 0 {
83 return nil, fmt.Errorf("No iperf output received in csv line")
84 }
85 csvLine = strings.Trim(csvLine, "\n")
86 slice := StrSlice(strings.Split(csvLine, ","))
87 if len(slice) != 9 {
88 return nil, fmt.Errorf("Incorrect fields in the output: %v (%v out of 9)", slice, len(slice))
89 }
90 i := IPerfCSVResult{}
91 i.date = slice.get(0)
92 i.cli = slice.get(1)
93 i.cliPort = intOrFail("client port", slice.get(2))
94 i.server = slice.get(3)
95 i.servPort = intOrFail("server port", slice.get(4))
96 i.id = slice.get(5)
97 i.interval = slice.get(6)
98 i.transferBits = intOrFail("transfer port", slice.get(7))
99 i.bandwidthBits = intOrFail("bandwidth port", slice.get(8))
100 return &i, nil
101 }
102
103
104 type StrSlice []string
105
106 func (s StrSlice) get(i int) string {
107 if i >= 0 && i < len(s) {
108 return s[i]
109 }
110 return ""
111 }
112
113
114 func intOrFail(debugName string, rawValue string) int64 {
115 value, err := strconv.ParseInt(rawValue, 10, 64)
116 if err != nil {
117 framework.Failf("Failed parsing value %v from the string '%v' as an integer", debugName, rawValue)
118 }
119 return value
120 }
121
122
123 type IPerf2EnhancedCSVResults struct {
124 Intervals []*IPerfCSVResult
125 Total *IPerfCSVResult
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 func ParseIPerf2EnhancedResultsFromCSV(output string) (*IPerf2EnhancedCSVResults, error) {
142 var parsedResults []*IPerfCSVResult
143 for _, line := range strings.Split(output, "\n") {
144 parsed, err := NewIPerf(line)
145 if err != nil {
146 return nil, err
147 }
148 parsedResults = append(parsedResults, parsed)
149 }
150 if parsedResults == nil || len(parsedResults) == 0 {
151 return nil, fmt.Errorf("no results parsed from iperf2 output")
152 }
153
154
155 intervals := parsedResults[:len(parsedResults)-1]
156
157 total := parsedResults[len(parsedResults)-1]
158 return &IPerf2EnhancedCSVResults{
159 Intervals: intervals,
160 Total: total,
161 }, nil
162 }
163
164
165
166
167 type IPerf2NodeToNodeCSVResults struct {
168 ServerNode string
169 Results map[string]*IPerf2EnhancedCSVResults
170 }
171
View as plain text