Last active
July 24, 2018 12:06
-
-
Save rhnvrm/7144684607a034597310933fda94d396 to your computer and use it in GitHub Desktop.
Example of Lazy Serialization using msgp (go generate based MessagePack library) enveloping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/tinylib/msgp/msgp" | |
) | |
//go:generate msgp | |
type Foo struct { | |
A string `msg:"a"` | |
Another string `msg:"b"` | |
} | |
type Data struct { | |
Header uint16 `msg:"header"` | |
Body msgp.Raw `msg:"body"` | |
} | |
func main() { | |
foo1 := Foo{A: "Hello", Another: "World"} | |
body, _ := foo1.MarshalMsg(nil) | |
fmt.Printf("foo1 is encoded as %x\n", body) | |
data := Data{ | |
Header: 1, | |
Body: body, | |
} | |
dataPacket, _ := data.MarshalMsg(nil) | |
var ( | |
readPacket Data | |
readFoo Foo | |
) | |
_, _ = readPacket.UnmarshalMsg(dataPacket) | |
if readPacket.Header == 1 { | |
_, _ = readFoo.UnmarshalMsg([]byte(readPacket.Body)) | |
fmt.Println(readFoo) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"time" | |
"zerodha.tech/kite-ticker/snaps" | |
) | |
var foo1 = snaps.Snap{ | |
InstrumentToken: 123, | |
LastPrice: 123.434, | |
Exchange: "NSE", | |
TradingSymbol: "NIX", | |
LastTradedQuantity: 1220, | |
AverageTradePrice: 12312.32, | |
VolumeTraded: 1231, | |
TotalBuyQuantity: 2232, | |
TotalSellQuantity: 3323, | |
OHLC: snaps.OHLC{ | |
Open: 1231.22, | |
High: 1231.22, | |
Low: 1231.22, | |
Close: 1231.22, | |
}, | |
LastTradeTime: snaps.Time{time.Now()}, | |
OI: 123, | |
OIDayHigh: 321, | |
OIDayLow: 131, | |
Timestamp: snaps.Time{time.Now()}, | |
NetChange: 1231.23, | |
} | |
func BenchmarkWithLazy(b *testing.B) { | |
b.ReportAllocs() | |
// run the Fib function b.N times | |
for n := 0; n < b.N; n++ { | |
body, _ := foo1.MarshalMsg(nil) | |
// fmt.Printf("foo1 is encoded as %x\n", body) | |
data := Data{ | |
Header: 1, | |
Body: body, | |
} | |
dataPacket, _ := data.MarshalMsg(nil) | |
var ( | |
readPacket Data | |
) | |
_, _ = readPacket.UnmarshalMsg(dataPacket) | |
if readPacket.Header == 1 { | |
var readSnap snaps.Snap | |
readSnap.UnmarshalMsg([]byte(readPacket.Body)) | |
} | |
} | |
} | |
// func BenchmarkWithLazyButHeaderMismatch(b *testing.B) { | |
// // run the Fib function b.N times | |
// for n := 0; n < b.N; n++ { | |
// body, _ := foo1.MarshalMsg(nil) | |
// // fmt.Printf("foo1 is encoded as %x\n", body) | |
// data := Data{ | |
// Header: 0, | |
// Body: body, | |
// } | |
// dataPacket, _ := data.MarshalMsg(nil) | |
// var ( | |
// readPacket Data | |
// readFoo snaps.Snap | |
// ) | |
// _, _ = readPacket.UnmarshalMsg(dataPacket) | |
// if readPacket.Header == 1 { | |
// _, _ = readFoo.UnmarshalMsg([]byte(readPacket.Body)) | |
// // fmt.Println(readFoo) | |
// } | |
// } | |
// } | |
func BenchmarkWithoutLazy(b *testing.B) { | |
b.ReportAllocs() | |
// run the Fib function b.N times | |
for n := 0; n < b.N; n++ { | |
body, _ := foo1.MarshalMsg(nil) | |
payload := make([]byte, len(body)+1) | |
payload[0] = 1 | |
copy(payload[1:], body) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment