diff --git a/packets/connack.go b/packets/connack.go index 53d7ba1..8d9c31c 100644 --- a/packets/connack.go +++ b/packets/connack.go @@ -16,7 +16,6 @@ type ConnackPacket struct { // Encode encodes and writes the packet data values to the buffer. func (pk *ConnackPacket) Encode(w io.Writer) error { - var body bytes.Buffer // Write flags to packet body. diff --git a/packets/connack_test.go b/packets/connack_test.go index 033661a..a28d8e3 100644 --- a/packets/connack_test.go +++ b/packets/connack_test.go @@ -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) { require.Contains(t, expectedPackets, Connack) for i, wanted := range expectedPackets[Connack] { diff --git a/packets/fixedheader.go b/packets/fixedheader.go index 5640c34..f96a077 100644 --- a/packets/fixedheader.go +++ b/packets/fixedheader.go @@ -26,16 +26,15 @@ type FixedHeader struct { // Encode encodes the FixedHeader and returns a bytes buffer. func (fh *FixedHeader) encode() bytes.Buffer { - - var out bytes.Buffer + var encoded bytes.Buffer // 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. - 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. func encodeLength(length int) []byte { - var encLength []byte + encodedLength := make([]byte, 0, 8) for { digit := byte(length % 128) length /= 128 if length > 0 { digit |= 0x80 } - encLength = append(encLength, digit) + encodedLength = append(encodedLength, digit) if length == 0 { break } } - return encLength + return encodedLength } diff --git a/packets/fixedheader_test.go b/packets/fixedheader_test.go index 4fc2e6f..f6da76a 100644 --- a/packets/fixedheader_test.go +++ b/packets/fixedheader_test.go @@ -1,9 +1,11 @@ package packets import ( - "github.com/stretchr/testify/require" - "testing" //"bytes" + "math" + "testing" + + "github.com/stretchr/testify/require" ) type fixedHeaderTable struct { @@ -146,25 +148,23 @@ var fixedHeaderExpected = []fixedHeaderTable{ } func TestFixedHeaderEncode(t *testing.T) { - for i, wanted := range fixedHeaderExpected { b := wanted.header.encode() - 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) - } +} +func BenchmarkFixedHeaderEncode(b *testing.B) { + for n := 0; n < b.N; n++ { + fixedHeaderExpected[0].header.encode() + } } func TestFixedHeaderDecode(t *testing.T) { - for i, wanted := range fixedHeaderExpected { - - fh := &FixedHeader{} - + fh := new(FixedHeader) err := fh.decode(wanted.rawBytes[0]) - if wanted.flagError { require.Error(t, err, "Expected error reading fixedheader [i:%d] %v", i, wanted.rawBytes) } 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.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) - } } - +} + +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) + } }