这是indexloc提供的服务,不要输入任何密码
Skip to content

Allow concurrency to be set with percentage values #873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions cli/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"text/tabwriter"
Expand Down Expand Up @@ -562,14 +561,11 @@ func parseRunArgs(args []string, output cli.Ui) (*RunOptions, error) {
output.Warn("[WARNING] The --serial flag has been deprecated and will be removed in future versions of turbo. Please use `--concurrency=1` instead")
runOptions.concurrency = 1
case strings.HasPrefix(arg, "--concurrency"):
if i, err := strconv.Atoi(arg[len("--concurrency="):]); err != nil {
return nil, fmt.Errorf("invalid value for --concurrency CLI flag. This should be a positive integer greater than or equal to 1: %w", err)
concurrencyRaw := arg[len("--concurrency="):]
if concurrency, err := util.ParseConcurrency(concurrencyRaw); err != nil {
return nil, err
} else {
if i >= 1 {
runOptions.concurrency = i
} else {
return nil, fmt.Errorf("invalid value %v for --concurrency CLI flag. This should be a positive integer greater than or equal to 1", i)
}
runOptions.concurrency = concurrency
}
case strings.HasPrefix(arg, "--includeDependencies"):
output.Warn("[WARNING] The --includeDependencies flag has renamed to --include-dependencies for consistency. Please use `--include-dependencies` instead")
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestParseConfig(t *testing.T) {
if err != nil {
t.Fatalf("invalid parse: %#v", err)
}
assert.EqualValues(t, actual, tc.Expected)
assert.EqualValues(t, tc.Expected, actual)
})
}
}
Expand Down
36 changes: 36 additions & 0 deletions cli/internal/util/parse_concurrency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package util

import (
"fmt"
"math"
"runtime"
"strconv"
"strings"
)

var (
// alias so we can mock in tests
runtimeNumCPU = runtime.NumCPU
)

func ParseConcurrency(concurrencyRaw string) (int, error) {
if strings.HasSuffix(concurrencyRaw, "%") {
if percent, err := strconv.ParseFloat(concurrencyRaw[:len(concurrencyRaw)-1], 64); err != nil {
return 0, fmt.Errorf("invalid value for --concurrency CLI flag. This should be a number --concurrency=4 or percentage of CPU cores --concurrency=50%% : %w", err)
} else {
if percent > 0 {
return int(math.Max(1, float64(runtimeNumCPU())*percent/100)), nil
} else {
return 0, fmt.Errorf("invalid percentage value for --concurrency CLI flag. This should be a percentage of CPU cores, betteen 1%% and 100%% : %w", err)
}
}
} else if i, err := strconv.Atoi(concurrencyRaw); err != nil {
return 0, fmt.Errorf("invalid value for --concurrency CLI flag. This should be a positive integer greater than or equal to 1: %w", err)
} else {
if i >= 1 {
return i, nil
} else {
return 0, fmt.Errorf("invalid value %v for --concurrency CLI flag. This should be a positive integer greater than or equal to 1", i)
}
}
}
69 changes: 69 additions & 0 deletions cli/internal/util/parse_concurrency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package util

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseConcurrency(t *testing.T) {
cases := []struct {
Input string
Expected int
}{
{
"12",
12,
},
{
"200%",
20,
},
{
"100%",
10,
},
{
"50%",
5,
},
{
"25%",
2,
},
{
"1%",
1,
},
}

// mock runtime.NumCPU() to 10
runtimeNumCPU = func() int {
return 10
}

for i, tc := range cases {
t.Run(fmt.Sprintf("%d) '%s' should be parsed at '%d'", i, tc.Input, tc.Expected), func(t *testing.T) {
if result, err := ParseConcurrency(tc.Input); err != nil {
t.Fatalf("invalid parse: %#v", err)
} else {
assert.EqualValues(t, tc.Expected, result)
}
})
}

t.Run("throw on invalid string input", func(t *testing.T) {
_, err := ParseConcurrency("asdf")
assert.Error(t, err)
})

t.Run("throw on invalid number input", func(t *testing.T) {
_, err := ParseConcurrency("-1")
assert.Error(t, err)
})

t.Run("throw on invalid percent input - negative", func(t *testing.T) {
_, err := ParseConcurrency("-1%")
assert.Error(t, err)
})
}