github.com/decred/dcrlnd@v0.7.6/tlv/bench_test.go (about)

     1  package tlv_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"testing"
     8  
     9  	"github.com/decred/dcrlnd/lnwallet/chainfee"
    10  	"github.com/decred/dcrlnd/tlv"
    11  	"github.com/decred/dcrlnd/watchtower/blob"
    12  	"github.com/decred/dcrlnd/watchtower/wtwire"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  // CreateSessionTLV mirrors the wtwire.CreateSession message, but uses TLV for
    17  // encoding/decoding.
    18  type CreateSessionTLV struct {
    19  	BlobType     blob.Type
    20  	MaxUpdates   uint16
    21  	RewardBase   uint32
    22  	RewardRate   uint32
    23  	SweepFeeRate chainfee.AtomPerKByte
    24  
    25  	tlvStream *tlv.Stream
    26  }
    27  
    28  // EBlobType is an encoder for blob.Type.
    29  func EBlobType(w io.Writer, val interface{}, buf *[8]byte) error {
    30  	if t, ok := val.(*blob.Type); ok {
    31  		return tlv.EUint16T(w, uint16(*t), buf)
    32  	}
    33  	return tlv.NewTypeForEncodingErr(val, "blob.Type")
    34  }
    35  
    36  // EBlobType is an decoder for blob.Type.
    37  func DBlobType(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
    38  	if typ, ok := val.(*blob.Type); ok {
    39  		var t uint16
    40  		err := tlv.DUint16(r, &t, buf, l)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		*typ = blob.Type(t)
    45  		return nil
    46  	}
    47  	return tlv.NewTypeForDecodingErr(val, "blob.Type", l, 2)
    48  }
    49  
    50  // EAtomsPerKB is an encoder for chainfee.AtomPerKByte.
    51  func EAtomsPerKB(w io.Writer, val interface{}, buf *[8]byte) error {
    52  	if v, ok := val.(*chainfee.AtomPerKByte); ok {
    53  		v64 := uint64(*v)
    54  		return tlv.EUint64(w, &v64, buf)
    55  	}
    56  	return tlv.NewTypeForEncodingErr(val, "chainfee.AtomPerKByte")
    57  }
    58  
    59  // DAtomsPerKB is an decoder for chainfee.AtomPerKByte.
    60  func DAtomsPerKB(r io.Reader, val interface{}, buf *[8]byte, l uint64) error {
    61  	if v, ok := val.(*chainfee.AtomPerKByte); ok {
    62  		var sat uint64
    63  		err := tlv.DUint64(r, &sat, buf, l)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		*v = chainfee.AtomPerKByte(sat)
    68  		return nil
    69  	}
    70  	return tlv.NewTypeForDecodingErr(val, "chainfee.AtomPerKByte", l, 8)
    71  }
    72  
    73  // NewCreateSessionTLV initializes a new CreateSessionTLV message.
    74  func NewCreateSessionTLV() *CreateSessionTLV {
    75  	m := &CreateSessionTLV{}
    76  	m.tlvStream = tlv.MustNewStream(
    77  		tlv.MakeStaticRecord(0, &m.BlobType, 2, EBlobType, DBlobType),
    78  		tlv.MakePrimitiveRecord(1, &m.MaxUpdates),
    79  		tlv.MakePrimitiveRecord(2, &m.RewardBase),
    80  		tlv.MakePrimitiveRecord(3, &m.RewardRate),
    81  		tlv.MakeStaticRecord(4, &m.SweepFeeRate, 8, EAtomsPerKB, DAtomsPerKB),
    82  	)
    83  
    84  	return m
    85  }
    86  
    87  // Encode writes the CreateSessionTLV to the passed io.Writer.
    88  func (c *CreateSessionTLV) Encode(w io.Writer) error {
    89  	return c.tlvStream.Encode(w)
    90  }
    91  
    92  // Decode reads the CreateSessionTLV from the passed io.Reader.
    93  func (c *CreateSessionTLV) Decode(r io.Reader) error {
    94  	return c.tlvStream.Decode(r)
    95  }
    96  
    97  // BenchmarkEncodeCreateSession benchmarks encoding of the non-TLV
    98  // CreateSession.
    99  func BenchmarkEncodeCreateSession(t *testing.B) {
   100  	m := &wtwire.CreateSession{}
   101  
   102  	t.ReportAllocs()
   103  	t.ResetTimer()
   104  
   105  	var err error
   106  	for i := 0; i < t.N; i++ {
   107  		err = m.Encode(ioutil.Discard, 0)
   108  	}
   109  	require.NoError(t, err)
   110  }
   111  
   112  // BenchmarkEncodeCreateSessionTLV benchmarks encoding of the TLV CreateSession.
   113  func BenchmarkEncodeCreateSessionTLV(t *testing.B) {
   114  	m := NewCreateSessionTLV()
   115  
   116  	t.ReportAllocs()
   117  	t.ResetTimer()
   118  
   119  	var err error
   120  	for i := 0; i < t.N; i++ {
   121  		err = m.Encode(ioutil.Discard)
   122  	}
   123  	require.NoError(t, err)
   124  }
   125  
   126  // BenchmarkDecodeCreateSession benchmarks encoding of the non-TLV
   127  // CreateSession.
   128  func BenchmarkDecodeCreateSession(t *testing.B) {
   129  	m := &wtwire.CreateSession{}
   130  
   131  	var b bytes.Buffer
   132  	err := m.Encode(&b, 0)
   133  	require.NoError(t, err)
   134  
   135  	r := bytes.NewReader(b.Bytes())
   136  
   137  	t.ReportAllocs()
   138  	t.ResetTimer()
   139  
   140  	for i := 0; i < t.N; i++ {
   141  		r.Seek(0, 0)
   142  		err = m.Decode(r, 0)
   143  	}
   144  	require.NoError(t, err)
   145  }
   146  
   147  // BenchmarkDecodeCreateSessionTLV benchmarks decoding of the TLV CreateSession.
   148  func BenchmarkDecodeCreateSessionTLV(t *testing.B) {
   149  	m := NewCreateSessionTLV()
   150  
   151  	var b bytes.Buffer
   152  	err := m.Encode(&b)
   153  	require.NoError(t, err)
   154  
   155  	r := bytes.NewReader(b.Bytes())
   156  
   157  	t.ReportAllocs()
   158  	t.ResetTimer()
   159  
   160  	for i := 0; i < t.N; i++ {
   161  		r.Seek(0, 0)
   162  		err = m.Decode(r)
   163  	}
   164  	require.NoError(t, err)
   165  }