Home | History | Annotate | Download | only in sample
      1 // sample program that is used to produce some of the files in
      2 // pprof/internal/report/testdata.
      3 package main
      4 
      5 import (
      6 	"flag"
      7 	"fmt"
      8 	"log"
      9 	"math"
     10 	"os"
     11 	"runtime/pprof"
     12 )
     13 
     14 var cpuProfile = flag.String("cpuprofile", "", "where to write cpu profile")
     15 
     16 func main() {
     17 	flag.Parse()
     18 	f, err := os.Create(*cpuProfile)
     19 	if err != nil {
     20 		log.Fatal("could not create CPU profile: ", err)
     21 	}
     22 	if err := pprof.StartCPUProfile(f); err != nil {
     23 		log.Fatal("could not start CPU profile: ", err)
     24 	}
     25 	defer pprof.StopCPUProfile()
     26 	busyLoop()
     27 }
     28 
     29 func busyLoop() {
     30 	m := make(map[int]int)
     31 	for i := 0; i < 1000000; i++ {
     32 		m[i] = i + 10
     33 	}
     34 	var sum float64
     35 	for i := 0; i < 100; i++ {
     36 		for _, v := range m {
     37 			sum += math.Abs(float64(v))
     38 		}
     39 	}
     40 	fmt.Println("Sum", sum)
     41 }
     42