Skip to content

Commit

Permalink
Demo generate a CPU profile.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmallard committed Mar 8, 2014
1 parent 62f4719 commit dc66b1d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -69,6 +69,7 @@ dirs = testppack \
rangecopy \
ranges \
recover \
runpprof \
shax \
shifter \
show64 \
Expand Down
43 changes: 43 additions & 0 deletions runpprof/runpprof.go
@@ -0,0 +1,43 @@
/*
Demo generating a CPU profile.
*/
package main

import (
"fmt"
"os"
"runtime/pprof"
"time"
)

func runner() {
for i := 0; i < 1000000000; i++ {
for j := 0; j < 30; j++ { //
_ = i + j
}
}
}

func main() {
s := time.Now()
fmt.Println("Start", s)
//
f, err := os.Create("./cpu.prof")
if err != nil {
panic(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
panic(err)
}
runner()
pprof.StopCPUProfile()
//
err = f.Close()
if err != nil {
panic(err)
}
e := time.Now()
fmt.Println("End", s)
fmt.Println("Elapsed", e.Sub(s))
}

0 comments on commit dc66b1d

Please sign in to comment.