mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-04 10:41:14 +00:00
We want the middle ground between a small compression size, a fast compression time and a fast decompression time. Tests suggest that the default compression level is better than the maximum compression level: although the reports are 4% bigger and decompress slower, they compress 33% faster. See discussion on https://github.com/weaveworks/scope/issues/1457#issuecomment-293288682
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package report_test
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/weaveworks/scope/report"
|
|
)
|
|
|
|
func TestRoundtrip(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
r1 := report.MakeReport()
|
|
r1.WriteBinary(&buf, gzip.DefaultCompression)
|
|
bytes := append([]byte{}, buf.Bytes()...) // copy the contents for later
|
|
r2, err := report.MakeFromBinary(&buf)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !reflect.DeepEqual(r1, *r2) {
|
|
t.Errorf("%v != %v", r1, *r2)
|
|
}
|
|
r3, err := report.MakeFromBytes(bytes)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !reflect.DeepEqual(r1, *r3) {
|
|
t.Errorf("%v != %v", r1, *r3)
|
|
}
|
|
}
|
|
|
|
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.DefaultCompression)
|
|
r.WriteBinary(&buf2, 0)
|
|
if buf1.Len() >= buf2.Len() {
|
|
t.Errorf("Compression doesn't change size: %v >= %v", buf1.Len(), buf2.Len())
|
|
}
|
|
}
|