| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package bench |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"flag" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"golang.org/x/tools/gopls/internal/protocol" |
| 14 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 15 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 16 |
"golang.org/x/tools/gopls/internal/test/integration/fake" |
| 17 |
) |
| 18 |
|
| 19 |
var gopath = flag.String("gopath", "", "if set, run goimports scan with this GOPATH value") |
| 20 |
|
| 21 |
func BenchmarkInitialGoimportsScan(b *testing.B) { |
| 22 |
if *gopath == "" { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
b.Skip("imports scan requires an explicit GOPATH to be set with -gopath") |
| 28 |
} |
| 29 |
|
| 30 |
repo := getRepo(b, "tools") |
| 31 |
|
| 32 |
for b.Loop() { |
| 33 |
func() { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
b.StopTimer() |
| 42 |
config := fake.EditorConfig{ |
| 43 |
Env: map[string]string{"GOPATH": *gopath}, |
| 44 |
} |
| 45 |
env := repo.newEnv(b, config, "imports", false) |
| 46 |
defer env.Close() |
| 47 |
env.Await(InitialWorkspaceLoad) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
env.CreateBuffer("internal/lsp/cache/temp.go", ` |
| 52 |
// This is a temp file to exercise goimports scan of the module cache. |
| 53 |
package cache |
| 54 |
|
| 55 |
func _() { |
| 56 |
_ = a.B // a dangling selector causes goimports to scan many packages |
| 57 |
} |
| 58 |
`) |
| 59 |
env.AfterChange() |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 64 |
Command: command.ScanImports.String(), |
| 65 |
}, nil) |
| 66 |
|
| 67 |
if stopAndRecord := startProfileIfSupported(b, env, "importsscan"); stopAndRecord != nil { |
| 68 |
defer stopAndRecord() |
| 69 |
} |
| 70 |
|
| 71 |
b.StartTimer() |
| 72 |
if false { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ctx, cancel := context.WithTimeout(env.Ctx, 50*time.Millisecond) |
| 79 |
defer cancel() |
| 80 |
if err := env.Editor.OrganizeImports(ctx, "internal/lsp/cache/temp.go"); err != nil { |
| 81 |
b.Logf("organize imports failed: %v", err) |
| 82 |
} |
| 83 |
} |
| 84 |
env.OrganizeImports("internal/lsp/cache/temp.go") |
| 85 |
}() |
| 86 |
} |
| 87 |
} |