func compareGraphs(t *testing.T, got, want []byte) { if string(got) != string(want) { d, err := proftest.Diff(got, want) if err != nil { t.Fatalf("error finding diff: %v", err) } t.Errorf("Compose incorrectly wrote %s", string(d)) } }
func TestSource(t *testing.T) { const path = "testdata/" sampleValue1 := func(v []int64) int64 { return v[1] } for _, tc := range []testcase{ { rpt: New( testProfile.Copy(), &Options{ OutputFormat: List, Symbol: regexp.MustCompile(`.`), Title: filepath.Base(testProfile.Mapping[0].File), SampleValue: sampleValue1, SampleUnit: testProfile.SampleType[1].Unit, }, ), want: path + "source.rpt", }, { rpt: New( testProfile.Copy(), &Options{ OutputFormat: Dot, CallTree: true, Symbol: regexp.MustCompile(`.`), Title: filepath.Base(testProfile.Mapping[0].File), SampleValue: sampleValue1, SampleUnit: testProfile.SampleType[1].Unit, }, ), want: path + "source.dot", }, } { b := bytes.NewBuffer(nil) if err := Generate(b, tc.rpt, &binutils.Binutils{}); err != nil { t.Fatalf("%s: %v", tc.want, err) } gold, err := ioutil.ReadFile(tc.want) if err != nil { t.Fatalf("%s: %v", tc.want, err) } if string(b.String()) != string(gold) { d, err := proftest.Diff(gold, b.Bytes()) if err != nil { t.Fatalf("%s: %v", "source", err) } t.Error("source" + "\n" + string(d) + "\n" + "gold:\n" + tc.want) } } }
func TestMarshalUnmarshal(t *testing.T) { // Write the profile, parse it, and ensure they're equal. buf := bytes.NewBuffer(nil) all.Write(buf) all2, err := Parse(buf) if err != nil { t.Fatal(err) } js1 := proftest.EncodeJSON(&all) js2 := proftest.EncodeJSON(&all2) if string(js1) != string(js2) { t.Errorf("profiles differ") d, err := proftest.Diff(js1, js2) if err != nil { t.Fatal(err) } t.Error("\n" + string(d)) } }
func TestParse(t *testing.T) { const path = "testdata/" for _, source := range []string{ "go.crc32.cpu", "go.godoc.thread", "gobench.cpu", "gobench.heap", "cppbench.cpu", "cppbench.heap", "cppbench.contention", "cppbench.growth", "cppbench.thread", "cppbench.thread.all", "cppbench.thread.none", "java.cpu", "java.heap", "java.contention", } { inbytes, err := ioutil.ReadFile(filepath.Join(path, source)) if err != nil { t.Fatal(err) } p, err := Parse(bytes.NewBuffer(inbytes)) if err != nil { t.Fatalf("%s: %s", source, err) } js := p.String() goldFilename := path + source + ".string" gold, err := ioutil.ReadFile(goldFilename) if err != nil { t.Fatalf("%s: %v", source, err) } if js != string(gold) { t.Errorf("diff %s %s", source, goldFilename) d, err := proftest.Diff(gold, []byte(js)) if err != nil { t.Fatalf("%s: %v", source, err) } t.Error(source + "\n" + string(d) + "\n" + "new profile at:\n" + leaveTempfile([]byte(js))) } // Reencode and decode. bw := bytes.NewBuffer(nil) if err := p.Write(bw); err != nil { t.Fatalf("%s: %v", source, err) } if p, err = Parse(bw); err != nil { t.Fatalf("%s: %v", source, err) } js2 := p.String() if js2 != string(gold) { d, err := proftest.Diff(gold, []byte(js2)) if err != nil { t.Fatalf("%s: %v", source, err) } t.Error(source + "\n" + string(d) + "\n" + "gold:\n" + goldFilename + "\nnew profile at:\n" + leaveTempfile([]byte(js))) } } }