Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Zstandard compressed input and output files (.zst) #1228

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions input_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"expvar"
"fmt"
"github.com/klauspost/compress/zstd"
"io"
"math"
"os"
Expand Down Expand Up @@ -187,6 +188,13 @@ func newFileInputReader(path string, readDepth int, dryRun bool) *fileInputReade
return nil
}
r.reader = bufio.NewReader(gzReader)
} else if strings.HasSuffix(path, ".zst") {
zstdReader, err := zstd.NewReader(file)
if err != nil {
Debug(0, fmt.Sprintf("[INPUT-FILE] err: %q", err))
return nil
}
r.reader = bufio.NewReader(zstdReader)
} else {
r.reader = bufio.NewReader(file)
}
Expand Down
61 changes: 47 additions & 14 deletions input_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,23 @@ func TestInputFileWithGETAndPOST(t *testing.T) {

func TestInputFileMultipleFilesWithRequestsOnly(t *testing.T) {
rnd := rand.Int63()
tmpDir := t.TempDir()

file1, _ := os.OpenFile(fmt.Sprintf("/tmp/%d_0", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file1, _ := os.OpenFile(fmt.Sprintf(tmpDir + "/%d_0", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file1.Write([]byte("1 1 1\ntest1"))
file1.Write([]byte(payloadSeparator))
file1.Write([]byte("1 1 3\ntest2"))
file1.Write([]byte(payloadSeparator))
file1.Close()

file2, _ := os.OpenFile(fmt.Sprintf("/tmp/%d_1", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file2, _ := os.OpenFile(fmt.Sprintf(tmpDir + "/%d_1", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file2.Write([]byte("1 1 2\ntest3"))
file2.Write([]byte(payloadSeparator))
file2.Write([]byte("1 1 4\ntest4"))
file2.Write([]byte(payloadSeparator))
file2.Close()

input := NewFileInput(fmt.Sprintf("/tmp/%d*", rnd), false, 100, 0, false)
input := NewFileInput(fmt.Sprintf(tmpDir + "/%d*", rnd), false, 100, 0, false)

for i := '1'; i <= '4'; i++ {
msg, _ := input.PluginRead()
Expand All @@ -119,8 +120,9 @@ func TestInputFileMultipleFilesWithRequestsOnly(t *testing.T) {

func TestInputFileRequestsWithLatency(t *testing.T) {
rnd := rand.Int63()
tmpDir := t.TempDir()

file, _ := os.OpenFile(fmt.Sprintf("/tmp/%d", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file, _ := os.OpenFile(fmt.Sprintf(tmpDir + "/%d", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
defer file.Close()

file.Write([]byte("1 1 100000000\nrequest1"))
Expand All @@ -130,7 +132,7 @@ func TestInputFileRequestsWithLatency(t *testing.T) {
file.Write([]byte("1 3 250000000\nrequest3"))
file.Write([]byte(payloadSeparator))

input := NewFileInput(fmt.Sprintf("/tmp/%d", rnd), false, 100, 0, false)
input := NewFileInput(fmt.Sprintf(tmpDir + "/%d", rnd), false, 100, 0, false)

start := time.Now().UnixNano()
for i := 0; i < 3; i++ {
Expand All @@ -147,8 +149,9 @@ func TestInputFileRequestsWithLatency(t *testing.T) {

func TestInputFileMultipleFilesWithRequestsAndResponses(t *testing.T) {
rnd := rand.Int63()
tmpDir := t.TempDir()

file1, _ := os.OpenFile(fmt.Sprintf("/tmp/%d_0", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file1, _ := os.OpenFile(fmt.Sprintf(tmpDir + "/%d_0", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file1.Write([]byte("1 1 1\nrequest1"))
file1.Write([]byte(payloadSeparator))
file1.Write([]byte("2 1 1\nresponse1"))
Expand All @@ -159,7 +162,7 @@ func TestInputFileMultipleFilesWithRequestsAndResponses(t *testing.T) {
file1.Write([]byte(payloadSeparator))
file1.Close()

file2, _ := os.OpenFile(fmt.Sprintf("/tmp/%d_1", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file2, _ := os.OpenFile(fmt.Sprintf(tmpDir + "/%d_1", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file2.Write([]byte("1 3 2\nrequest3"))
file2.Write([]byte(payloadSeparator))
file2.Write([]byte("2 3 2\nresponse3"))
Expand All @@ -170,7 +173,7 @@ func TestInputFileMultipleFilesWithRequestsAndResponses(t *testing.T) {
file2.Write([]byte(payloadSeparator))
file2.Close()

input := NewFileInput(fmt.Sprintf("/tmp/%d*", rnd), false, 100, 0, false)
input := NewFileInput(fmt.Sprintf(tmpDir + "/%d*", rnd), false, 100, 0, false)

for i := '1'; i <= '4'; i++ {
msg, _ := input.PluginRead()
Expand All @@ -190,15 +193,16 @@ func TestInputFileMultipleFilesWithRequestsAndResponses(t *testing.T) {

func TestInputFileLoop(t *testing.T) {
rnd := rand.Int63()
tmpDir := t.TempDir()

file, _ := os.OpenFile(fmt.Sprintf("/tmp/%d", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file, _ := os.OpenFile(fmt.Sprintf(tmpDir + "/%d", rnd), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
file.Write([]byte("1 1 1\ntest1"))
file.Write([]byte(payloadSeparator))
file.Write([]byte("1 1 2\ntest2"))
file.Write([]byte(payloadSeparator))
file.Close()

input := NewFileInput(fmt.Sprintf("/tmp/%d", rnd), true, 100, 0, false)
input := NewFileInput(fmt.Sprintf(tmpDir + "/%d", rnd), true, 100, 0, false)

// Even if we have just 2 requests in file, it should indifinitly loop
for i := 0; i < 1000; i++ {
Expand All @@ -209,24 +213,52 @@ func TestInputFileLoop(t *testing.T) {
os.Remove(file.Name())
}

func TestInputFileCompressed(t *testing.T) {
func TestInputFileCompressedGzip(t *testing.T) {
rnd := rand.Int63()
tmpDir := t.TempDir()

output := NewFileOutput(fmt.Sprintf("/tmp/%d_0.gz", rnd), &FileOutputConfig{FlushInterval: time.Minute, Append: true})
output := NewFileOutput(fmt.Sprintf(tmpDir + "/%d_0.gz", rnd), &FileOutputConfig{FlushInterval: time.Minute, Append: true})
for i := 0; i < 1000; i++ {
output.PluginWrite(&Message{Meta: []byte("1 1 1\r\n"), Data: []byte("test")})
}
name1 := output.file.Name()
output.Close()

output2 := NewFileOutput(fmt.Sprintf("/tmp/%d_1.gz", rnd), &FileOutputConfig{FlushInterval: time.Minute, Append: true})
output2 := NewFileOutput(fmt.Sprintf(tmpDir + "/%d_1.gz", rnd), &FileOutputConfig{FlushInterval: time.Minute, Append: true})
for i := 0; i < 1000; i++ {
output2.PluginWrite(&Message{Meta: []byte("1 1 1\r\n"), Data: []byte("test")})
}
name2 := output2.file.Name()
output2.Close()

input := NewFileInput(fmt.Sprintf("/tmp/%d*", rnd), false, 100, 0, false)
input := NewFileInput(fmt.Sprintf(tmpDir + "/%d*", rnd), false, 100, 0, false)
for i := 0; i < 2000; i++ {
input.PluginRead()
}

os.Remove(name1)
os.Remove(name2)
}

func TestInputFileCompressedZstd(t *testing.T) {
rnd := rand.Int63()
tmpDir := t.TempDir()

output := NewFileOutput(fmt.Sprintf(tmpDir + "/%d_0.zst", rnd), &FileOutputConfig{FlushInterval: time.Minute, Append: true})
for i := 0; i < 1000; i++ {
output.PluginWrite(&Message{Meta: []byte("1 1 1\r\n"), Data: []byte("test")})
}
name1 := output.file.Name()
output.Close()

output2 := NewFileOutput(fmt.Sprintf(tmpDir + "/%d_1.zst", rnd), &FileOutputConfig{FlushInterval: time.Minute, Append: true})
for i := 0; i < 1000; i++ {
output2.PluginWrite(&Message{Meta: []byte("1 1 1\r\n"), Data: []byte("test")})
}
name2 := output2.file.Name()
output2.Close()

input := NewFileInput(fmt.Sprintf(tmpDir + "/%d*", rnd), false, 100, 0, false)
for i := 0; i < 2000; i++ {
input.PluginRead()
}
Expand Down Expand Up @@ -318,6 +350,7 @@ func CreateCaptureFile(requestGenerator *RequestGenerator) *CaptureFile {

time.Sleep(100 * time.Millisecond)
emitter.Close()
outputFile.flush()

return NewExpectedCaptureFile(readPayloads, f)

Expand Down
10 changes: 10 additions & 0 deletions output_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/buger/goreplay/internal/size"
"github.com/klauspost/compress/zstd"
"io"
"log"
"math/rand"
Expand Down Expand Up @@ -231,6 +232,11 @@ func (o *FileOutput) PluginWrite(msg *Message) (n int, err error) {

if strings.HasSuffix(o.currentName, ".gz") {
o.writer = gzip.NewWriter(o.file)
} else if strings.HasSuffix(o.currentName, ".zst") {
o.writer, err = zstd.NewWriter(o.file)
if err != nil {
log.Fatal(o, "Error opening file %q. Error: %s", o.currentName, err)
}
} else {
o.writer = bufio.NewWriter(o.file)
}
Expand Down Expand Up @@ -274,6 +280,8 @@ func (o *FileOutput) flush() {
if o.file != nil {
if strings.HasSuffix(o.currentName, ".gz") {
o.writer.(*gzip.Writer).Flush()
} else if strings.HasSuffix(o.currentName, ".zst") {
o.writer.(*zstd.Encoder).Flush()
} else {
o.writer.(*bufio.Writer).Flush()
}
Expand All @@ -294,6 +302,8 @@ func (o *FileOutput) closeLocked() error {
if o.file != nil {
if strings.HasSuffix(o.currentName, ".gz") {
o.writer.(*gzip.Writer).Close()
} else if strings.HasSuffix(o.currentName, ".zst") {
o.writer.(*zstd.Encoder).Close()
} else {
o.writer.(*bufio.Writer).Flush()
}
Expand Down
Loading