-
-
Save doujiang24/9d066bce3a2bdd0f1b9fe1ef49699e4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"runtime" | |
"runtime/pprof" | |
"strconv" | |
"time" | |
) | |
type Buffer struct { | |
} | |
func (buf *Buffer) Write(p []byte) (n int, err error) { | |
len := len(p) | |
return len, err | |
} | |
//go:noinline | |
func deepcall(n int) { | |
if n > 0 { | |
deepcall(n - 1) | |
} | |
time.Sleep(time.Second * 10) | |
} | |
func main() { | |
max := 0 | |
if len(os.Args) > 1 { | |
arg := os.Args[1] | |
max, _ = strconv.Atoi(arg) | |
} | |
fmt.Printf("expected number of goroutine: %v\n", max+1) | |
for i := 0; i < max; i++ { | |
go deepcall(10) | |
} | |
fmt.Printf("got num of goroutine: %v\n", runtime.NumGoroutine()) | |
time.Sleep(time.Second) | |
start := time.Now() | |
var buf Buffer | |
pprof.Lookup("goroutine").WriteTo(&buf, 0) | |
elasped := time.Since(start) | |
fmt.Printf("time cost: %v\n", elasped) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment