如何在压缩之前和之后测量字节大小和时间

I want to gzip a string (it is actually a JSON response)

var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte("YourDataHere")); err != nil {
    panic(err)
}

How can I easily output the size of bytes before and after compression and more importantly how can I time it takes to compress and decompress back to a string?

You can calculate the size as per Nipun Talukdar's comment.

len([]byte("YourDataHere")) b.Len()

And use time.Now() and time.Since() to get the time taken.

var b bytes.Buffer
input := []byte("YourDataHere")

fmt.Println("Input size : ", len(input))

gz := gzip.NewWriter(&b)


start := time.Now() 

gz.Write(input)
if _, err := gz.Flush(); err != nil {
        panic(err)
}


totalTime := time.Since(start) 

fmt.Println("Compressed size : ", b.Len(), "
Time taken : ", totalTime)
gz.Close()

Same method can be applied with unzipping. You can also create a support function that can do the timing.

func timer(startTime time.Time) {

totalTime := time.Since(startTime)
log.Println("Time taken : ",totalTime)

}

Usage : defer timer(time.Now()) at the start of the function.

There are examples of how to do this in Go here

https://golang.org/test/bench/go1/gzip_test.go

Thankfully it's BSD licensed...

// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This benchmark tests gzip and gunzip performance.

package go1 

import (
  "bytes"
  gz "compress/gzip"
  "io"
  "io/ioutil"
  "testing"
)

var (
  jsongunz = bytes.Repeat(jsonbytes, 10) 
  jsongz   []byte
)

func init() {
  var buf bytes.Buffer
  c := gz.NewWriter(&buf)
  c.Write(jsongunz)
  c.Close()
  jsongz = buf.Bytes()
}

func gzip() {
  c := gz.NewWriter(ioutil.Discard)
  if _, err := c.Write(jsongunz); err != nil {
    panic(err)
  }
  if err := c.Close(); err != nil {
    panic(err)
  }
}

func gunzip() {
  r, err := gz.NewReader(bytes.NewBuffer(jsongz))
  if err != nil {
    panic(err)
  }
  if _, err := io.Copy(ioutil.Discard, r); err != nil {
    panic(err)
  }
  r.Close()
}

func BenchmarkGzip(b *testing.B) {
  b.SetBytes(int64(len(jsongunz)))
  for i := 0; i < b.N; i++ {
    gzip()
  }
}

func BenchmarkGunzip(b *testing.B) {
  b.SetBytes(int64(len(jsongunz)))
  for i := 0; i < b.N; i++ {
    gunzip()
  }
}