Text file src/cmd/go/testdata/script/build_pgo.txt

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

View as plain text