Skip to content

Instantly share code, notes, and snippets.

@doujiang24
Created January 25, 2022 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doujiang24/9d066bce3a2bdd0f1b9fe1ef49699e4e to your computer and use it in GitHub Desktop.
Save doujiang24/9d066bce3a2bdd0f1b9fe1ef49699e4e to your computer and use it in GitHub Desktop.
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