github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/profile/example_test.go (about) 1 package profile_test 2 3 import ( 4 "flag" 5 "os" 6 7 "github.com/angenalZZZ/gofunc/profile" 8 ) 9 10 func ExampleStart() { 11 // start a simple CPU profile and register 12 // a defer to Stop (flush) the profiling data. 13 defer profile.Start().Stop() 14 } 15 16 func ExampleCPUProfile() { 17 // CPU profiling is the default profiling mode, but you can specify it 18 // explicitly for completeness. 19 defer profile.Start(profile.CPUProfile).Stop() 20 } 21 22 func ExampleMemProfile() { 23 // use memory profiling, rather than the default cpu profiling. 24 defer profile.Start(profile.MemProfile).Stop() 25 } 26 27 func ExampleMemProfileRate() { 28 // use memory profiling with custom rate. 29 defer profile.Start(profile.MemProfileRate(2048)).Stop() 30 } 31 32 func ExampleMemProfileHeap() { 33 // use heap memory profiling. 34 defer profile.Start(profile.MemProfileHeap).Stop() 35 } 36 37 func ExampleMemProfileAllocs() { 38 // use allocs memory profiling. 39 defer profile.Start(profile.MemProfileAllocs).Stop() 40 } 41 42 func ExampleProfilePath() { 43 // set the location that the profile will be written to 44 defer profile.Start(profile.ProfilePath(os.Getenv("HOME"))).Stop() 45 } 46 47 func ExampleNoShutdownHook() { 48 // disable the automatic shutdown hook. 49 defer profile.Start(profile.NoShutdownHook).Stop() 50 } 51 52 func ExampleStart_withFlags() { 53 // use the flags package to selectively enable profiling. 54 mode := flag.String("profile.mode", "", "enable profiling mode, one of [cpu, mem, mutex, block]") 55 flag.Parse() 56 switch *mode { 57 case "cpu": 58 defer profile.Start(profile.CPUProfile).Stop() 59 case "mem": 60 defer profile.Start(profile.MemProfile).Stop() 61 case "mutex": 62 defer profile.Start(profile.MutexProfile).Stop() 63 case "block": 64 defer profile.Start(profile.BlockProfile).Stop() 65 default: 66 // do nothing 67 } 68 }