Benchmarks

This commit is contained in:
Mochi
2019-09-22 15:11:02 +01:00
parent f49bde22b0
commit a5c6739778
4 changed files with 63 additions and 21 deletions

View File

@@ -16,7 +16,6 @@ type ConnackPacket struct {
// Encode encodes and writes the packet data values to the buffer. // Encode encodes and writes the packet data values to the buffer.
func (pk *ConnackPacket) Encode(w io.Writer) error { func (pk *ConnackPacket) Encode(w io.Writer) error {
var body bytes.Buffer var body bytes.Buffer
// Write flags to packet body. // Write flags to packet body.

View File

@@ -39,6 +39,16 @@ func TestConnackEncode(t *testing.T) {
} }
} }
func BenchmarkConnackEncode(b *testing.B) {
pk := new(ConnackPacket)
copier.Copy(pk, expectedPackets[Connack][0].packet.(*ConnackPacket))
var buf bytes.Buffer
for n := 0; n < b.N; n++ {
pk.Encode(&buf)
}
}
func TestConnackDecode(t *testing.T) { func TestConnackDecode(t *testing.T) {
require.Contains(t, expectedPackets, Connack) require.Contains(t, expectedPackets, Connack)
for i, wanted := range expectedPackets[Connack] { for i, wanted := range expectedPackets[Connack] {

View File

@@ -26,16 +26,15 @@ type FixedHeader struct {
// Encode encodes the FixedHeader and returns a bytes buffer. // Encode encodes the FixedHeader and returns a bytes buffer.
func (fh *FixedHeader) encode() bytes.Buffer { func (fh *FixedHeader) encode() bytes.Buffer {
var encoded bytes.Buffer
var out bytes.Buffer
// Encode flags. // Encode flags.
out.WriteByte(fh.Type<<4 | encodeBool(fh.Dup)<<3 | fh.Qos<<1 | encodeBool(fh.Retain)) encoded.WriteByte(fh.Type<<4 | encodeBool(fh.Dup)<<3 | fh.Qos<<1 | encodeBool(fh.Retain))
// Determine encoded length and write the buffer. // Determine encoded length and write the buffer.
out.Write(encodeLength(fh.Remaining)) encoded.Write(encodeLength(fh.Remaining))
return out return encoded
} }
@@ -79,17 +78,17 @@ func (fh *FixedHeader) decode(headerByte byte) error {
// encodeLength creates length bits for the header. // encodeLength creates length bits for the header.
func encodeLength(length int) []byte { func encodeLength(length int) []byte {
var encLength []byte encodedLength := make([]byte, 0, 8)
for { for {
digit := byte(length % 128) digit := byte(length % 128)
length /= 128 length /= 128
if length > 0 { if length > 0 {
digit |= 0x80 digit |= 0x80
} }
encLength = append(encLength, digit) encodedLength = append(encodedLength, digit)
if length == 0 { if length == 0 {
break break
} }
} }
return encLength return encodedLength
} }

View File

@@ -1,9 +1,11 @@
package packets package packets
import ( import (
"github.com/stretchr/testify/require"
"testing"
//"bytes" //"bytes"
"math"
"testing"
"github.com/stretchr/testify/require"
) )
type fixedHeaderTable struct { type fixedHeaderTable struct {
@@ -146,25 +148,23 @@ var fixedHeaderExpected = []fixedHeaderTable{
} }
func TestFixedHeaderEncode(t *testing.T) { func TestFixedHeaderEncode(t *testing.T) {
for i, wanted := range fixedHeaderExpected { for i, wanted := range fixedHeaderExpected {
b := wanted.header.encode() b := wanted.header.encode()
require.Equal(t, len(wanted.rawBytes), len(b.Bytes()), "Mismatched fixedheader length [i:%d] %v", i, wanted.rawBytes) require.Equal(t, len(wanted.rawBytes), len(b.Bytes()), "Mismatched fixedheader length [i:%d] %v", i, wanted.rawBytes)
require.EqualValues(t, wanted.rawBytes, b.Bytes(), "Mismatched byte values [i:%d] %v", i, wanted.rawBytes) require.EqualValues(t, wanted.rawBytes, b.Bytes(), "Mismatched byte values [i:%d] %v", i, wanted.rawBytes)
}
} }
func BenchmarkFixedHeaderEncode(b *testing.B) {
for n := 0; n < b.N; n++ {
fixedHeaderExpected[0].header.encode()
}
} }
func TestFixedHeaderDecode(t *testing.T) { func TestFixedHeaderDecode(t *testing.T) {
for i, wanted := range fixedHeaderExpected { for i, wanted := range fixedHeaderExpected {
fh := new(FixedHeader)
fh := &FixedHeader{}
err := fh.decode(wanted.rawBytes[0]) err := fh.decode(wanted.rawBytes[0])
if wanted.flagError { if wanted.flagError {
require.Error(t, err, "Expected error reading fixedheader [i:%d] %v", i, wanted.rawBytes) require.Error(t, err, "Expected error reading fixedheader [i:%d] %v", i, wanted.rawBytes)
} else { } else {
@@ -173,8 +173,42 @@ func TestFixedHeaderDecode(t *testing.T) {
require.Equal(t, wanted.header.Dup, fh.Dup, "Mismatched fixedheader dup [i:%d] %v", i, wanted.rawBytes) require.Equal(t, wanted.header.Dup, fh.Dup, "Mismatched fixedheader dup [i:%d] %v", i, wanted.rawBytes)
require.Equal(t, wanted.header.Qos, fh.Qos, "Mismatched fixedheader qos [i:%d] %v", i, wanted.rawBytes) require.Equal(t, wanted.header.Qos, fh.Qos, "Mismatched fixedheader qos [i:%d] %v", i, wanted.rawBytes)
require.Equal(t, wanted.header.Retain, fh.Retain, "Mismatched fixedheader retain [i:%d] %v", i, wanted.rawBytes) require.Equal(t, wanted.header.Retain, fh.Retain, "Mismatched fixedheader retain [i:%d] %v", i, wanted.rawBytes)
}
} }
} }
func BenchmarkFixedHeaderDecode(b *testing.B) {
fh := new(FixedHeader)
for n := 0; n < b.N; n++ {
err := fh.decode(fixedHeaderExpected[0].rawBytes[0])
if err != nil {
panic(err)
}
}
}
func TestEncodeLength(t *testing.T) {
tt := []struct {
have int
want []byte
}{
{
120,
[]byte{0x78},
},
{
math.MaxInt64,
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f},
},
}
for i, wanted := range tt {
require.Equal(t, wanted.want, encodeLength(wanted.have), "Returned bytes should match length [i:%d] %s", i, wanted.have)
}
}
func BenchmarkEncodeLength(b *testing.B) {
for n := 0; n < b.N; n++ {
encodeLength(120)
}
} }