...

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

Documentation: cmd/go/testdata/script

     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.
     7env GOCACHE=$WORK/gocache
     8mkdir $GOCACHE
     9
    10# build without PGO
    11go build triv.go
    12
    13# build with PGO, should trigger rebuild
    14# starting with an empty profile (the compiler accepts it)
    15go build -x -pgo=prof -o triv.exe triv.go
    16stderr 'preprofile.*-i.*prof'
    17stderr '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.
    22go version -m triv.exe
    23stdout 'build\s+-pgo=.*'${/}'prof'
    24
    25# store the build ID
    26go list -export -json=BuildID -pgo=prof triv.go
    27stdout '"BuildID":' # check that output actually contains a build ID
    28cp stdout list.out
    29
    30# build again with the same profile, should be cached
    31go build -x -pgo=prof -o triv.exe triv.go
    32! stderr 'compile.*triv.go'
    33
    34# check that the build ID is the same
    35go list -export -json=BuildID -pgo=prof triv.go
    36cmp stdout list.out
    37
    38# overwrite the prof
    39go run overwrite.go
    40
    41# build again, profile content changed, should trigger rebuild, including std
    42go build -n -pgo=prof triv.go
    43stderr 'preprofile.*-i.*prof'
    44stderr 'compile.*-pgoprofile=.*triv.go'
    45stderr 'compile.*-p runtime.*-pgoprofile=.*'
    46
    47# check that the build ID is different
    48go list -export -json=BuildID -pgo=prof triv.go
    49! cmp stdout list.out
    50
    51# build with trimpath, buildinfo path should be trimmed
    52go build -x -pgo=prof -trimpath -o triv.exe triv.go
    53
    54# check that path is trimmed
    55go version -m triv.exe
    56stdout 'build\s+-pgo=prof'
    57
    58-- prof --
    59-- triv.go --
    60package main
    61func main() {}
    62-- overwrite.go --
    63package main
    64
    65import (
    66	"os"
    67	"runtime/pprof"
    68	"time"
    69)
    70
    71func 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}

View as plain text