Skip to content

Commit 33d7a2c

Browse files
authored
tools/check: adpot staged build to make ut a little bit faster (pingcap#51374)
ref pingcap#31880
1 parent c23f0c9 commit 33d7a2c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
# Copyright 2024 PingCAP, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
# See https://gist.github.com/howardjohn/c0f5d0bc293ef7d7fada533a2c9ffaf4
17+
# Usage: go test -exec=true -toolexec=go-compile-without-link -vet=off ./...
18+
# Preferably as an alias like `alias go-test-compile='go test -exec=true -toolexec=go-compile-without-link -vet=off'`
19+
# This will compile all tests, but not link them (which is the least cacheable part)
20+
21+
if [[ "${2}" == "-V=full" ]]; then
22+
"$@"
23+
exit 0
24+
fi
25+
case "$(basename ${1})" in
26+
link)
27+
# Output a dummy file
28+
touch "${3}"
29+
;;
30+
# We could skip vet as well, but it can be done with -vet=off if desired
31+
*)
32+
"$@"
33+
esac

tools/check/ut.go

+18
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,26 @@ func buildTestBinary(pkg string) error {
869869
return nil
870870
}
871871

872+
func generateBuildCache() error {
873+
// cd cmd/tidb-server && go test -tags intest -exec true -vet off -toolexec=go-compile-without-link
874+
cmd := exec.Command("go", "test", "-tags=intest", "-exec=true", "-vet=off")
875+
goCompileWithoutLink := fmt.Sprintf("-toolexec=%s/tools/check/go-compile-without-link.sh", workDir)
876+
cmd.Args = append(cmd.Args, goCompileWithoutLink)
877+
cmd.Dir = path.Join(workDir, "cmd/tidb-server")
878+
if err := cmd.Run(); err != nil {
879+
return withTrace(err)
880+
}
881+
return nil
882+
}
883+
872884
// buildTestBinaryMulti is much faster than build the test packages one by one.
873885
func buildTestBinaryMulti(pkgs []string) error {
886+
// staged build, generate the build cache for all the tests first, then generate the test binary.
887+
// This way is faster than generating test binaries directly, because the cache can be used.
888+
if err := generateBuildCache(); err != nil {
889+
return withTrace(err)
890+
}
891+
874892
// go test --exec=xprog -cover -vet=off --count=0 $(pkgs)
875893
xprogPath := path.Join(workDir, "tools/bin/xprog")
876894
packages := make([]string, 0, len(pkgs))

0 commit comments

Comments
 (0)