trpc.group/trpc-go/trpc-go@v1.0.3/codec/compress_bench_test.go (about)

     1  package codec_test
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"trpc.group/trpc-go/trpc-go/codec"
     8  )
     9  
    10  func BenchmarkCheckNoopCompression(b *testing.B) {
    11  	b.Run("check noop compression", func(b *testing.B) {
    12  		bs := make([]byte, 1024)
    13  		var result []byte
    14  		for i := 0; i < b.N; i++ {
    15  			result, _ = codec.Compress(codec.CompressTypeNoop, bs)
    16  			bs, _ = codec.Decompress(codec.CompressTypeNoop, result)
    17  		}
    18  	})
    19  	b.Run("not check noop compression", func(b *testing.B) {
    20  		bs := make([]byte, 1024)
    21  		var result []byte
    22  		for i := 0; i < b.N; i++ {
    23  			result, _ = oldCompress(codec.CompressTypeNoop, bs)
    24  			bs, _ = oldDecompress(codec.CompressTypeNoop, result)
    25  		}
    26  	})
    27  }
    28  
    29  // oldCompress returns the compressed data, the data is compressed
    30  // by a specific compressor.
    31  func oldCompress(compressorType int, in []byte) ([]byte, error) {
    32  	if len(in) == 0 {
    33  		return nil, nil
    34  	}
    35  	compressor := codec.GetCompressor(compressorType)
    36  	if compressor == nil {
    37  		return nil, errors.New("compressor not registered")
    38  	}
    39  	return compressor.Compress(in)
    40  }
    41  
    42  // oldDecompress returns the decompressed data, the data is decompressed
    43  // by a specific compressor.
    44  func oldDecompress(compressorType int, in []byte) ([]byte, error) {
    45  	if len(in) == 0 {
    46  		return nil, nil
    47  	}
    48  	compressor := codec.GetCompressor(compressorType)
    49  	if compressor == nil {
    50  		return nil, errors.New("compressor not registered")
    51  	}
    52  	return compressor.Decompress(in)
    53  }