From 4fd6c4ed562f57941da293d75a007057580e43d7 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 19 Jan 2021 19:21:58 +0800 Subject: [PATCH] switch from gzip to zstd for qlog compression --- go.mod | 1 + go.sum | 2 ++ tracer.go | 13 +++++++++---- tracer_test.go | 9 +++++---- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 5dc7713..e4aa1dd 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.14 require ( github.com/golang/mock v1.4.4 github.com/ipfs/go-log v1.0.4 + github.com/klauspost/compress v1.11.7 github.com/libp2p/go-libp2p-core v0.8.0 github.com/libp2p/go-libp2p-tls v0.1.3 github.com/libp2p/go-netroute v0.1.3 diff --git a/go.sum b/go.sum index a472091..eb5b182 100644 --- a/go.sum +++ b/go.sum @@ -115,6 +115,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= +github.com/klauspost/compress v1.11.7 h1:0hzRabrMN4tSTvMfnL3SCv1ZGeAP23ynzodBgaHeMeg= +github.com/klauspost/compress v1.11.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= diff --git a/tracer.go b/tracer.go index f88cbce..f8e1dbb 100644 --- a/tracer.go +++ b/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, diff --git a/tracer_test.go b/tracer_test.go index 753a6d3..5035f88 100644 --- a/tracer_test.go +++ b/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())