当前位置: 首页>>代码示例>>Golang>>正文


Golang pprof.StartCPUProfile函数代码示例

本文整理汇总了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)
	}
}
开发者ID:andreas-jonsson,项目名称:octatron,代码行数:33,代码来源:backend.go

示例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)
			}
		})
	}
}
开发者ID:sreis,项目名称:go,代码行数:27,代码来源:util.go

示例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()

}
开发者ID:postfix,项目名称:bufferManager.go,代码行数:34,代码来源:main.go

示例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)
	}
}
开发者ID:Debian,项目名称:dcs,代码行数:61,代码来源:importer.go

示例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())

}
开发者ID:elivoa,项目名称:gotapestry,代码行数:32,代码来源:goprofile.go

示例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)
	}
}
开发者ID:jebjerg,项目名称:bolt,代码行数:31,代码来源:bench.go

示例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
}
开发者ID:jmptrader,项目名称:encoding,代码行数:27,代码来源:benchtools.go

示例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)
		}
	}
}
开发者ID:Comcast,项目名称:rulio,代码行数:34,代码来源:util.go

示例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)
			}
		}()
	}
}
开发者ID:marcopaganini,项目名称:rclone,代码行数:61,代码来源:cmd.go

示例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)
}
开发者ID:jsfr,项目名称:SteinerExact,代码行数:25,代码来源:main.go

示例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()
		}
	}

}
开发者ID:maruel,项目名称:hugo,代码行数:32,代码来源:benchmark.go

示例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)
	}
}
开发者ID:cdupuis,项目名称:strava,代码行数:34,代码来源:main.go

示例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
	}
}
开发者ID:gulinfang,项目名称:GarageEngine,代码行数:31,代码来源:main.go

示例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)
	}
}
开发者ID:hialin,项目名称:hialin,代码行数:27,代码来源:main.go

示例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()
}
开发者ID:ASPecherkin,项目名称:Experiments,代码行数:33,代码来源:massAccess.go


注:本文中的runtime/pprof.StartCPUProfile函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。