// test malloc in heap or stack function func TestIfObjectInStack(t *testing.T) { amount := 10000 for i := 0; i < amount; i++ { ob := Object{} if ob.a == "aaa" { ob.a = "bbb" } freeObject(&ob) } fmt.Println(lenPool()) ob := &Object{} fmt.Println("----------------expect malloc in heap--------------") pprof.MemStats() for i := 0; i < amount; i++ { s := expectMallocInHeap(ob) // never hit this statment use s to prevent compile optimize code if s.a == "aaa" { fmt.Println("sssss") } } pprof.MemStats() pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc) fmt.Println("-------------- expect malloc in stack--------------") pprof.MemStats() for i := 0; i < amount; i++ { expectMallocInStack(ob) } pprof.MemStats() pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc) pprof.ProcessStats() fmt.Println("----------------expect malloc in mem pool--------------") pprof.MemStats() for i := 0; i < amount; i++ { s := newObjectFromPool() // never hit this statment use s to prevent compile optimize code if s.a == "aaa" { fmt.Println("sssss") } freeObject(s) } pprof.MemStats() pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc) fmt.Print("\n\n") }
// list amount of goroutines and memory occupied func TestGoroutineCost(t *testing.T) { amount := 100000 pprof.MemStats() fmt.Println(pprof.Current()) fmt.Printf("create goroutines amount: %d\n", amount) var wg sync.WaitGroup for i := 0; i < amount; i++ { wg.Add(1) go func() { defer wg.Done() time.Sleep(time.Second * 10) }() } wg.Wait() fmt.Println(pprof.Current()) pprof.ProcessStats() pprof.MemStats() pprof.StatIncrement(pprof.TotalAlloc) fmt.Print("\n\n") }