-
Notifications
You must be signed in to change notification settings - Fork 648
Add configurable concurrency option #1176
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
Open
jakebailey
wants to merge
10
commits into
main
Choose a base branch
from
jabaile/concurrency-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24e52b4
Add configurable concurency options
jakebailey ca9c055
Fix nil
jakebailey 4675e42
Test program stuff
jakebailey 1448c31
Remove print
jakebailey d341e4e
CI
jakebailey ff61276
Don't make more checkers than files
jakebailey 99fc50c
Testing
jakebailey 9a0905a
lint
jakebailey 5479879
Merge branch 'main' into jabaile/concurrency-options
jakebailey e4740ca
Fix macos
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package core | ||
|
||
import ( | ||
"os" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/microsoft/typescript-go/internal/testutil/race" | ||
) | ||
|
||
type Concurrency struct { | ||
checkerCount int | ||
} | ||
|
||
func ParseConcurrency(options *CompilerOptions) Concurrency { | ||
if options.SingleThreaded.IsTrue() { | ||
return Concurrency{ | ||
checkerCount: 1, | ||
} | ||
} | ||
return parseConcurrency(options.Concurrency) | ||
} | ||
|
||
func parseConcurrency(s string) Concurrency { | ||
checkerCount := 4 | ||
|
||
switch strings.ToLower(s) { | ||
case "default", "auto", "true", "yes", "on": | ||
break | ||
case "single", "none", "false", "no", "off": | ||
checkerCount = 1 | ||
case "max": | ||
checkerCount = runtime.GOMAXPROCS(0) | ||
case "half": | ||
checkerCount = max(1, runtime.GOMAXPROCS(0)/2) | ||
case "checker-per-file": | ||
checkerCount = -1 | ||
default: | ||
if s != "" { | ||
if v, err := strconv.Atoi(s); err == nil && v > 0 { | ||
checkerCount = v | ||
} | ||
} | ||
} | ||
|
||
return Concurrency{ | ||
checkerCount: checkerCount, | ||
} | ||
} | ||
|
||
func (c Concurrency) SingleThreaded() bool { | ||
return c.checkerCount == 1 | ||
} | ||
|
||
func (c Concurrency) CheckerCount(numFiles int) int { | ||
checkerCount := c.checkerCount | ||
if c.checkerCount == -1 { | ||
return max(1, numFiles) | ||
} | ||
return min(max(1, checkerCount), numFiles) | ||
} | ||
|
||
var testProgramConcurrency = sync.OnceValues(func() (concurrency Concurrency, raw string) { | ||
// Leave Program in SingleThreaded mode unless explicitly configured or in race mode. | ||
v := os.Getenv("TSGO_TEST_PROGRAM_CONCURRENCY") | ||
if v == "" && !race.Enabled { | ||
v = "single" | ||
} | ||
return parseConcurrency(v), v | ||
}) | ||
|
||
func TestProgramConcurrency() (concurrency Concurrency, raw string) { | ||
return testProgramConcurrency() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package core | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestConcurrency(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
opts *CompilerOptions | ||
numFiles int | ||
singleThreaded bool | ||
checkerCount int | ||
}{ | ||
{"defaults", &CompilerOptions{}, 100, false, 4}, | ||
{"default", &CompilerOptions{Concurrency: "default"}, 100, false, 4}, | ||
{"auto", &CompilerOptions{Concurrency: "true"}, 100, false, 4}, | ||
{"true", &CompilerOptions{Concurrency: "true"}, 100, false, 4}, | ||
{"yes", &CompilerOptions{Concurrency: "yes"}, 100, false, 4}, | ||
{"on", &CompilerOptions{Concurrency: "on"}, 100, false, 4}, | ||
{"singleThreaded", &CompilerOptions{SingleThreaded: TSTrue}, 100, true, 1}, | ||
{"single", &CompilerOptions{Concurrency: "single"}, 100, true, 1}, | ||
{"none", &CompilerOptions{Concurrency: "none"}, 100, true, 1}, | ||
{"false", &CompilerOptions{Concurrency: "false"}, 100, true, 1}, | ||
{"no", &CompilerOptions{Concurrency: "no"}, 100, true, 1}, | ||
{"off", &CompilerOptions{Concurrency: "off"}, 100, true, 1}, | ||
{"max", &CompilerOptions{Concurrency: "max"}, 1000, false, runtime.GOMAXPROCS(0)}, | ||
{"half", &CompilerOptions{Concurrency: "half"}, 1000, runtime.GOMAXPROCS(0)/2 == 1, runtime.GOMAXPROCS(0) / 2}, | ||
{"checker-per-file", &CompilerOptions{Concurrency: "checker-per-file"}, 100, false, 100}, | ||
{"more than files", &CompilerOptions{Concurrency: "1000"}, 100, false, 100}, | ||
{"10", &CompilerOptions{Concurrency: "10"}, 100, false, 10}, | ||
{"1", &CompilerOptions{Concurrency: "1"}, 100, true, 1}, | ||
{"invalid", &CompilerOptions{Concurrency: "i dunno"}, 100, false, 4}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
c := ParseConcurrency(tt.opts) | ||
singleThreaded := c.SingleThreaded() | ||
checkerCount := c.CheckerCount(tt.numFiles) | ||
assert.Equal(t, singleThreaded, tt.singleThreaded) | ||
assert.Equal(t, checkerCount, tt.checkerCount) | ||
}) | ||
} | ||
|
||
t.Run("TestProgramConcurrency", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
c, _ := TestProgramConcurrency() | ||
assert.Assert(t, c.CheckerCount(10000) > 0) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.