Browse Source

switch from gzip to zstd for qlog compression

pull/1424/head
Marten Seemann 4 years ago
parent
commit
89dffadb02
  1. 13
      p2p/transport/quic/tracer.go
  2. 9
      p2p/transport/quic/tracer_test.go

13
p2p/transport/quic/tracer.go

@ -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"
@ -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,

9
p2p/transport/quic/tracer_test.go

@ -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"
@ -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"),
@ -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())

Loading…
Cancel
Save