Skip to content

Commit

Permalink
Merge pull request #2386 from weaveworks/decode-bytes
Browse files Browse the repository at this point in the history
Decode reports from a byte buffer
  • Loading branch information
bboreham authored Nov 30, 2017
2 parents d5eab89 + 05d5d33 commit d6e47c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
40 changes: 40 additions & 0 deletions report/benchmark_internal_test.go
Original file line number Diff line number Diff line change
@@ -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
})
}
25 changes: 7 additions & 18 deletions report/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit d6e47c0

Please sign in to comment.