Skip to content

Commit 1b71eda

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/regtest/bench: add flags to reuse benchmark repo dirs
It can be slow and use significant bandwidth to do repeated shallow clones of e.g. kubernetes while iterating on the benchmark. Add flags to re-use existing dirs checked out the the correct commit, assuming the user knows what they are doing. Change-Id: I2a8584ab26290a317ac23c4dec32f9c13de42d41 Reviewed-on: https://go-review.googlesource.com/c/tools/+/477978 Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent f8a7325 commit 1b71eda

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

gopls/internal/regtest/bench/repo_test.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"context"
1010
"errors"
11+
"flag"
1112
"fmt"
1213
"log"
1314
"os"
@@ -30,6 +31,7 @@ var repos = map[string]*repo{
3031
name: "istio",
3132
url: "https://github.com/istio/istio",
3233
commit: "1.17.0",
34+
inDir: flag.String("istio_dir", "", "if set, reuse this directory as [email protected]"),
3335
},
3436

3537
// Kubernetes is a large repo with many dependencies, and in the past has
@@ -38,13 +40,15 @@ var repos = map[string]*repo{
3840
name: "kubernetes",
3941
url: "https://github.com/kubernetes/kubernetes",
4042
commit: "v1.24.0",
43+
inDir: flag.String("kubernetes_dir", "", "if set, reuse this directory as [email protected]"),
4144
},
4245

4346
// A large, industrial application.
4447
"kuma": {
4548
name: "kuma",
4649
url: "https://github.com/kumahq/kuma",
4750
commit: "2.1.1",
51+
inDir: flag.String("kuma_dir", "", "if set, reuse this directory as [email protected]"),
4852
},
4953

5054
// x/pkgsite is familiar and represents a common use case (a webserver). It
@@ -54,6 +58,7 @@ var repos = map[string]*repo{
5458
url: "https://go.googlesource.com/pkgsite",
5559
commit: "81f6f8d4175ad0bf6feaa03543cc433f8b04b19b",
5660
short: true,
61+
inDir: flag.String("pkgsite_dir", "", "if set, reuse this directory as pkgsite@81f6f8d4"),
5762
},
5863

5964
// A tiny self-contained project.
@@ -62,6 +67,7 @@ var repos = map[string]*repo{
6267
url: "https://github.com/google/starlark-go",
6368
commit: "3f75dec8e4039385901a30981e3703470d77e027",
6469
short: true,
70+
inDir: flag.String("starlark_dir", "", "if set, reuse this directory as starlark@3f75dec8"),
6571
},
6672

6773
// The current repository, which is medium-small and has very few dependencies.
@@ -70,6 +76,7 @@ var repos = map[string]*repo{
7076
url: "https://go.googlesource.com/tools",
7177
commit: "gopls/v0.9.0",
7278
short: true,
79+
inDir: flag.String("tools_dir", "", "if set, reuse this directory as x/[email protected]"),
7380
},
7481
}
7582

@@ -94,10 +101,11 @@ func getRepo(tb testing.TB, name string) *repo {
94101
// codebase.
95102
type repo struct {
96103
// static configuration
97-
name string // must be unique, used for subdirectory
98-
url string // repo url
99-
commit string // full commit hash or tag
100-
short bool // whether this repo runs with -short
104+
name string // must be unique, used for subdirectory
105+
url string // repo url
106+
commit string // full commit hash or tag
107+
short bool // whether this repo runs with -short
108+
inDir *string // if set, use this dir as url@commit, and don't delete
101109

102110
dirOnce sync.Once
103111
dir string // directory contaning source code checked out to url@commit
@@ -109,14 +117,37 @@ type repo struct {
109117
awaiter *Awaiter
110118
}
111119

120+
// reusableDir return a reusable directory for benchmarking, or "".
121+
//
122+
// If the user specifies a directory, the test will create and populate it
123+
// on the first run an re-use it on subsequent runs. Otherwise it will
124+
// create, populate, and delete a temporary directory.
125+
func (r *repo) reusableDir() string {
126+
if r.inDir == nil {
127+
return ""
128+
}
129+
return *r.inDir
130+
}
131+
112132
// getDir returns directory containing repo source code, creating it if
113133
// necessary. It is safe for concurrent use.
114134
func (r *repo) getDir() string {
115135
r.dirOnce.Do(func() {
116-
r.dir = filepath.Join(getTempDir(), r.name)
117-
log.Printf("cloning %s@%s into %s", r.url, r.commit, r.dir)
118-
if err := shallowClone(r.dir, r.url, r.commit); err != nil {
136+
if r.dir = r.reusableDir(); r.dir == "" {
137+
r.dir = filepath.Join(getTempDir(), r.name)
138+
}
139+
140+
_, err := os.Stat(r.dir)
141+
switch {
142+
case os.IsNotExist(err):
143+
log.Printf("cloning %s@%s into %s", r.url, r.commit, r.dir)
144+
if err := shallowClone(r.dir, r.url, r.commit); err != nil {
145+
log.Fatal(err)
146+
}
147+
case err != nil:
119148
log.Fatal(err)
149+
default:
150+
log.Printf("reusing %s as %s@%s", r.dir, r.url, r.commit)
120151
}
121152
})
122153
return r.dir
@@ -200,7 +231,7 @@ func (r *repo) Close() error {
200231
fmt.Fprintf(&errBuf, "closing sandbox: %v", err)
201232
}
202233
}
203-
if r.dir != "" {
234+
if r.dir != "" && r.reusableDir() == "" {
204235
if err := os.RemoveAll(r.dir); err != nil {
205236
fmt.Fprintf(&errBuf, "cleaning dir: %v", err)
206237
}

0 commit comments

Comments
 (0)