Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lorawan bench code
package lorawan
import (
"bytes"
"testing"
)
func BenchmarkDecode(b *testing.B) {
data := []byte{0x40, 0x04, 0x03, 0x02, 0x01, 0x80, 0x01, 0x00, 0x01, 0xa6, 0x94, 0x64, 0x26, 0x15, 0xd6, 0xc3, 0xb5, 0x82}
b.ResetTimer()
for n := 0; n < b.N*1000; n++ {
phy := PHYPayload{}
err := phy.UnmarshalBinary(data)
if err != nil {
b.FailNow()
}
macPL, ok := phy.MACPayload.(*MACPayload)
if !ok {
b.FailNow()
}
if macPL.FHDR.FCnt != 1 {
b.FailNow()
}
if len(macPL.FHDR.FOpts) != 0 {
b.FailNow()
}
}
}
func BenchmarkValidateMic(b *testing.B) {
nwkSKey := AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
data := []byte{0x40, 0x04, 0x03, 0x02, 0x01, 0x80, 0x01, 0x00, 0x01, 0xa6, 0x94, 0x64, 0x26, 0x15, 0xd6, 0xc3, 0xb5, 0x82}
b.ResetTimer()
for n := 0; n < b.N*1000; n++ {
phy := PHYPayload{}
err := phy.UnmarshalBinary(data)
if err != nil {
b.FailNow()
}
ok, err := phy.ValidateUplinkDataMIC(LoRaWAN1_0, 1, 0, 0, nwkSKey, AES128Key{})
if err != nil || !ok {
b.FailNow()
}
}
}
func BenchmarkDecrypt(b *testing.B) {
appSKey := AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
data := []byte{0x40, 0x04, 0x03, 0x02, 0x01, 0x80, 0x01, 0x00, 0x01, 0xa6, 0x94, 0x64, 0x26, 0x15, 0xd6, 0xc3, 0xb5, 0x82}
b.ResetTimer()
for n := 0; n < b.N*1000; n++ {
phy := PHYPayload{}
err := phy.UnmarshalBinary(data)
if err != nil {
b.FailNow()
}
err = phy.DecryptFRMPayload(appSKey)
if err != nil {
b.FailNow()
}
macPL, ok := phy.MACPayload.(*MACPayload)
if !ok {
b.FailNow()
}
pl, ok := macPL.FRMPayload[0].(*DataPayload)
if !ok {
b.FailNow()
}
if !bytes.Equal(pl.Bytes, []byte("hello")) {
b.FailNow()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.