Skip to content

Instantly share code, notes, and snippets.

@alf-ytakada
Created December 16, 2021 04:28
Show Gist options
  • Save alf-ytakada/8537fa19294ff33429d23b96fff45bed to your computer and use it in GitHub Desktop.
Save alf-ytakada/8537fa19294ff33429d23b96fff45bed to your computer and use it in GitHub Desktop.
golang snappy encode/decode
$  go test -bench . -benchmem
goos: linux
goarch: amd64
pkg: a
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
Benchmark_snappy_encode-8                        8189756               145.9 ns/op           176 B/op          1 allocs/op
Benchmark_snappy_decode-8                       14360095                77.73 ns/op          112 B/op          1 allocs/op
Benchmark_snappy_decode_and_json_unmarshal-8      211792              5511 ns/op            2128 B/op         32 allocs/op
Benchmark_json_unmarshal-8                        234332              4878 ns/op            2016 B/op         31 allocs/op
PASS
ok      a       4.979s
package main
import (
"encoding/json"
"fmt"
"github.com/golang/snappy"
)
type Test struct {
A int `json:"a,omitempty"`
B string `json:"b,omitempty"`
C []string `json:"c,omitempty"`
D map[string]string `json:"d,omitempty"`
E []Inner `json:"e,omitempty"`
}
type Inner struct {
A int `json:"a,omitempty"`
B string `json:"b,omitempty"`
C []string `json:"c,omitempty"`
D map[string]string `json:"d,omitempty"`
}
func main() {
d := createDummyData()
fmt.Printf("%+v\n", d)
jsonBytes, _ := json.Marshal(d)
fmt.Println(string(jsonBytes))
snappyBytes := snappy.Encode(nil, jsonBytes)
fmt.Printf("orig len = %d, snappy len = %d\n", len(jsonBytes), len(snappyBytes))
fmt.Println(jsonBytes)
fmt.Println(snappyBytes)
decoded, _ := snappy.Decode(nil, snappyBytes)
fmt.Println(decoded)
var a []Test
err := json.Unmarshal(decoded, &a)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", a)
}
func createDummyData() []Test {
return []Test{
{
A: 1,
B: "2",
C: []string{"3", "4"},
D: map[string]string{"5": "6", "7": "8"},
E: []Inner{
{
A: 1,
B: "2",
C: []string{"3", "4"},
D: map[string]string{"5": "6", "7": "8"},
},
},
},
}
}
package main
import (
"encoding/json"
"testing"
"github.com/golang/snappy"
)
func Benchmark_snappy_encode(b *testing.B) {
b.StopTimer()
data := createDummyData()
dataBytes, _ := json.Marshal(data)
b.StartTimer()
for i := 0; i < b.N; i++ {
snappy.Encode(nil, dataBytes)
}
}
func Benchmark_snappy_decode(b *testing.B) {
b.StopTimer()
data := createDummyData()
dataBytes, _ := json.Marshal(data)
snappyBytes := snappy.Encode(nil, dataBytes)
b.StartTimer()
for i := 0; i < b.N; i++ {
snappy.Decode(nil, snappyBytes)
}
}
func Benchmark_snappy_decode_and_json_unmarshal(b *testing.B) {
b.StopTimer()
data := createDummyData()
dataBytes, _ := json.Marshal(data)
snappyBytes := snappy.Encode(nil, dataBytes)
b.StartTimer()
for i := 0; i < b.N; i++ {
var a []Test
decodedBytes, _ := snappy.Decode(nil, snappyBytes)
json.Unmarshal(decodedBytes, &a)
}
}
func Benchmark_json_unmarshal(b *testing.B) {
b.StopTimer()
data := createDummyData()
dataBytes, _ := json.Marshal(data)
b.StartTimer()
for i := 0; i < b.N; i++ {
var a []Test
json.Unmarshal(dataBytes, &a)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment