Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-daemon/pkg/cpulimit/bandwidth_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cpulimit_test
6
7
import (
8
"testing"
9
"time"
10
11
"github.com/gitpod-io/gitpod/ws-daemon/pkg/cpulimit"
12
"github.com/google/go-cmp/cmp"
13
"k8s.io/apimachinery/pkg/api/resource"
14
)
15
16
func TestBandwidthFromQuotaAndPeriod(t *testing.T) {
17
tests := []struct {
18
Name string
19
Period time.Duration
20
Quota time.Duration
21
Expectation cpulimit.Bandwidth
22
}{
23
{
24
Name: "0",
25
Period: 0,
26
Quota: 0,
27
Expectation: 0,
28
},
29
{
30
Name: "mag10 period",
31
Period: 100 * time.Millisecond,
32
Quota: 500 * time.Millisecond,
33
Expectation: 5000,
34
},
35
{
36
Name: "non mag10 period",
37
Period: 250 * time.Millisecond,
38
Quota: 1250 * time.Millisecond,
39
Expectation: 5000,
40
},
41
}
42
43
for _, test := range tests {
44
t.Run(test.Name, func(t *testing.T) {
45
act := cpulimit.BandwidthFromQuotaAndPeriod(test.Quota, test.Period)
46
if diff := cmp.Diff(test.Expectation, act); diff != "" {
47
t.Errorf("unexpected result (-want +got):\n%s", diff)
48
}
49
})
50
}
51
}
52
53
func TestBandwidthFromQuantity(t *testing.T) {
54
tests := []struct {
55
Name string
56
Quantity resource.Quantity
57
Expectation cpulimit.Bandwidth
58
}{
59
{Name: "0", Quantity: resource.MustParse("0"), Expectation: 0},
60
{Name: "full CPUs", Quantity: resource.MustParse("5"), Expectation: 5000},
61
{Name: "milli CPUs", Quantity: resource.MustParse("500m"), Expectation: 500},
62
}
63
64
for _, test := range tests {
65
t.Run(test.Name, func(t *testing.T) {
66
act := cpulimit.BandwidthFromQuantity(test.Quantity)
67
if diff := cmp.Diff(test.Expectation, act); diff != "" {
68
t.Errorf("unexpected result (-want +got):\n%s", diff)
69
}
70
})
71
}
72
}
73
74
func TestQuota(t *testing.T) {
75
tests := []struct {
76
Name string
77
Bandwidth cpulimit.Bandwidth
78
Period time.Duration
79
Expectation time.Duration
80
}{
81
{Name: "0", Bandwidth: 0, Period: 100 * time.Millisecond, Expectation: 0},
82
{Name: "0 period", Bandwidth: 0, Period: 0 * time.Millisecond, Expectation: 0},
83
{Name: "1 to 1ms", Bandwidth: 1, Period: 1 * time.Millisecond, Expectation: 1 * time.Microsecond},
84
{Name: "1 to 100ms", Bandwidth: 1, Period: 100 * time.Millisecond, Expectation: 100 * time.Microsecond},
85
{Name: "mag10", Bandwidth: 6000, Period: 100 * time.Millisecond, Expectation: 600 * time.Millisecond},
86
{Name: "not mag10", Bandwidth: 6000, Period: 250 * time.Millisecond, Expectation: 1500 * time.Millisecond},
87
}
88
89
for _, test := range tests {
90
t.Run(test.Name, func(t *testing.T) {
91
act := test.Bandwidth.Quota(test.Period)
92
if diff := cmp.Diff(test.Expectation, act); diff != "" {
93
t.Errorf("unexpected result (-want +got):\n%s", diff)
94
}
95
})
96
}
97
}
98
99
func TestIntegrate(t *testing.T) {
100
tests := []struct {
101
Name string
102
Bandwidth cpulimit.Bandwidth
103
DT time.Duration
104
Expectation cpulimit.CPUTime
105
}{
106
{Name: "0", Bandwidth: 0, DT: 1 * time.Second, Expectation: 0},
107
{Name: "1 CPU second", Bandwidth: 1000, DT: 1 * time.Second, Expectation: cpulimit.CPUTime(1 * time.Second)},
108
{Name: "1 CPU minute", Bandwidth: 6000, DT: 10 * time.Second, Expectation: cpulimit.CPUTime(1 * time.Minute)},
109
}
110
111
for _, test := range tests {
112
t.Run(test.Name, func(t *testing.T) {
113
act := test.Bandwidth.Integrate(test.DT)
114
if diff := cmp.Diff(test.Expectation, act); diff != "" {
115
t.Errorf("unexpected result (-want +got):\n%s", diff)
116
}
117
})
118
}
119
}
120
121