Commit 2bfd6d7e authored by Jonathan Lange's avatar Jonathan Lange
Browse files

Parametrize compression level

parent e4c75e62
Showing with 34 additions and 5 deletions
+34 -5
......@@ -2,6 +2,7 @@ package multitenant
import (
"bytes"
"compress/gzip"
"crypto/md5"
"fmt"
"io"
......@@ -346,7 +347,7 @@ func (c *awsCollector) Add(ctx context.Context, rep report.Report) error {
// first, encode the report into a buffer and record its size
var buf bytes.Buffer
rep.WriteBinary(&buf)
rep.WriteBinary(&buf, gzip.BestCompression)
reportSizeHistogram.Observe(float64(buf.Len()))
// second, put the report on s3
......
......@@ -2,6 +2,7 @@ package appclient
import (
"bytes"
"compress/gzip"
"github.com/weaveworks/scope/report"
)
......@@ -28,6 +29,6 @@ func (p *ReportPublisher) Publish(r report.Report) error {
})
}
buf := &bytes.Buffer{}
r.WriteBinary(buf)
r.WriteBinary(buf, gzip.BestCompression)
return p.publisher.Publish(buf)
}
......@@ -9,8 +9,8 @@ import (
)
// WriteBinary writes a Report as a gzipped msgpack.
func (rep Report) WriteBinary(w io.Writer) error {
gzwriter, err := gzip.NewWriterLevel(w, gzip.BestCompression)
func (rep Report) WriteBinary(w io.Writer, compressionLevel int) error {
gzwriter, err := gzip.NewWriterLevel(w, compressionLevel)
if err != nil {
return err
}
......
......@@ -2,6 +2,7 @@ package report_test
import (
"bytes"
"compress/gzip"
"reflect"
"testing"
......@@ -11,7 +12,7 @@ import (
func TestRoundtrip(t *testing.T) {
var buf bytes.Buffer
r1 := report.MakeReport()
r1.WriteBinary(&buf)
r1.WriteBinary(&buf, gzip.BestCompression)
r2, err := report.MakeFromBinary(&buf)
if err != nil {
t.Error(err)
......@@ -20,3 +21,29 @@ func TestRoundtrip(t *testing.T) {
t.Errorf("%v != %v", r1, *r2)
}
}
func TestRoundtripNoCompression(t *testing.T) {
// Make sure that we can use our standard routines for decompressing
// something with '0' level compression.
var buf bytes.Buffer
r1 := report.MakeReport()
r1.WriteBinary(&buf, 0)
r2, err := report.MakeFromBinary(&buf)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(r1, *r2) {
t.Errorf("%v != %v", r1, *r2)
}
}
func TestMoreCompressionMeansSmaller(t *testing.T) {
// Make sure that 0 level compression actually does compress less.
var buf1, buf2 bytes.Buffer
r := report.MakeReport()
r.WriteBinary(&buf1, gzip.BestCompression)
r.WriteBinary(&buf2, 0)
if buf1.Len() >= buf2.Len() {
t.Errorf("Compression doesn't change size: %v >= %v", buf1.Len(), buf2.Len())
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment