Пример #1
0
func BenchmarkCpuProfile(b *testing.B) {
	f, err := os.Open("testdata/cpu.prof")
	if err != nil {
		b.Fatal(err)
	}
	defer f.Close()
	p, err := parser.NewCpuProfParser(f)
	if err != nil {
		b.Fatal(err)
	}
	bsize := int64(0)
	for i := 0; i < b.N; i++ {
		trace, count, err := p.ReadTrace()
		if trace == nil && err == io.EOF {
			// rewind.
			bsize += tell(f)
			f.Seek(0, 0)
			p, _ = parser.NewCpuProfParser(f)
			continue
		}
		_ = count
	}
	bsize += tell(f)
	b.SetBytes(bsize / int64(b.N))
}
Пример #2
0
func TestCpuProfileReport(t *testing.T) {
	syms := readSymbols("testdata/cpu.prof.symbols")
	resolve := func(u uint64) string { s, _ := lookup(u, syms); return s }

	f, err := os.Open("testdata/cpu.prof")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	p, err := parser.NewCpuProfParser(f)
	if err != nil {
		t.Fatal(err)
	}
	reporter := &Reporter{Resolver: resolve}
	for {
		trace, count, err := p.ReadTrace()
		if trace == nil && err == io.EOF {
			break
		}
		reporter.Add(trace, int64(count))
	}
	entries := reporter.ReportByFunc(ColCPU)
	buf := new(bytes.Buffer)
	w := tabwriter.NewWriter(buf, 0, 8, 2, ' ', 0)
	fmt.Fprintf(w, "\nSelf\tCumul\tName\n")
	for _, e := range entries {
		fmt.Fprintf(w, "%.3g\t%.3g\t%s\n", e.Self[ColCPU], e.Cumul[ColCPU], e.Name)
	}
	w.Flush()
	t.Log(buf.String())
}
Пример #3
0
func TestCpuProfile(t *testing.T) {
	f, err := os.Open("testdata/cpu.prof")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	p, err := parser.NewCpuProfParser(f)
	if err != nil {
		t.Fatal(err)
	}

	syms := readSymbols("testdata/cpu.prof.symbols")
	printed := 0
	for {
		trace, count, err := p.ReadTrace()
		if trace == nil && err == io.EOF {
			break
		}
		s := stringify(trace, syms)
		if printed < 10 {
			printed++
			t.Logf("%d× %v", count, s)
		}
	}
}
Пример #4
0
func TestCpuProfileGraph(t *testing.T) {
	syms := readSymbols("testdata/cpu.prof.symbols")
	resolve := func(u uint64) string { s, _ := lookup(u, syms); return s }

	f, err := os.Open("testdata/cpu.prof")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	p, err := parser.NewCpuProfParser(f)
	if err != nil {
		t.Fatal(err)
	}
	reporter := &Reporter{Resolver: resolve}
	total := int64(0)
	for {
		trace, count, err := p.ReadTrace()
		if trace == nil && err == io.EOF {
			break
		}
		reporter.Add(trace, int64(count))
		total += int64(count)
	}

	g := reporter.GraphByFunc(ColCPU)
	t.Logf("%#v", g)
	report := GraphReport{
		Prog:  "pprof.test",
		Total: total,
		Unit:  "samples",
		Graph: g,
	}

	buf := new(bytes.Buffer)
	err = graphvizTpl.Execute(buf, report)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(buf.String())
}
Пример #5
0
func LoadProfile(r *report.Reporter, f io.ReadCloser) {
	defer f.Close()
	buf := bufio.NewReader(f)
	magic, _ := buf.Peek(4)
	switch string(magic) {
	case "heap":
		// Heap profile.
		p, err := parser.NewHeapProfParser(buf)
		if err != nil {
			log.Fatal(err)
		}
		for {
			rec, err := p.ReadRecord()
			if err == io.EOF {
				break
			}
			p.AdjustRecord(&rec,
				func(uint64) string { return "" })
			r.Add(rec.Trace,
				rec.LiveObj, rec.LiveBytes,
				rec.AllocObj, rec.AllocBytes)
		}
	default:
		// CPU Profile.
		p, err := parser.NewCpuProfParser(buf)
		if err != nil {
			log.Fatal(err)
		}
		for {
			trace, count, err := p.ReadTrace()
			if trace == nil && err == io.EOF {
				break
			}
			r.Add(trace, int64(count))
		}
	}
}
		cpuProfilePath = tmpDir + "/cpu.pprof"
		memProfilePath = tmpDir + "/mem.pprof"
	})

	Describe("cpu profiling", func() {
		It("profiles cpu usage", func() {
			profiler := profiler.New(cpuProfilePath, "", 1*time.Millisecond, logger)
			profiler.Profile()

			profiler.Stop()

			f, err := os.Open(cpuProfilePath)
			Expect(err).NotTo(HaveOccurred())

			_, err = parser.NewCpuProfParser(f)
			Expect(err).NotTo(HaveOccurred())

			memProfile := profiler.GetMemProfileHandle()
			Expect(memProfile).To(BeNil())
		})

		It("panics when there is an error", func() {
			filePath := "/tmp/asdf/asdf.pprof"
			profiler := profiler.New(filePath, "", 1*time.Millisecond, logger)
			Expect(profiler.Profile).To(Panic())
		})
	})

	Describe("memory profiling", func() {
		It("profiles memory usage", func() {