-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Improve the speed of do-nothing build. #24850
Merged
k8s-github-robot
merged 7 commits into
kubernetes:master
from
madhusudancs:speedup-do-nothing
May 22, 2016
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dcaf005
Improve the speed of do-nothing build.
madhusudancs dd15435
Address review comments.
madhusudancs 704ef5b
Replace log with glog.
madhusudancs 55589e0
Virtualize the constructs a bit to make the code testable.
madhusudancs 317a7d2
Write the first test: Isn't it strange that the elaborate setup that …
madhusudancs 7e486bd
Build and use the teststale binary on only the host platform.
madhusudancs 47efee7
Add more tests.
madhusudancs File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,209 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// teststale checks the staleness of a test binary. go test -c builds a test | ||
// binary but it does no staleness check. In other words, every time one runs | ||
// go test -c, it compiles the test packages and links the binary even when | ||
// nothing has changed. This program helps to mitigate that problem by allowing | ||
// to check the staleness of a given test package and its binary. | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
) | ||
|
||
const usageHelp = "" + | ||
`This program checks the staleness of a given test package and its test | ||
binary so that one can make a decision about re-building the test binary. | ||
|
||
Usage: | ||
teststale -binary=/path/to/test/binary -package=package | ||
|
||
Example: | ||
teststale -binary="$HOME/gosrc/bin/e2e.test" -package="k8s.io/kubernetes/test/e2e" | ||
|
||
` | ||
|
||
var ( | ||
binary = flag.String("binary", "", "filesystem path to the test binary file. Example: \"$HOME/gosrc/bin/e2e.test\"") | ||
pkgPath = flag.String("package", "", "import path of the test package in the format used while importing packages. Example: \"k8s.io/kubernetes/test/e2e\"") | ||
) | ||
|
||
func usage() { | ||
fmt.Fprintln(os.Stderr, usageHelp) | ||
fmt.Fprintln(os.Stderr, "Flags:") | ||
flag.PrintDefaults() | ||
os.Exit(2) | ||
} | ||
|
||
// golist is an interface emulating the `go list` command to get package information. | ||
// TODO: Evaluate using `go/build` package instead. It doesn't provide staleness | ||
// information, but we can probably run `go list` and `go/build.Import()` concurrently | ||
// in goroutines and merge the results. Evaluate if that's faster. | ||
type golist interface { | ||
pkgInfo(pkgPaths []string) ([]pkg, error) | ||
} | ||
|
||
// execmd implements the `golist` interface. | ||
type execcmd struct { | ||
cmd string | ||
args []string | ||
env []string | ||
} | ||
|
||
func (e *execcmd) pkgInfo(pkgPaths []string) ([]pkg, error) { | ||
args := append(e.args, pkgPaths...) | ||
cmd := exec.Command(e.cmd, args...) | ||
cmd.Env = e.env | ||
|
||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to obtain the metadata output stream: %v", err) | ||
} | ||
|
||
dec := json.NewDecoder(stdout) | ||
|
||
// Start executing the command | ||
if err := cmd.Start(); err != nil { | ||
return nil, fmt.Errorf("command did not start: %v", err) | ||
} | ||
|
||
var pkgs []pkg | ||
for { | ||
var p pkg | ||
if err := dec.Decode(&p); err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal metadata for package %s: %v", p.ImportPath, err) | ||
} | ||
pkgs = append(pkgs, p) | ||
} | ||
|
||
if err := cmd.Wait(); err != nil { | ||
return nil, fmt.Errorf("command did not complete: %v", err) | ||
} | ||
return pkgs, nil | ||
} | ||
|
||
type pkg struct { | ||
Dir string | ||
ImportPath string | ||
Target string | ||
Stale bool | ||
TestGoFiles []string | ||
TestImports []string | ||
XTestGoFiles []string | ||
XTestImports []string | ||
} | ||
|
||
func (p *pkg) isNewerThan(cmd golist, buildTime time.Time) bool { | ||
// If the package itself is stale, then we have to rebuild the whole thing anyway. | ||
if p.Stale { | ||
return true | ||
} | ||
|
||
// Test for file staleness | ||
for _, f := range p.TestGoFiles { | ||
if isNewerThan(filepath.Join(p.Dir, f), buildTime) { | ||
glog.V(4).Infof("test Go file %s is stale", f) | ||
return true | ||
} | ||
} | ||
for _, f := range p.XTestGoFiles { | ||
if isNewerThan(filepath.Join(p.Dir, f), buildTime) { | ||
glog.V(4).Infof("external test Go file %s is stale", f) | ||
return true | ||
} | ||
} | ||
|
||
imps := []string{} | ||
imps = append(imps, p.TestImports...) | ||
imps = append(imps, p.XTestImports...) | ||
|
||
// This calls `go list` the second time. This is required because the first | ||
// call to `go list` checks the staleness of the package in question by | ||
// looking the non-test dependencies, but it doesn't look at the test | ||
// dependencies. However, it returns the list of test dependencies. This | ||
// second call to `go list` checks the staleness of all the test | ||
// dependencies. | ||
pkgs, err := cmd.pkgInfo(imps) | ||
if err != nil || len(pkgs) < 1 { | ||
glog.V(4).Infof("failed to obtain metadata for packages %s: %v", imps, err) | ||
return true | ||
} | ||
|
||
for _, p := range pkgs { | ||
if p.Stale { | ||
glog.V(4).Infof("import %q is stale", p.ImportPath) | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func isNewerThan(filename string, buildTime time.Time) bool { | ||
stat, err := os.Stat(filename) | ||
if err != nil { | ||
return true | ||
} | ||
return stat.ModTime().After(buildTime) | ||
} | ||
|
||
// isTestStale checks if the test binary is stale and needs to rebuilt. | ||
// Some of the ideas here are inspired by how Go does staleness checks. | ||
func isTestStale(cmd golist, binPath, pkgPath string) bool { | ||
bStat, err := os.Stat(binPath) | ||
if err != nil { | ||
glog.V(4).Infof("Couldn't obtain the modified time of the binary %s: %v", binPath, err) | ||
return true | ||
} | ||
buildTime := bStat.ModTime() | ||
|
||
pkgs, err := cmd.pkgInfo([]string{pkgPath}) | ||
if err != nil || len(pkgs) < 1 { | ||
glog.V(4).Infof("Couldn't retrieve test package information for package %s: %v", pkgPath, err) | ||
return false | ||
} | ||
|
||
return pkgs[0].isNewerThan(cmd, buildTime) | ||
} | ||
|
||
func main() { | ||
flag.Usage = usage | ||
flag.Parse() | ||
|
||
cmd := &execcmd{ | ||
cmd: "go", | ||
args: []string{ | ||
"list", | ||
"-json", | ||
}, | ||
env: os.Environ(), | ||
} | ||
if !isTestStale(cmd, *binary, *pkgPath) { | ||
os.Exit(1) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments on the point of this program?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.