github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/persist/fs/msgpack/encoder_decoder_bench_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE
    20  
    21  package msgpack
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func BenchmarkLogEntryDecoder(b *testing.B) {
    30  	var (
    31  		enc    = NewEncoder()
    32  		dec    = NewDecoder(nil)
    33  		stream = NewByteDecoderStream(nil)
    34  		err    error
    35  	)
    36  
    37  	require.NoError(b, enc.EncodeLogEntry(testLogEntry))
    38  	buf := enc.Bytes()
    39  	for n := 0; n < b.N; n++ {
    40  		stream.Reset(buf)
    41  		dec.Reset(stream)
    42  		_, err = dec.DecodeLogEntry()
    43  		if err != nil {
    44  			panic(err)
    45  		}
    46  	}
    47  }
    48  
    49  func BenchmarkLogEntryDecoderFast(b *testing.B) {
    50  	var (
    51  		enc = NewEncoder()
    52  		err error
    53  	)
    54  
    55  	require.NoError(b, enc.EncodeLogEntry(testLogEntry))
    56  	buf := enc.Bytes()
    57  	for n := 0; n < b.N; n++ {
    58  		_, err = DecodeLogEntryFast(buf)
    59  		if err != nil {
    60  			panic(err)
    61  		}
    62  	}
    63  }
    64  
    65  var benchmarkBuf []byte
    66  
    67  func BenchmarkLogEntryEncoderFast(b *testing.B) {
    68  	var err error
    69  	benchmarkBuf = []byte{}
    70  
    71  	for n := 0; n < b.N; n++ {
    72  		benchmarkBuf, err = EncodeLogEntryFast(benchmarkBuf[:0], testLogEntry)
    73  		if err != nil {
    74  			panic(err)
    75  		}
    76  	}
    77  }
    78  
    79  func BenchmarkLogEntryEncoder(b *testing.B) {
    80  	var (
    81  		enc = NewEncoder()
    82  		err error
    83  	)
    84  
    85  	for n := 0; n < b.N; n++ {
    86  		enc.EncodeLogEntry(testLogEntry)
    87  		if err != nil {
    88  			panic(err)
    89  		}
    90  		benchmarkBuf = enc.Bytes()
    91  	}
    92  }
    93  
    94  func BenchmarkLogMetadataEncoder(b *testing.B) {
    95  	var (
    96  		enc = NewEncoder()
    97  		err error
    98  	)
    99  
   100  	for n := 0; n < b.N; n++ {
   101  		enc.EncodeLogMetadata(testLogMetadata)
   102  		if err != nil {
   103  			panic(err)
   104  		}
   105  		benchmarkBuf = enc.Bytes()
   106  	}
   107  }
   108  
   109  func BenchmarkLogMetadataEncoderFast(b *testing.B) {
   110  	var err error
   111  	benchmarkBuf = []byte{}
   112  
   113  	for n := 0; n < b.N; n++ {
   114  		benchmarkBuf, err = EncodeLogMetadataFast(benchmarkBuf[:0], testLogMetadata)
   115  		if err != nil {
   116  			panic(err)
   117  		}
   118  	}
   119  }
   120  
   121  func BenchmarkLogMetadataDecoder(b *testing.B) {
   122  	var (
   123  		enc    = NewEncoder()
   124  		dec    = NewDecoder(nil)
   125  		stream = NewByteDecoderStream(nil)
   126  		err    error
   127  	)
   128  
   129  	require.NoError(b, enc.EncodeLogMetadata(testLogMetadata))
   130  	buf := enc.Bytes()
   131  	for n := 0; n < b.N; n++ {
   132  		stream.Reset(buf)
   133  		dec.Reset(stream)
   134  		_, err = dec.DecodeLogMetadata()
   135  		if err != nil {
   136  			panic(err)
   137  		}
   138  	}
   139  }
   140  
   141  func BenchmarkLogMetadataDecoderFast(b *testing.B) {
   142  	var (
   143  		enc = NewEncoder()
   144  		err error
   145  	)
   146  
   147  	require.NoError(b, enc.EncodeLogMetadata(testLogMetadata))
   148  	buf := enc.Bytes()
   149  	for n := 0; n < b.N; n++ {
   150  		_, err = DecodeLogMetadataFast(buf)
   151  		if err != nil {
   152  			panic(err)
   153  		}
   154  	}
   155  }