Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit 5704d31

Browse files
committed
Add a test for compression
Signed-off-by: Chris Marchbanks <[email protected]>
1 parent 96efac4 commit 5704d31

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

testutil/directory.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package testutil
1616
import (
1717
"io/ioutil"
1818
"os"
19+
"path/filepath"
1920
)
2021

2122
const (
@@ -127,3 +128,18 @@ func NewTemporaryDirectory(name string, t T) (handler TemporaryDirectory) {
127128

128129
return
129130
}
131+
132+
// DirSize returns the size in bytes of all files in a directory.
133+
func DirSize(path string) (int64, error) {
134+
var size int64
135+
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
136+
if err != nil {
137+
return err
138+
}
139+
if !info.IsDir() {
140+
size += info.Size()
141+
}
142+
return nil
143+
})
144+
return size, err
145+
}

wal/wal_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,46 @@ func TestSegmentMetric(t *testing.T) {
370370
testutil.Ok(t, w.Close())
371371
}
372372

373+
func TestCompression(t *testing.T) {
374+
boostrap := func(compressed bool) string {
375+
const (
376+
segmentSize = pageSize
377+
recordSize = (pageSize / 2) - recordHeaderSize
378+
records = 100
379+
)
380+
381+
dirPath, err := ioutil.TempDir("", fmt.Sprintf("TestCompression_%t", compressed))
382+
testutil.Ok(t, err)
383+
384+
w, err := NewSize(nil, nil, dirPath, segmentSize, compressed)
385+
testutil.Ok(t, err)
386+
387+
buf := make([]byte, recordSize)
388+
for i := 0; i < records; i++ {
389+
testutil.Ok(t, w.Log(buf))
390+
}
391+
testutil.Ok(t, w.Close())
392+
393+
return dirPath
394+
}
395+
396+
dirCompressed := boostrap(true)
397+
defer func() {
398+
testutil.Ok(t, os.RemoveAll(dirCompressed))
399+
}()
400+
dirUnCompressed := boostrap(false)
401+
defer func() {
402+
testutil.Ok(t, os.RemoveAll(dirUnCompressed))
403+
}()
404+
405+
uncompressedSize, err := testutil.DirSize(dirUnCompressed)
406+
testutil.Ok(t, err)
407+
compressedSize, err := testutil.DirSize(dirCompressed)
408+
testutil.Ok(t, err)
409+
410+
testutil.Assert(t, float64(uncompressedSize)*0.75 > float64(compressedSize), "Compressing zeroes should save at least 25%% space - uncompressedSize: %d, compressedSize: %d", uncompressedSize, compressedSize)
411+
}
412+
373413
func BenchmarkWAL_LogBatched(b *testing.B) {
374414
for _, compress := range []bool{true, false} {
375415
b.Run(fmt.Sprintf("compress=%t", compress), func(b *testing.B) {

0 commit comments

Comments
 (0)