本文整理汇总了Golang中runtime/pprof.StartCPUProfile函数的典型用法代码示例。如果您正苦于以下问题:Golang StartCPUProfile函数的具体用法?Golang StartCPUProfile怎么用?Golang StartCPUProfile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了StartCPUProfile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
flag.Parse()
if arguments.pprof {
log.Println("pprof enabled")
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
fp, err := os.Create("backend.pprof")
if err != nil {
panic(err)
}
defer fp.Close()
pprof.StartCPUProfile(fp)
defer pprof.StopCPUProfile()
}
if err := loadTree(arguments.tree); err != nil {
log.Println(err)
os.Exit(-1)
}
http.Handle("/", http.FileServer(http.Dir(arguments.web)))
http.Handle("/render", websocket.Handler(renderServer))
log.Println("waiting for connections...")
if err := http.ListenAndServe(fmt.Sprintf(":%v", arguments.port), nil); err != nil {
log.Println(err)
os.Exit(-1)
}
}
示例2: startProfile
func startProfile() {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatalf("%v", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatalf("%v", err)
}
AtExit(pprof.StopCPUProfile)
}
if memprofile != "" {
if memprofilerate != 0 {
runtime.MemProfileRate = int(memprofilerate)
}
f, err := os.Create(memprofile)
if err != nil {
log.Fatalf("%v", err)
}
AtExit(func() {
runtime.GC() // profile all outstanding allocations
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatalf("%v", err)
}
})
}
}
示例3: main
func main() {
list_T := make([]*buffered.Token, M)
list_D := make([]*buffered.Data, M)
t := time.Now()
f, _ := os.Create("with_bufferManager.prof")
pprof.StartCPUProfile(f)
m := buffered.NewBufferManager(M * 2)
for i := 0; i < N; i++ {
for j := 0; j < M; j++ {
t := m.GetToken()
t.Data.Str = "haha"
list_T[j] = t
}
for j := 0; j < M; j++ {
list_T[j].Return()
}
}
fmt.Printf("With bufferManager: %v\n", time.Since(t))
pprof.StopCPUProfile()
t = time.Now()
f, _ = os.Create("without_bufferManager.prof")
pprof.StartCPUProfile(f)
for i := 0; i < N; i++ {
for j := 0; j < M; j++ {
d := new(buffered.Data)
d.Str = "haha"
list_D[j] = d
}
}
fmt.Printf("Without buffermanager (Relying on GC): %v\n", time.Since(t))
pprof.StopCPUProfile()
}
示例4: mergeToShard
// Merges all packages in *unpackedPath into a big index shard.
func mergeToShard() {
names := packageNames()
indexFiles := make([]string, 0, len(names))
for _, name := range names {
if strings.HasSuffix(name, ".idx") && name != "full.idx" {
indexFiles = append(indexFiles, filepath.Join(*unpackedPath, name))
}
}
filesInIndex.Set(float64(len(indexFiles)))
log.Printf("Got %d index files\n", len(indexFiles))
if len(indexFiles) < 2 {
return
}
tmpIndexPath, err := ioutil.TempFile(*unpackedPath, "newshard")
if err != nil {
log.Fatal(err)
}
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
t0 := time.Now()
index.ConcatN(tmpIndexPath.Name(), indexFiles...)
t1 := time.Now()
log.Printf("merged in %v\n", t1.Sub(t0))
//for i := 1; i < len(indexFiles); i++ {
// log.Printf("merging %s with %s\n", indexFiles[i-1], indexFiles[i])
// t0 := time.Now()
// index.Concat(tmpIndexPath.Name(), indexFiles[i-1], indexFiles[i])
// t1 := time.Now()
// log.Printf("merged in %v\n", t1.Sub(t0))
//}
log.Printf("merged into shard %s\n", tmpIndexPath.Name())
// If full.idx does not exist (i.e. on initial deployment), just move the
// new index to full.idx, the dcs-index-backend will not be running anyway.
fullIdxPath := filepath.Join(*unpackedPath, "full.idx")
if _, err := os.Stat(fullIdxPath); os.IsNotExist(err) {
if err := os.Rename(tmpIndexPath.Name(), fullIdxPath); err != nil {
log.Fatal(err)
}
return
}
successfulMerges.Inc()
// Replace the current index with the newly created index.
if _, err := indexBackend.ReplaceIndex(context.Background(), &proto.ReplaceIndexRequest{ReplacementPath: filepath.Base(tmpIndexPath.Name())}); err != nil {
log.Fatalf("dcs-index-backend ReplaceIndex failed: %v", err)
}
}
示例5: test
func test() {
f, _ := os.Create("ls.prof")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer func() {
pprof.StopCPUProfile()
}()
}
// ...
t := &T{}
for i := 0; i < 1000000; i++ {
v := reflect.ValueOf(t)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
t.B = append(t.B, i)
t.A = append(t.A, sumtest())
}
// var buffer bytes.Buffer
pprof.WriteHeapProfile(f)
// fmt.Println(buffer.String())
}
示例6: benchStartProfiling
// Starts all profiles set on the options.
func benchStartProfiling(options *BenchOptions) {
var err error
// Start CPU profiling.
if options.CPUProfile != "" {
cpuprofile, err = os.Create(options.CPUProfile)
if err != nil {
fatal("bench: could not create cpu profile %q: %v", options.CPUProfile, err)
}
pprof.StartCPUProfile(cpuprofile)
}
// Start memory profiling.
if options.MemProfile != "" {
memprofile, err = os.Create(options.MemProfile)
if err != nil {
fatal("bench: could not create memory profile %q: %v", options.MemProfile, err)
}
runtime.MemProfileRate = 4096
}
// Start fatal profiling.
if options.BlockProfile != "" {
blockprofile, err = os.Create(options.BlockProfile)
if err != nil {
fatal("bench: could not create block profile %q: %v", options.BlockProfile, err)
}
runtime.SetBlockProfileRate(1)
}
}
示例7: RunCompress
func RunCompress(codec encoding.Integer, in []int32, length int, prof bool) (duration int64, out []int32, err error) {
out = make([]int32, length*2)
inpos := cursor.New()
outpos := cursor.New()
now := time.Now()
if prof {
f, e := os.Create("cpu.compress.pprof")
if e != nil {
log.Fatal(e)
}
defer f.Close()
pprof.StartCPUProfile(f)
}
if err = codec.Compress(in, inpos, len(in), out, outpos); err != nil {
return 0, nil, err
}
since := time.Since(now).Nanoseconds()
if prof {
pprof.StopCPUProfile()
}
return since, out[:outpos.Get()], nil
}
示例8: Profile
// Profile starts CPU and memory profiling and returns a function that will stop that.
//
//
// Writes "cpu" + filename and "mem" + filename.
//
// Usage:
//
// defer Profile("logfast.prof")()
//
func Profile(filename string) func() {
cpu, err := os.Create("cpu" + filename)
if err != nil {
panic(err)
}
mem, err := os.Create("mem" + filename)
if err != nil {
panic(err)
}
then := time.Now()
log.Printf("Profile starting %s\n", filename)
pprof.StartCPUProfile(cpu)
pprof.WriteHeapProfile(mem)
return func() {
elapsed := time.Now().Sub(then)
log.Printf("Profile stopping %s (elapsed %v)\n", filename, elapsed)
pprof.StopCPUProfile()
if err := mem.Close(); err != nil {
log.Printf("error closing mem profile (%v)", err)
}
}
}
示例9: initConfig
// initConfig is run by cobra after initialising the flags
func initConfig() {
// Log file output
if *logFile != "" {
f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
_, err = f.Seek(0, os.SEEK_END)
if err != nil {
fs.ErrorLog(nil, "Failed to seek log file to end: %v", err)
}
log.SetOutput(f)
fs.DebugLogger.SetOutput(f)
redirectStderr(f)
}
// Load the rest of the config now we have started the logger
fs.LoadConfig()
// Write the args for debug purposes
fs.Debug("rclone", "Version %q starting with parameters %q", fs.Version, os.Args)
// Setup CPU profiling if desired
if *cpuProfile != "" {
fs.Log(nil, "Creating CPU profile %q\n", *cpuProfile)
f, err := os.Create(*cpuProfile)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
err = pprof.StartCPUProfile(f)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
// Setup memory profiling if desired
if *memProfile != "" {
defer func() {
fs.Log(nil, "Saving Memory profile %q\n", *memProfile)
f, err := os.Create(*memProfile)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
err = pprof.WriteHeapProfile(f)
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
err = f.Close()
if err != nil {
fs.Stats.Error()
log.Fatal(err)
}
}()
}
}
示例10: main
func main() {
c := initConfig()
offset := 0
if c.Offset {
offset = 1
}
if c.CPUProfile != "" {
f, err := os.Create(c.CPUProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if c.SortPoints {
c.Points.SortPoints()
}
t := smt.InitTree(&c.Points)
optimize(t, offset, c.Iteration)
}
示例11: bench
func bench(cmd *cobra.Command, args []string) {
if memProfilefile != "" {
f, err := os.Create(memProfilefile)
if err != nil {
panic(err)
}
for i := 0; i < benchmarkTimes; i++ {
_ = buildSite()
}
pprof.WriteHeapProfile(f)
f.Close()
} else {
if cpuProfilefile == "" {
cpuProfilefile = "/tmp/hugo-cpuprofile"
}
f, err := os.Create(cpuProfilefile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
for i := 0; i < benchmarkTimes; i++ {
_ = buildSite()
}
}
}
示例12: startProfiling
// Starts all profiles set on the options.
func (cmd *BenchCommand) startProfiling(options *BenchOptions) {
var err error
// Start CPU profiling.
if options.CPUProfile != "" {
cpuprofile, err = os.Create(options.CPUProfile)
if err != nil {
fmt.Fprintf(cmd.Stderr, "bench: could not create cpu profile %q: %v\n", options.CPUProfile, err)
os.Exit(1)
}
pprof.StartCPUProfile(cpuprofile)
}
// Start memory profiling.
if options.MemProfile != "" {
memprofile, err = os.Create(options.MemProfile)
if err != nil {
fmt.Fprintf(cmd.Stderr, "bench: could not create memory profile %q: %v\n", options.MemProfile, err)
os.Exit(1)
}
runtime.MemProfileRate = 4096
}
// Start fatal profiling.
if options.BlockProfile != "" {
blockprofile, err = os.Create(options.BlockProfile)
if err != nil {
fmt.Fprintf(cmd.Stderr, "bench: could not create block profile %q: %v\n", options.BlockProfile, err)
os.Exit(1)
}
runtime.SetBlockProfileRate(1)
}
}
示例13: main
func main() {
runtime.GOMAXPROCS(8)
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Errorf("%s\n", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
file, _ := os.Create("./log.txt")
os.Stdout = file
os.Stderr = file
os.Stdin = file
defer file.Close()
Start()
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
fmt.Errorf("%s\n", err)
}
pprof.WriteHeapProfile(f)
f.Close()
return
}
}
示例14: main
func main() {
defer tlog.FuncLog(tlog.Func("main"))
cpuProfile := flag.String("cpu-profile", "", "write cpu profile to file")
isServerProcess := flag.Bool("s", false, "start hialin server only")
flag.Parse()
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
tlog.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *isServerProcess {
serverMain()
} else if singleProcess {
go serverMain()
client.Main(serverAddress)
} else {
findOrExecServer()
client.Main(serverAddress)
}
}
示例15: main
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Println("Error: ", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
fmt.Println("Error: ", err)
}
pprof.WriteHeapProfile(f)
}
var wg sync.WaitGroup
cidrs, err := parseCidrs("./hosts.json")
if err != nil {
log.Fatal(err)
}
servers := make([]Server, 0, 10)
for k := range cidrs.Cidrs {
servers = append(servers, Server{Cidr: cidrs.Cidrs[k]})
}
fmt.Println(len(servers))
for k := range servers {
wg.Add(1)
go servers[k].checkIP(&wg)
}
wg.Wait()
}