-
Notifications
You must be signed in to change notification settings - Fork 1
/
iperf3.go
247 lines (221 loc) · 6.72 KB
/
iperf3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Steadybit GmbH
package e2e
import (
"encoding/json"
"errors"
"fmt"
"github.com/rs/zerolog/log"
"github.com/steadybit/action-kit/go/action_kit_api/v2"
"github.com/steadybit/extension-kit/extutil"
"github.com/yalp/jsonpath"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
acorev1 "k8s.io/client-go/applyconfigurations/core/v1"
ametav1 "k8s.io/client-go/applyconfigurations/meta/v1"
"sync"
"testing"
"time"
)
type Iperf struct {
Minikube *Minikube
ServerPod metav1.Object
ClientPod metav1.Object
ServerIp string
ImageOnce sync.Once
ImageErr error
}
const (
iperf3Dockerfile = "https://raw.githubusercontent.com/nerdalert/iperf3/master/Dockerfile"
iperf3Image = "networkstatic/iperf3:local"
)
func (n *Iperf) Deploy(name string, opts ...func(server *acorev1.PodApplyConfiguration, client *acorev1.PodApplyConfiguration)) error {
if err := n.buildImages(); err != nil {
return err
}
serverPodName := fmt.Sprintf("%s-server", name)
serverCfg := &acorev1.PodApplyConfiguration{
TypeMetaApplyConfiguration: ametav1.TypeMetaApplyConfiguration{
Kind: extutil.Ptr("Pod"),
APIVersion: extutil.Ptr("v1"),
},
ObjectMetaApplyConfiguration: &ametav1.ObjectMetaApplyConfiguration{
Name: &serverPodName,
Labels: map[string]string{"app": serverPodName},
},
Spec: &acorev1.PodSpecApplyConfiguration{
RestartPolicy: extutil.Ptr(corev1.RestartPolicyNever),
Containers: []acorev1.ContainerApplyConfiguration{
{
Name: extutil.Ptr("iperf"),
Image: extutil.Ptr(iperf3Image),
Args: []string{"-s", "-p", "5201"},
Ports: []acorev1.ContainerPortApplyConfiguration{
{
Name: extutil.Ptr("control"),
ContainerPort: extutil.Ptr(int32(5201)),
},
{
Name: extutil.Ptr("data-tcp"),
ContainerPort: extutil.Ptr(int32(5000)),
Protocol: extutil.Ptr(corev1.ProtocolTCP),
}, {
Name: extutil.Ptr("data-udp"),
ContainerPort: extutil.Ptr(int32(5001)),
Protocol: extutil.Ptr(corev1.ProtocolUDP),
},
},
},
},
},
Status: nil,
}
clientPodName := fmt.Sprintf("%s-client", name)
clientCfg := &acorev1.PodApplyConfiguration{
TypeMetaApplyConfiguration: ametav1.TypeMetaApplyConfiguration{
Kind: extutil.Ptr("Pod"),
APIVersion: extutil.Ptr("v1"),
},
ObjectMetaApplyConfiguration: &ametav1.ObjectMetaApplyConfiguration{
Name: &clientPodName,
Labels: map[string]string{"app": clientPodName},
},
Spec: &acorev1.PodSpecApplyConfiguration{
RestartPolicy: extutil.Ptr(corev1.RestartPolicyNever),
Containers: []acorev1.ContainerApplyConfiguration{
{
Name: extutil.Ptr("iperf"),
Image: extutil.Ptr(iperf3Image),
Command: []string{"sleep", "infinity"},
},
},
},
Status: nil,
}
for _, fn := range opts {
fn(serverCfg, clientCfg)
}
serverPod, err := n.Minikube.CreatePod(serverCfg)
if err != nil {
return err
}
describe, err := n.Minikube.GetPod(serverPod)
if err != nil {
return err
}
n.ServerIp = describe.Status.PodIP
n.ServerPod = serverPod
clientPod, err := n.Minikube.CreatePod(clientCfg)
if err != nil {
return err
}
n.ClientPod = clientPod
return nil
}
func (n *Iperf) buildImages() error {
n.ImageOnce.Do(func() {
eg := errgroup.Group{}
eg.Go(func() error {
return n.Minikube.BuildImage(iperf3Dockerfile, iperf3Image)
})
n.ImageErr = eg.Wait()
})
return n.ImageErr
}
func (n *Iperf) Target() (*action_kit_api.Target, error) {
return NewContainerTarget(n.Minikube, n.ServerPod, "iperf")
}
func (n *Iperf) Delete() error {
return errors.Join(
n.Minikube.DeletePod(n.ServerPod),
n.Minikube.DeletePod(n.ClientPod),
)
}
func (n *Iperf) MeasurePackageLoss() (float64, error) {
out, err := n.Minikube.PodExec(n.ClientPod, "iperf", "iperf3", "--client", n.ServerIp, "--port=5201", "--udp", "--time=2", "--length=1k", "--bind=0.0.0.0", "--reverse", "--cport=5001", "--no-delay", "--zerocopy", "--json")
if err != nil {
return 0, fmt.Errorf("%w: %s", err, out)
}
var result interface{}
err = json.Unmarshal([]byte(out), &result)
if err != nil {
return 0, fmt.Errorf("failed reading results: %w", err)
}
lost, err := jsonpath.Read(result, "$.end.sum.lost_percent")
if err != nil {
return 0, fmt.Errorf("failed reading lost_percent: %w", err)
}
return lost.(float64), nil
}
func (n *Iperf) AssertPackageLoss(t *testing.T, min float64, max float64) {
t.Helper()
measurements := make([]float64, 0, 5)
Retry(t, 8, 500*time.Millisecond, func(r *R) {
loss, err := n.MeasurePackageLoss()
if err != nil {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "failed to measure package loss: %s", err)
}
if loss < min || loss > max {
r.Failed = true
measurements = append(measurements, loss)
_, _ = fmt.Fprintf(r.Log, "package loss %v is not in expected range [%f, %f]", measurements, min, max)
}
})
}
func (n *Iperf) AssertPackageLossWithRetry(min float64, max float64, maxRetries int) bool {
measurements := make([]float64, 0, 5)
success := false
for i := 0; i < maxRetries; i++ {
loss, err := n.MeasurePackageLoss()
if err != nil {
success = false
log.Err(err).Msg("failed to measure package loss")
break
}
if loss < min || loss > max {
success = false
measurements = append(measurements, loss)
} else {
success = true
break
}
}
if !success {
log.Info().Msgf("package loss %v is not in expected range [%f, %f]", measurements, min, max)
}
return success
}
func (n *Iperf) MeasureBandwidth() (float64, error) {
out, err := n.Minikube.PodExec(n.ClientPod, "iperf", "iperf3", "--client", n.ServerIp, "--port=5201", "--udp", "--time=3", "--bind=0.0.0.0", "--reverse", "--cport=5001", "--bitrate=500M", "--no-delay", "--json")
if err != nil {
return 0, fmt.Errorf("%w: %s", err, out)
}
var result interface{}
err = json.Unmarshal([]byte(out), &result)
if err != nil {
return 0, fmt.Errorf("failed reading results: %w", err)
}
bps, err := jsonpath.Read(result, "$.end.sum.bits_per_second")
if err != nil {
return 0, fmt.Errorf("failed reading bits_per_second: %w", err)
}
return bps.(float64) / 1_000_000, nil
}
func (n *Iperf) AssertBandwidth(t *testing.T, min float64, max float64) {
t.Helper()
measurements := make([]float64, 0, 5)
Retry(t, 8, 500*time.Millisecond, func(r *R) {
bandwidth, err := n.MeasureBandwidth()
if err != nil {
r.Failed = true
_, _ = fmt.Fprintf(r.Log, "failed to measure bandwidth bandwidth: %s", err)
}
if bandwidth < min || bandwidth > max {
r.Failed = true
measurements = append(measurements, bandwidth)
_, _ = fmt.Fprintf(r.Log, "bandwidth %f is not in expected range [%f, %f]", measurements, min, max)
}
})
}