Esempio n. 1
1
File: util.go Progetto: sreis/go
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)
			}
		})
	}
}
Esempio n. 2
0
// runBenchmarkOnce runs f once and collects all performance metrics and profiles.
func runBenchmarkOnce(f func(uint64), N uint64) Result {
	latencyInit(N)
	runtime.GC()
	mstats0 := new(runtime.MemStats)
	runtime.ReadMemStats(mstats0)
	ss := InitSysStats(N)
	res := MakeResult()
	res.N = N
	res.Files["memprof0"] = tempFilename("memprof")
	memprof0, err := os.Create(res.Files["memprof0"])
	if err != nil {
		log.Fatalf("Failed to create profile file '%v': %v", res.Files["memprof0"], err)
	}
	pprof.WriteHeapProfile(memprof0)
	memprof0.Close()

	res.Files["cpuprof"] = tempFilename("cpuprof")
	cpuprof, err := os.Create(res.Files["cpuprof"])
	if err != nil {
		log.Fatalf("Failed to create profile file '%v': %v", res.Files["cpuprof"], err)
	}
	defer cpuprof.Close()
	pprof.StartCPUProfile(cpuprof)
	t0 := time.Now()
	f(N)
	res.Duration = time.Since(t0)
	res.RunTime = uint64(time.Since(t0)) / N
	res.Metrics["time"] = res.RunTime
	pprof.StopCPUProfile()

	latencyCollect(&res)
	ss.Collect(&res)

	res.Files["memprof"] = tempFilename("memprof")
	memprof, err := os.Create(res.Files["memprof"])
	if err != nil {
		log.Fatalf("Failed to create profile file '%v': %v", res.Files["memprof"], err)
	}
	pprof.WriteHeapProfile(memprof)
	memprof.Close()

	mstats1 := new(runtime.MemStats)
	runtime.ReadMemStats(mstats1)
	res.Metrics["allocated"] = (mstats1.TotalAlloc - mstats0.TotalAlloc) / N
	res.Metrics["allocs"] = (mstats1.Mallocs - mstats0.Mallocs) / N
	res.Metrics["sys-total"] = mstats1.Sys
	res.Metrics["sys-heap"] = mstats1.HeapSys
	res.Metrics["sys-stack"] = mstats1.StackSys
	res.Metrics["gc-pause-total"] = (mstats1.PauseTotalNs - mstats0.PauseTotalNs) / N
	collectGo12MemStats(&res, mstats0, mstats1)
	numGC := uint64(mstats1.NumGC - mstats0.NumGC)
	if numGC == 0 {
		res.Metrics["gc-pause-one"] = 0
	} else {
		res.Metrics["gc-pause-one"] = (mstats1.PauseTotalNs - mstats0.PauseTotalNs) / numGC
	}
	return res
}
Esempio n. 3
0
func main() {
	inputPath := flag.String("input", "-", "Input filename, '-' for stdin")
	cpuprofile := flag.String("cpuprofile", "", "Write CPU profile to file")
	memprofile := flag.String("memprofile", "", "Write memory profile to file")
	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Println(err.Error())
			os.Exit(1)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
		defer f.Close()
	}
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Println(err.Error())
			os.Exit(1)
		}
		go func() {
			for {
				time.Sleep(5 * time.Second)
				pprof.WriteHeapProfile(f)
			}
		}()
		defer pprof.WriteHeapProfile(f)
		defer f.Close()
	}

	sigIntChan := make(chan os.Signal, 1)
	signal.Notify(sigIntChan, syscall.SIGINT)
	go func() {
		<-sigIntChan
		os.Exit(1)
	}()

	// Initialize input reading
	log.Println("Reading from", *inputPath)
	var inputFile *os.File = os.Stdin
	if *inputPath != "-" {
		var err error
		inputFile, err = os.Open(*inputPath)
		if err != nil {
			log.Println(err.Error())
			os.Exit(1)
		}
		defer inputFile.Close()
	}
	input := bufio.NewReader(inputFile)
	// TODO
	output := os.Stdout

	process(Config, input, output)
}
Esempio n. 4
0
func main() {
	go func() {
		if *memprofile != "" {
			written := false
			for {
				if written {
					os.Remove(*memprofile)
				}

				f, err := os.Create(*memprofile)
				if err != nil {
					log.Fatal(err)
				}
				pprof.WriteHeapProfile(f)
				time.Sleep(1 * time.Millisecond)
				f.Close()
			}
		}
	}()

	args, err := GetLegacyArguments()

	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if args.Help {
		flag.Usage()
		return
	}

	legacy, err := args.GetLegacy()
	if err != nil {
		log.Println(err)
		return
	}

	legacy.SetupLogging()
	legacy.Run()

	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}

	legacy.ShutdownLogging()
}
Esempio n. 5
0
// 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)
			}
		}()
	}
}
Esempio n. 6
0
// 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)
		}
	}
}
Esempio n. 7
0
func saveHeapProfiles() {
	runtime.MemProfileRate = 1
	var memstats, prevMemstats runtime.MemStats

	t0 := time.Now()
	for t := range time.NewTicker(250 * time.Millisecond).C {
		startms := int(t.Sub(t0).Seconds() * 1000)
		runtime.ReadMemStats(&memstats)
		if memstats.HeapInuse > prevMemstats.HeapInuse {
			fd, err := os.Create(fmt.Sprintf("heap-%05d-%07d.pprof", syscall.Getpid(), startms))
			if err != nil {
				panic(err)
			}
			err = pprof.WriteHeapProfile(fd)
			if err != nil {
				panic(err)
			}
			err = fd.Close()
			if err != nil {
				panic(err)
			}
			prevMemstats = memstats
		}
	}
}
Esempio n. 8
0
func main() {
	flag.Parse()
	log.SetFlags(log.Lshortfile | log.Ltime)
	fmt.Printf(
		`lldb:      version exp
Keys:       16 bytes each
Values:     100 bytes each (50 bytes after compression)
Entries:    1000000
RawSize:    110.6 MB (estimated)
FileSize:   62.9 MB (estimated)
------------------------------------------------
`)
	if *memprofile != "" {
		runtime.MemProfileRate = 1
	}
	fillseq()
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}

}
Esempio n. 9
0
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()
}
Esempio n. 10
0
func main() {
	f, err := os.Create("cpuprofile.prof")
	if err != nil {
		log.Fatal(err)
	}
	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()

	fmt.Printf("GoHM Software Version [%s]\n", TLibCommon.NV_VERSION)
	if len(os.Args) <= 2 {
		fmt.Printf("Usage: \n")
		fmt.Printf("	HM Encoder: gohm.exe -c encoder.cfg [trace.txt]\n")
		fmt.Printf("	HM Decoder: gohm.exe -d test.bin test.yuv [n trace.txt]\n")
	} else {
		if os.Args[1] == "-c" {
			Encoder()
		} else if os.Args[1] == "-d" {
			Decoder()
		} else {
			fmt.Printf("Unknown argment %s\n", os.Args[1])
		}
	}

	m, err := os.Create("memprofile.prof")
	if err != nil {
		log.Fatal(err)
	}
	pprof.WriteHeapProfile(m)
	m.Close()
}
Esempio n. 11
0
File: main.go Progetto: houcy/gofast
func main() {
	argParse()
	runtime.GOMAXPROCS(runtime.NumCPU())

	// start cpu profile.
	fname := "client.pprof"
	fd, err := os.Create(fname)
	if err != nil {
		log.Fatalf("unable to create %q: %v\n", fname, err)
	}
	defer fd.Close()
	pprof.StartCPUProfile(fd)
	defer pprof.StopCPUProfile()

	doTransport()

	// take memory profile.
	fname = "client.mprof"
	fd, err = os.Create(fname)
	if err != nil {
		log.Fatal(err)
	}
	defer fd.Close()
	pprof.WriteHeapProfile(fd)
}
Esempio n. 12
0
func cleanup() {
	Debug("cleanup")

	// write memory profile
	if *flag_memprof != "" {
		f, err := os.Create(*flag_memprof)
		if err != nil {
			Log(err)
		}
		Log("Writing memory profile to", *flag_memprof)
		pprof.WriteHeapProfile(f)
		f.Close()
	}

	// write cpu profile
	if *flag_cpuprof != "" {
		Log("Flushing CPU profile", *flag_cpuprof)
		pprof.StopCPUProfile()
	}

	// print timers
	if *flag_debug {
		PrintTimers()
	}

	cuda.DeviceReset()
	// kill subprocess?
}
Esempio n. 13
0
// Start starts listeners
func (s *Service) Start() error {
	s.Dispatcher.Run()

	f, err := os.Create("cpu.prof")
	if err != nil {
		log.Fatal("could not create CPU profile: ", err)
	}
	if err := pprof.StartCPUProfile(f); err != nil {
		log.Fatal("could not start CPU profile: ", err)
	}
	defer pprof.StopCPUProfile()

	f2, err := os.Create("mem.prof")
	if err != nil {
		log.Fatal("could not create memory profile: ", err)
	}
	runtime.GC() // get up-to-date statistics
	if err := pprof.WriteHeapProfile(f2); err != nil {
		log.Fatal("could not write memory profile: ", err)
	}
	f2.Close()

	server := &http.Server{
		Addr:           fmt.Sprintf("%s:%d", s.config.Interface, s.config.Port),
		Handler:        &myHandler{},
		ReadTimeout:    2 * time.Second,
		WriteTimeout:   2 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	mux = make(map[string]func(http.ResponseWriter, *http.Request))
	mux["/v1/events"] = s.eventConsumerHandler

	return server.ListenAndServe()
}
Esempio n. 14
0
func doprofile(fn string) {
	var err error
	var fc, fh, ft *os.File
	for i := 1; i > 0; i++ {
		fc, err = os.Create(fn + "-cpu-" + strconv.Itoa(i) + ".prof")
		if err != nil {
			log.Fatal(err)
		}

		pprof.StartCPUProfile(fc)
		time.Sleep(300 * time.Second)
		pprof.StopCPUProfile()
		fc.Close()

		fh, err = os.Create(fn + "-heap-" + strconv.Itoa(i) + ".prof")
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(fh)
		fh.Close()

		ft, err = os.Create(fn + "-threadcreate-" + strconv.Itoa(i) + ".prof")
		if err != nil {
			log.Fatal(err)
		}
		pprof.Lookup("threadcreate").WriteTo(ft, 0)
		ft.Close()
		log.Println("Created CPU, heap and threadcreate profile of 300 seconds")
	}
}
Esempio n. 15
0
// after runs after all testing.
func after() {
	if *cpuProfile != "" {
		pprof.StopCPUProfile() // flushes profile to disk
	}
	if *memProfile != "" {
		f, err := os.Create(*memProfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s", err)
			return
		}
		if err = pprof.WriteHeapProfile(f); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *memProfile, err)
		}
		f.Close()
	}
	if *blockProfile != "" && *blockProfileRate >= 0 {
		f, err := os.Create(*blockProfile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s", err)
			return
		}
		if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *blockProfile, err)
		}
		f.Close()
	}
}
Esempio n. 16
0
func main() {
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	lsgraph := NewLSG()
	cfgraph := NewCFG()

	cfgraph.CreateNode(0) // top
	cfgraph.CreateNode(1) // bottom
	NewBasicBlockEdge(cfgraph, 0, 2)

	for dummyloop := 0; dummyloop < 15000; dummyloop++ {
		FindHavlakLoops(cfgraph, NewLSG())
	}

	n := 2

	for parlooptrees := 0; parlooptrees < 10; parlooptrees++ {
		cfgraph.CreateNode(n + 1)
		buildConnect(cfgraph, 2, n+1)
		n = n + 1

		for i := 0; i < 100; i++ {
			top := n
			n = buildStraight(cfgraph, n, 1)
			for j := 0; j < 25; j++ {
				n = buildBaseLoop(cfgraph, n)
			}
			bottom := buildStraight(cfgraph, n, 1)
			buildConnect(cfgraph, n, top)
			n = bottom
		}
		buildConnect(cfgraph, n, 1)
	}

	FindHavlakLoops(cfgraph, lsgraph)
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}

	for i := 0; i < 50; i++ {
		FindHavlakLoops(cfgraph, NewLSG())
	}

	fmt.Printf("# of loops: %d (including 1 artificial root node)\n", lsgraph.NumLoops())
	lsgraph.CalculateNestingLevel()
}
Esempio n. 17
0
func main() {
	flag.Parse()
	t := &trie.Tree{}

	f, err := os.Open(*file)
	if err != nil {
		log.Fatalf("os.Open error: %v", err)
	}

	reader := bufio.NewReader(f)
	line, isPrefix, err := reader.ReadLine()

	for err == nil {
		if isPrefix {
			log.Fatalf("line bigger than buffer")
		}

		t.Add(string(line))

		line, isPrefix, err = reader.ReadLine()
	}

	runtime.GC()

	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
	}
	fmt.Fprintf(os.Stdout, "Done building ternary search tree\n")
	<-time.After(time.Hour * 24)
}
Esempio n. 18
0
// ShutdownContext shuts context down
func ShutdownContext() {
	if aptly.EnableDebug {
		if context.fileMemProfile != nil {
			pprof.WriteHeapProfile(context.fileMemProfile)
			context.fileMemProfile.Close()
			context.fileMemProfile = nil
		}
		if context.fileCPUProfile != nil {
			pprof.StopCPUProfile()
			context.fileCPUProfile.Close()
			context.fileCPUProfile = nil
		}
		if context.fileMemProfile != nil {
			context.fileMemProfile.Close()
			context.fileMemProfile = nil
		}
	}
	if context.database != nil {
		context.database.Close()
	}
	if context.downloader != nil {
		context.downloader.Shutdown()
	}
	if context.progress != nil {
		context.progress.Shutdown()
	}
}
Esempio n. 19
0
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
	}
}
Esempio n. 20
0
func waitForSignals(stoppable Stoppable, filename string, stopped <-chan bool) {
	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
outer:
	for {
		sig := <-ch
		log.Info("Received signal: %s", sig.String())
		switch sig {
		case syscall.SIGINT, syscall.SIGTERM:
			runtime.SetCPUProfileRate(0)
			f, err := os.OpenFile(fmt.Sprintf("%s.mem", filename), os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
			if err != nil {
				log.Error("Cannot open memory profile: %s", err)
				break outer
			}
			if err := pprof.WriteHeapProfile(f); err != nil {
				log.Error("Cannot write memory profile: %s", err)
			}
			f.Close()
			stopCHeapProfiler()
			// stopCCpuProfiler()
			stoppable.Stop()
			break outer
			// make sure everything stopped before exiting
		}
	}
	// wait for all logging messages to be printed
	<-stopped
	time.Sleep(5 * time.Second)
	os.Exit(0)
}
Esempio n. 21
0
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())

}
Esempio n. 22
0
// -- main
func main() {
	flag.Parse()

	if *version {
		fmt.Println(simplepush.VERSION)
		return
	}

	runtime.GOMAXPROCS(runtime.NumCPU())
	// Only create profiles if requested. To view the application profiles,
	// see http://blog.golang.org/profiling-go-programs
	if *profile != "" {
		log.Printf("Creating profile...")
		f, err := os.Create(*profile)
		if err != nil {
			log.Fatal(err)
		}
		defer func() {
			log.Printf("Closing profile...")
			pprof.StopCPUProfile()
		}()
		pprof.StartCPUProfile(f)
	}
	if *memProfile != "" {
		defer func() {
			profFile, err := os.Create(*memProfile)
			if err != nil {
				log.Fatalln(err)
			}
			pprof.WriteHeapProfile(profFile)
			profFile.Close()
		}()
	}

	// Load the app from the config file
	app, err := simplepush.LoadApplicationFromFileName(*configFile, *logging)
	if err != nil {
		log.Fatal(err.Error())
	}

	// Report what the app believes the current host to be, and what version.
	log.Printf("CurrentHost: %s, Version: %s", app.Hostname(), simplepush.VERSION)

	// wait for sigint
	sigChan := make(chan os.Signal)
	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGHUP, SIGUSR1)

	// And we're underway!
	errChan := app.Run()

	select {
	case err = <-errChan:
	case <-sigChan:
		app.Logger().Info("main", "Recieved signal, shutting down.", nil)
	}
	app.Stop()
	if err != nil {
		panic("Run: " + err.Error())
	}
}
Esempio n. 23
0
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()
		}
	}

}
Esempio n. 24
0
File: main.go Progetto: nuance/edged
func main() {
	flag.Parse()

	if *cpuProfile != "" {
		f, err := os.Create(*cpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	g, err := Open(*logPath)
	if err != nil {
		panic(err)
	}

	if *freebaseDumpPath != "" {
		if err := g.ReadFreebase(*freebaseDumpPath); err != nil {
			panic(err)
		}
	}

	log.Println("done!")

	if *heapProfile != "" {
		f, err := os.Create(*heapProfile)
		if err != nil {
			log.Fatal(err)
		}
		runtime.GC()
		pprof.WriteHeapProfile(f)
	}
}
Esempio n. 25
0
// HandleError is the actual error handler and will store the error details in analytics if analytics processing is enabled.
func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err string, errCode int) {
	if config.EnableAnalytics {
		t := time.Now()

		// Track the key ID if it exists
		authHeaderValue := context.Get(r, AuthHeaderValue)
		keyName := ""
		if authHeaderValue != nil {
			keyName = authHeaderValue.(string)
		}

		version := e.Spec.getVersionFromRequest(r)
		if version == "" {
			version = "Non Versioned"
		}

		if e.TykMiddleware.Spec.APIDefinition.Proxy.StripListenPath {
			r.URL.Path = strings.Replace(r.URL.Path, e.TykMiddleware.Spec.Proxy.ListenPath, "", 1)
		}

		OauthClientID := ""
		thisSessionState := context.Get(r, SessionData)

		if thisSessionState != nil {
			OauthClientID = thisSessionState.(SessionState).OauthClientID
		}

		thisRecord := AnalyticsRecord{
			r.Method,
			r.URL.Path,
			r.ContentLength,
			r.Header.Get("User-Agent"),
			t.Day(),
			t.Month(),
			t.Year(),
			t.Hour(),
			errCode,
			keyName,
			t,
			version,
			e.Spec.APIDefinition.Name,
			e.Spec.APIDefinition.APIID,
			e.Spec.APIDefinition.OrgID,
			OauthClientID}
		go analytics.RecordHit(thisRecord)
	}

	w.Header().Add("Content-Type", "application/json")
	w.Header().Add("X-Generator", "tyk.io")
	log.Debug("Returning error header")
	w.WriteHeader(errCode)
	thisError := APIError{fmt.Sprintf("%s", err)}
	templates.ExecuteTemplate(w, "error.json", &thisError)
	if doMemoryProfile {
		pprof.WriteHeapProfile(profileFile)
	}

	// Clean up
	context.Clear(r)
}
Esempio n. 26
0
func main() {

	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()
	go Start()
	engine.Terminated()
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			fmt.Errorf("%s\n", err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}
}
Esempio n. 27
0
func main() {
	f, err := os.Create("cpuprofile")
	if err != nil {
		panic(err.Error())
	}
	f2, err := os.Create("memprofile")
	if err != nil {
		panic(err.Error())
	}

	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()
	defer pprof.WriteHeapProfile(f2)

	m := radix.NewTree()
	var k []byte
	for i := 0; i < 100000; i++ {
		k = murmur.HashString(fmt.Sprint(i))
		m.Put(k, k, 1)
		x, _, _ := m.Get(k)
		if bytes.Compare(x, k) != 0 {
			panic("humbug")
		}
	}
}
Esempio n. 28
0
func main() {
	f, err := os.Create("cpuprofile")
	if err != nil {
		panic(err.Error())
	}
	f2, err := os.Create("memprofile")
	if err != nil {
		panic(err.Error())
	}

	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()
	defer pprof.WriteHeapProfile(f2)

	benchNode := NewNodeDir("127.0.0.1:1231", "127.0.0.1:1231", "")
	benchNode.MustStart()
	var k []byte
	for i := 0; i < 100000; i++ {
		k = murmur.HashString(fmt.Sprint(i))
		benchNode.Put(common.Item{
			Key:   k,
			Value: k,
		})
	}
}
Esempio n. 29
0
// Stop profiling and output results to a file. Also dump a memory profile.
func (pro *Prof) Stop() error {
	pprof.StopCPUProfile()
	pro.fd.Close()

	profileDir := "Profile-" + time.Now().Format("Jan_02_2006-15.04.05")
	if err := os.Mkdir(profileDir, 0777); err != nil {
		return err
	}

	cpuProfilePath := filepath.Join(profileDir, pro.fname)
	if err := os.Rename(pro.fname+".tmp", cpuProfilePath); err != nil {
		return fmt.Errorf("failed to rename tmp file: %s", err)
	}

	memProfilePath := filepath.Join(profileDir, pro.fname+".mem")
	memFile, err := os.Create(memProfilePath)
	if err != nil {
		return fmt.Errorf("failed to create mem file: %s", err)
	}
	defer memFile.Close()

	runtime.GC()
	if err := pprof.WriteHeapProfile(memFile); err != nil {
		return fmt.Errorf("failed to write heap profile: %s", err)
	}
	return nil
}
Esempio n. 30
0
// after runs after all testing.
func after() {
	if *cpuProfile != "" {
		pprof.StopCPUProfile() // flushes profile to disk
	}
	if *memProfile != "" {
		f, err := os.Create(toOutputDir(*memProfile))
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
			os.Exit(2)
		}
		if err = pprof.WriteHeapProfile(f); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *memProfile, err)
			os.Exit(2)
		}
		f.Close()
	}
	if *blockProfile != "" && *blockProfileRate >= 0 {
		f, err := os.Create(toOutputDir(*blockProfile))
		if err != nil {
			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
			os.Exit(2)
		}
		if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *blockProfile, err)
			os.Exit(2)
		}
		f.Close()
	}
	if cover.Mode != "" {
		coverReport()
	}
}