Path: blob/main/components/ws-daemon/pkg/cpulimit/bandwidth_test.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cpulimit_test56import (7"testing"8"time"910"github.com/gitpod-io/gitpod/ws-daemon/pkg/cpulimit"11"github.com/google/go-cmp/cmp"12"k8s.io/apimachinery/pkg/api/resource"13)1415func TestBandwidthFromQuotaAndPeriod(t *testing.T) {16tests := []struct {17Name string18Period time.Duration19Quota time.Duration20Expectation cpulimit.Bandwidth21}{22{23Name: "0",24Period: 0,25Quota: 0,26Expectation: 0,27},28{29Name: "mag10 period",30Period: 100 * time.Millisecond,31Quota: 500 * time.Millisecond,32Expectation: 5000,33},34{35Name: "non mag10 period",36Period: 250 * time.Millisecond,37Quota: 1250 * time.Millisecond,38Expectation: 5000,39},40}4142for _, test := range tests {43t.Run(test.Name, func(t *testing.T) {44act := cpulimit.BandwidthFromQuotaAndPeriod(test.Quota, test.Period)45if diff := cmp.Diff(test.Expectation, act); diff != "" {46t.Errorf("unexpected result (-want +got):\n%s", diff)47}48})49}50}5152func TestBandwidthFromQuantity(t *testing.T) {53tests := []struct {54Name string55Quantity resource.Quantity56Expectation cpulimit.Bandwidth57}{58{Name: "0", Quantity: resource.MustParse("0"), Expectation: 0},59{Name: "full CPUs", Quantity: resource.MustParse("5"), Expectation: 5000},60{Name: "milli CPUs", Quantity: resource.MustParse("500m"), Expectation: 500},61}6263for _, test := range tests {64t.Run(test.Name, func(t *testing.T) {65act := cpulimit.BandwidthFromQuantity(test.Quantity)66if diff := cmp.Diff(test.Expectation, act); diff != "" {67t.Errorf("unexpected result (-want +got):\n%s", diff)68}69})70}71}7273func TestQuota(t *testing.T) {74tests := []struct {75Name string76Bandwidth cpulimit.Bandwidth77Period time.Duration78Expectation time.Duration79}{80{Name: "0", Bandwidth: 0, Period: 100 * time.Millisecond, Expectation: 0},81{Name: "0 period", Bandwidth: 0, Period: 0 * time.Millisecond, Expectation: 0},82{Name: "1 to 1ms", Bandwidth: 1, Period: 1 * time.Millisecond, Expectation: 1 * time.Microsecond},83{Name: "1 to 100ms", Bandwidth: 1, Period: 100 * time.Millisecond, Expectation: 100 * time.Microsecond},84{Name: "mag10", Bandwidth: 6000, Period: 100 * time.Millisecond, Expectation: 600 * time.Millisecond},85{Name: "not mag10", Bandwidth: 6000, Period: 250 * time.Millisecond, Expectation: 1500 * time.Millisecond},86}8788for _, test := range tests {89t.Run(test.Name, func(t *testing.T) {90act := test.Bandwidth.Quota(test.Period)91if diff := cmp.Diff(test.Expectation, act); diff != "" {92t.Errorf("unexpected result (-want +got):\n%s", diff)93}94})95}96}9798func TestIntegrate(t *testing.T) {99tests := []struct {100Name string101Bandwidth cpulimit.Bandwidth102DT time.Duration103Expectation cpulimit.CPUTime104}{105{Name: "0", Bandwidth: 0, DT: 1 * time.Second, Expectation: 0},106{Name: "1 CPU second", Bandwidth: 1000, DT: 1 * time.Second, Expectation: cpulimit.CPUTime(1 * time.Second)},107{Name: "1 CPU minute", Bandwidth: 6000, DT: 10 * time.Second, Expectation: cpulimit.CPUTime(1 * time.Minute)},108}109110for _, test := range tests {111t.Run(test.Name, func(t *testing.T) {112act := test.Bandwidth.Integrate(test.DT)113if diff := cmp.Diff(test.Expectation, act); diff != "" {114t.Errorf("unexpected result (-want +got):\n%s", diff)115}116})117}118}119120121