Merge pull request #2386 from weaveworks/decode-bytes

Decode reports from a byte buffer
This commit is contained in:
Bryan Boreham
2017-11-29 18:41:24 -08:00
committed by GitHub
2 changed files with 47 additions and 18 deletions

View File

@@ -0,0 +1,40 @@
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
})
}

View File

@@ -70,10 +70,13 @@ func (rep *Report) ReadBinary(r io.Reader, gzipped bool, codecHandle codec.Handl
return err
}
}
if log.GetLevel() == log.DebugLevel {
r = byteCounter{next: r, count: &uncompressedSize}
// Read everything into memory before decoding: it's faster
buf, err := ioutil.ReadAll(r)
if err != nil {
return err
}
if err := codec.NewDecoder(r, codecHandle).Decode(&rep); err != nil {
uncompressedSize = uint64(len(buf))
if err := rep.ReadBytes(buf, codecHandle); err != nil {
return err
}
log.Debugf(
@@ -139,21 +142,7 @@ func MakeFromFile(path string) (rpt Report, _ error) {
return rpt, err
}
var buf []byte
if gzipped {
r, err := gzip.NewReader(f)
if err != nil {
return rpt, err
}
buf, err = ioutil.ReadAll(r)
} else {
buf, err = ioutil.ReadAll(f)
}
if err != nil {
return rpt, err
}
err = rpt.ReadBytes(buf, handle)
err = rpt.ReadBinary(f, gzipped, handle)
return rpt, err
}