Skip to content

Commit

Permalink
Merge pull request #190 from libp2p/zstd
Browse files Browse the repository at this point in the history
switch from gzip to zstd for qlog compression
  • Loading branch information
marten-seemann committed Jan 21, 2021
2 parents 3d1dc75 + 89dffad commit 8eb8796
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 9 additions & 4 deletions p2p/transport/quic/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package libp2pquic

import (
"bufio"
"compress/gzip"
"fmt"
"io"
"os"
"time"

"github.com/klauspost/compress/zstd"

"github.com/lucas-clemente/quic-go/logging"
"github.com/lucas-clemente/quic-go/metrics"
"github.com/lucas-clemente/quic-go/qlog"
Expand Down Expand Up @@ -48,14 +49,18 @@ func newQlogger(qlogDir string, role logging.Perspective, connID []byte) io.Writ
if role == logging.PerspectiveClient {
r = "client"
}
finalFilename := fmt.Sprintf("%s%clog_%s_%s_%x.qlog.gz", qlogDir, os.PathSeparator, t, r, connID)
filename := fmt.Sprintf("%s%c.log_%s_%s_%x.qlog.gz.swp", qlogDir, os.PathSeparator, t, r, connID)
finalFilename := fmt.Sprintf("%s%clog_%s_%s_%x.qlog.zst", qlogDir, os.PathSeparator, t, r, connID)
filename := fmt.Sprintf("%s%c.log_%s_%s_%x.qlog.zst.swp", qlogDir, os.PathSeparator, t, r, connID)
f, err := os.Create(filename)
if err != nil {
log.Errorf("unable to create qlog file %s: %s", filename, err)
return nil
}
gz := gzip.NewWriter(f)
gz, err := zstd.NewWriter(f, zstd.WithEncoderLevel(zstd.SpeedFastest))
if err != nil {
log.Errorf("failed to initialize zstd: %s", err)
return nil
}
return &qlogger{
f: f,
filename: finalFilename,
Expand Down
9 changes: 5 additions & 4 deletions p2p/transport/quic/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package libp2pquic

import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"os"

"github.com/klauspost/compress/zstd"

"github.com/lucas-clemente/quic-go/logging"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -43,12 +44,12 @@ var _ = Describe("qlogger", func() {
logger := newQlogger(qlogDir, logging.PerspectiveServer, []byte{0xde, 0xad, 0xbe, 0xef})
file := getFile()
Expect(string(file.Name()[0])).To(Equal("."))
Expect(file.Name()).To(HaveSuffix(".qlog.gz.swp"))
Expect(file.Name()).To(HaveSuffix(".qlog.zst.swp"))
// close the logger. This should move the file.
Expect(logger.Close()).To(Succeed())
file = getFile()
Expect(string(file.Name()[0])).ToNot(Equal("."))
Expect(file.Name()).To(HaveSuffix(".qlog.gz"))
Expect(file.Name()).To(HaveSuffix(".qlog.zst"))
Expect(file.Name()).To(And(
ContainSubstring("server"),
ContainSubstring("deadbeef"),
Expand Down Expand Up @@ -76,7 +77,7 @@ var _ = Describe("qlogger", func() {
compressed, err := ioutil.ReadFile(qlogDir + "/" + getFile().Name())
Expect(err).ToNot(HaveOccurred())
Expect(compressed).ToNot(Equal("foobar"))
gz, err := gzip.NewReader(bytes.NewReader(compressed))
gz, err := zstd.NewReader(bytes.NewReader(compressed))
Expect(err).ToNot(HaveOccurred())
data, err := ioutil.ReadAll(gz)
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 8eb8796

Please sign in to comment.