1 # Test go build -pgo flag.
2 # Specifically, the build cache handles profile content correctly.
3
4 [short] skip 'compiles and links executables'
5
6 # Ensure the build ID from "go list -export" matches that of "go build".
7 env GOEXPERIMENT=nogolistexportnewformat
8
9 # Set up fresh GOCACHE.
10 env GOCACHE=$WORK/gocache
11 mkdir $GOCACHE
12
13 # build without PGO
14 go build triv.go
15
16 # build with PGO, should trigger rebuild
17 # starting with an empty profile (the compiler accepts it)
18 go build -x -pgo=prof -o triv.exe triv.go
19 stderr 'preprofile.*-i.*prof'
20 stderr 'compile.*-pgoprofile=.*triv.go'
21
22 # check that PGO appears in build info
23 # N.B. we can't start the stdout check with -pgo because the script assumes that
24 # if the first arg starts with - it is a grep flag.
25 go version -m triv.exe
26 stdout 'build\s+-pgo=.*'${/}'prof'
27
28 # store the build ID
29 go list -export -json=BuildID -pgo=prof triv.go
30 stdout '"BuildID":' # check that output actually contains a build ID
31 cp stdout list.out
32
33 # build again with the same profile, should be cached
34 go build -x -pgo=prof -o triv.exe triv.go
35 ! stderr 'compile.*triv.go'
36
37 # check that the build ID is the same
38 go list -export -json=BuildID -pgo=prof triv.go
39 cmp stdout list.out
40
41 # overwrite the prof
42 go run overwrite.go
43
44 # build again, profile content changed, should trigger rebuild, including std
45 go build -n -pgo=prof triv.go
46 stderr 'preprofile.*-i.*prof'
47 stderr 'compile.*-pgoprofile=.*triv.go'
48 stderr 'compile.*-p runtime.*-pgoprofile=.*'
49
50 # check that the build ID is different
51 go list -export -json=BuildID -pgo=prof triv.go
52 ! cmp stdout list.out
53
54 # build with trimpath, buildinfo path should be trimmed
55 go build -x -pgo=prof -trimpath -o triv.exe triv.go
56
57 # check that path is trimmed
58 go version -m triv.exe
59 stdout 'build\s+-pgo=prof'
60
61 -- prof --
62 -- triv.go --
63 package main
64 func main() {}
65 -- overwrite.go --
66 package main
67
68 import (
69 "os"
70 "runtime/pprof"
71 "time"
72 )
73
74 func main() {
75 f, err := os.Create("prof")
76 if err != nil {
77 panic(err)
78 }
79 err = pprof.StartCPUProfile(f)
80 if err != nil {
81 panic(err)
82 }
83 // Spin to ensure we get some samples. If we get no samples, the result
84 // is equivalent to an empty profile.
85 start := time.Now()
86 for time.Since(start) < 100*time.Millisecond {}
87 pprof.StopCPUProfile()
88 f.Close()
89 }
90
View as plain text