From 9a1597cfc5bdbceecd537e9238df0e2d6b69bf76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= Date: Wed, 28 Aug 2024 14:40:15 +0200 Subject: [PATCH 1/2] Remove custom 'min' function (prior to Go v1.21) --- output/cloud/expv2/flush.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/output/cloud/expv2/flush.go b/output/cloud/expv2/flush.go index 036e724c16c..1ce39fb12ca 100644 --- a/output/cloud/expv2/flush.go +++ b/output/cloud/expv2/flush.go @@ -86,16 +86,8 @@ func (f *metricsFlusher) flush() error { } func (f *metricsFlusher) flushBatches(batches []*pbcloud.MetricSet) error { - // TODO remove after go 1.21 becomes the minimum supported version - it has `min` in it - minInt := func(a, b int) int { - if a < b { - return a - } - return b - } - var ( - workers = minInt(len(batches), f.batchPushConcurrency) + workers = min(len(batches), f.batchPushConcurrency) errs = make(chan error, workers) feed = make(chan *pbcloud.MetricSet) finalErr error From 44c779f9654a17cdc824459c19f235ddd81d6e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= Date: Wed, 28 Aug 2024 15:39:24 +0200 Subject: [PATCH 2/2] Replace more custom min/max functions in favor of stdlib --- cmd/tests/cmd_run_test.go | 15 ++------ cmd/ui.go | 4 +-- js/modules/k6/experimental/fs/file.go | 4 +-- lib/util.go | 19 ---------- lib/util_test.go | 52 --------------------------- 5 files changed, 6 insertions(+), 88 deletions(-) delete mode 100644 lib/util.go delete mode 100644 lib/util_test.go diff --git a/cmd/tests/cmd_run_test.go b/cmd/tests/cmd_run_test.go index 985eb0ba1ae..285cfe6e751 100644 --- a/cmd/tests/cmd_run_test.go +++ b/cmd/tests/cmd_run_test.go @@ -11,6 +11,7 @@ import ( "path/filepath" "regexp" "runtime" + "slices" "strings" "sync" "sync/atomic" @@ -1434,16 +1435,6 @@ func sum(vals []float64) (sum float64) { return sum } -func maxFloat64(vals []float64) float64 { - result := vals[0] - for _, val := range vals { - if result < val { - result = val - } - } - return result -} - func TestActiveVUsCount(t *testing.T) { t.Parallel() @@ -1499,8 +1490,8 @@ func TestActiveVUsCount(t *testing.T) { jsonResults, err := fsext.ReadFile(ts.FS, "results.json") require.NoError(t, err) // t.Log(string(jsonResults)) - assert.Equal(t, float64(10), maxFloat64(getSampleValues(t, jsonResults, "vus_max", nil))) - assert.Equal(t, float64(10), maxFloat64(getSampleValues(t, jsonResults, "vus", nil))) + assert.Equal(t, float64(10), slices.Max(getSampleValues(t, jsonResults, "vus_max", nil))) + assert.Equal(t, float64(10), slices.Max(getSampleValues(t, jsonResults, "vus", nil))) assert.Equal(t, float64(0), sum(getSampleValues(t, jsonResults, "iterations", nil))) logEntries := ts.LoggerHook.Drain() diff --git a/cmd/ui.go b/cmd/ui.go index 44b60cbe256..b6c959d9e5a 100644 --- a/cmd/ui.go +++ b/cmd/ui.go @@ -280,10 +280,10 @@ func showProgress(ctx context.Context, gs *state.GlobalState, pbs []*pb.Progress var leftLen int64 for _, pb := range pbs { l := pb.Left() - leftLen = lib.Max(int64(len(l)), leftLen) + leftLen = max(int64(len(l)), leftLen) } // Limit to maximum left text length - maxLeft := int(lib.Min(leftLen, maxLeftLength)) + maxLeft := int(min(leftLen, maxLeftLength)) var progressBarsLastRenderLock sync.Mutex var progressBarsLastRender []byte diff --git a/js/modules/k6/experimental/fs/file.go b/js/modules/k6/experimental/fs/file.go index b0e25416c35..d72a3517154 100644 --- a/js/modules/k6/experimental/fs/file.go +++ b/js/modules/k6/experimental/fs/file.go @@ -4,8 +4,6 @@ import ( "io" "path/filepath" "sync/atomic" - - "go.k6.io/k6/lib" ) // file is an abstraction for interacting with files. @@ -55,7 +53,7 @@ func (f *file) Read(into []byte) (n int, err error) { // Calculate the effective new offset targetOffset := currentOffset + int64(len(into)) - newOffset := lib.Min(targetOffset, fileSize) + newOffset := min(targetOffset, fileSize) // Read the data into the provided slice, and update // the offset accordingly diff --git a/lib/util.go b/lib/util.go deleted file mode 100644 index 0c4f2aef553..00000000000 --- a/lib/util.go +++ /dev/null @@ -1,19 +0,0 @@ -package lib - -// Max returns the maximum value of a and b. -// TODO: replace after 1.21 is the minimal supported version. -func Max(a, b int64) int64 { - if a > b { - return a - } - return b -} - -// Min returns the minimum value of a and b. -// TODO: replace after 1.21 is the minimal supported version. -func Min(a, b int64) int64 { - if a < b { - return a - } - return b -} diff --git a/lib/util_test.go b/lib/util_test.go deleted file mode 100644 index 1d9def2e0c5..00000000000 --- a/lib/util_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package lib - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -//TODO: update test -/* -func TestSumStages(t *testing.T) { - testdata := map[string]struct { - Time types.NullDuration - Stages []Stage - }{ - "Blank": {types.NullDuration{}, []Stage{}}, - "Infinite": {types.NullDuration{}, []Stage{{}}}, - "Limit": { - types.NullDurationFrom(10 * time.Second), - []Stage{ - {Duration: types.NullDurationFrom(5 * time.Second)}, - {Duration: types.NullDurationFrom(5 * time.Second)}, - }, - }, - "InfiniteTail": { - types.NullDuration{Duration: types.Duration(10 * time.Second), Valid: false}, - []Stage{ - {Duration: types.NullDurationFrom(5 * time.Second)}, - {Duration: types.NullDurationFrom(5 * time.Second)}, - {}, - }, - }, - } - for name, data := range testdata { - t.Run(name, func(t *testing.T) { - assert.Equal(t, data.Time, SumStages(data.Stages)) - }) - } -} -*/ - -func TestMin(t *testing.T) { - t.Parallel() - assert.Equal(t, int64(10), Min(10, 100)) - assert.Equal(t, int64(10), Min(100, 10)) -} - -func TestMax(t *testing.T) { - t.Parallel() - assert.Equal(t, int64(100), Max(10, 100)) - assert.Equal(t, int64(100), Max(100, 10)) -}