move main report processing benchmarks in one place

so they can share code and are easier to run in combination.

We take advantage of the code sharing by generalising the report
rendering benchmarks to read & merge reports from a dir.
This commit is contained in:
Matthias Radestock
2017-12-02 11:29:16 +00:00
parent 2f4c6507e5
commit c953313b01
2 changed files with 41 additions and 44 deletions

View File

@@ -4,6 +4,8 @@ import (
"flag"
"net/http"
"net/url"
"os"
"path/filepath"
"testing"
"github.com/weaveworks/scope/render"
@@ -12,9 +14,43 @@ import (
)
var (
benchReportFile = flag.String("bench-report-file", "", "report file to use for benchmarking (relative to this package)")
benchReportPath = flag.String("bench-report-path", "", "report file, or dir with files, to use for benchmarking (relative to this package)")
)
func readReportFiles(path string) ([]report.Report, error) {
reports := []report.Report{}
if err := filepath.Walk(path,
func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
rpt, err := report.MakeFromFile(p)
if err != nil {
return err
}
reports = append(reports, rpt)
return nil
}); err != nil {
return nil, err
}
return reports, nil
}
func BenchmarkReportUnmarshal(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
if _, err := readReportFiles(*benchReportPath); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkTopologyList(b *testing.B) {
benchmarkRender(b, func(report report.Report) {
request := &http.Request{
@@ -26,11 +62,12 @@ func BenchmarkTopologyList(b *testing.B) {
func benchmarkRender(b *testing.B, f func(report.Report)) {
r := fixture.Report
if *benchReportFile != "" {
var err error
if r, err = report.MakeFromFile(*benchReportFile); err != nil {
if *benchReportPath != "" {
reports, err := readReportFiles(*benchReportPath)
if err != nil {
b.Fatal(err)
}
r = NewSmartMerger().Merge(reports)
}
b.ReportAllocs()

View File

@@ -1,40 +0,0 @@
package report
import (
"flag"
"os"
"path/filepath"
"testing"
)
var (
benchReportPath = flag.String("bench-report-path", "", "report file, or dir with files, to use for benchmarking (relative to this package)")
)
func BenchmarkReportUnmarshal(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
b.StartTimer()
if err := readReportFiles(*benchReportPath); err != nil {
b.Fatal(err)
}
}
}
func readReportFiles(path string) error {
return filepath.Walk(path,
func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if _, err := MakeFromFile(p); err != nil {
return err
}
return nil
})
}