Skip to content

Instantly share code, notes, and snippets.

@redbo
Created April 11, 2015 00:02
Show Gist options
  • Save redbo/9b67691ef9600ed87755 to your computer and use it in GitHub Desktop.
Save redbo/9b67691ef9600ed87755 to your computer and use it in GitHub Desktop.
package common
import (
"bytes"
"encoding/json"
"testing"
"github.com/hydrogen18/stalecucumber"
msgpack "gopkg.in/vmihailenco/msgpack.v2"
)
// typical object metadata
var d = map[string]string{
"Content-Length": "65536", "Content-Type": "application/octet-stream", "ETag": "fcd6bcb56c1689fcef28b57c22475bad",
"X-Timestamp": "1422766779.57463", "name": "/someaccountname/somecontainername/5821142269423797100",
}
// This is what cPickle makes of the above data structure. It's not a very efficient pickle.
var pythoned = []byte("\x80\x02}q\x01(U\x0bX-TimestampU\x101422766779.57463U\x0eContent-LengthU\x0565536U\x04ETag" +
"U fcd6bcb56c1689fcef28b57c22475badU\x0cContent-TypeU\x18application/octet-streamU\x04nameq\x02" +
"U6/someaccountname/somecontainername/5821142269423797100u.")
func BenchmarkStalecucumber(b *testing.B) {
for i := 0; i < b.N; i++ {
var buf = bytes.Buffer{}
stalecucumber.NewPickler(&buf).Pickle(d)
}
}
func BenchmarkMsgpack(b *testing.B) {
for i := 0; i < b.N; i++ {
msgpack.Marshal(d)
}
}
func BenchmarkJson(b *testing.B) {
for i := 0; i < b.N; i++ {
json.Marshal(d)
}
}
func BenchmarkUnpickle(b *testing.B) {
pickled := PickleDumps(d)
b.ResetTimer()
for i := 0; i < b.N; i++ {
PickleLoads(pickled)
}
}
func BenchmarkUnstalecucumber(b *testing.B) {
var buf = bytes.Buffer{}
stalecucumber.NewPickler(&buf).Pickle(d)
pickled := buf.Bytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
stalecucumber.Unpickle(bytes.NewBuffer(pickled))
}
}
func BenchmarkUnpicklePythoned(b *testing.B) {
for i := 0; i < b.N; i++ {
PickleLoads(pythoned)
}
}
func BenchmarkUnstalecucumberPythoned(b *testing.B) {
for i := 0; i < b.N; i++ {
stalecucumber.Unpickle(bytes.NewBuffer(pythoned))
}
}
func BenchmarkUnmsgpack(b *testing.B) {
var msgpacked, _ = msgpack.Marshal(d)
var out interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
msgpack.Unmarshal(msgpacked, &out)
}
}
func BenchmarkUnjson(b *testing.B) {
var jsoned, _ = json.Marshal(d)
var out interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
json.Unmarshal(jsoned, &out)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment