github.com/niubaoshu/gotiny@v0.0.4-0.20211018120156-10d393f19ad0/gotiny_bench_test.go (about)

     1  package gotiny
     2  
     3  import (
     4  	"io"
     5  	"math/rand"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  	"unsafe"
    10  )
    11  
    12  var (
    13  	buf   []byte
    14  	value = genA()
    15  	e     *Encoder
    16  	d     *Decoder
    17  )
    18  
    19  func init() {
    20  	t := reflect.TypeOf(value).Elem()
    21  	e = NewEncoderWithType(t)
    22  	d = NewDecoderWithType(t)
    23  	buf = e.Encode(value)
    24  }
    25  
    26  func BenchmarkEncode(b *testing.B) {
    27  	for i := 0; i < b.N; i++ {
    28  		Marshal(value)
    29  	}
    30  }
    31  
    32  func BenchmarkDecode(b *testing.B) {
    33  	for i := 0; i < b.N; i++ {
    34  		Unmarshal(buf, value)
    35  	}
    36  }
    37  
    38  func BenchmarkEncode2(b *testing.B) {
    39  	for i := 0; i < b.N; i++ {
    40  		e.Encode(value)
    41  	}
    42  }
    43  
    44  func BenchmarkDecode2(b *testing.B) {
    45  	for i := 0; i < b.N; i++ {
    46  		d.Decode(buf, value)
    47  	}
    48  }
    49  
    50  type (
    51  	baseTyp struct {
    52  		FBool          bool
    53  		FInt8          int8
    54  		FInt16         int16
    55  		FInt32         int32
    56  		FInt64         int64
    57  		FInt           int
    58  		FUint8         uint8
    59  		FUint16        uint16
    60  		FUint32        uint32
    61  		FUint64        uint64
    62  		FUint          uint
    63  		FUintptr       uintptr
    64  		FFloat32       float32
    65  		FFloat64       float64
    66  		FComplex64     complex64
    67  		FComplex128    complex128
    68  		FString        string
    69  		FUnsafePointer unsafe.Pointer
    70  	}
    71  
    72  	A struct {
    73  		Array    [10]baseTyp
    74  		Slice    []baseTyp
    75  		BirthDay time.Time
    76  		Inter    interface{}
    77  		M        map[string]*baseTyp
    78  	}
    79  )
    80  
    81  func genBase() baseTyp {
    82  	return baseTyp{
    83  		FBool:          rand.Int()%2 == 0,
    84  		FInt8:          int8(rand.Int()),
    85  		FInt16:         int16(rand.Int()),
    86  		FInt32:         int32(rand.Int()),
    87  		FInt64:         int64(rand.Int()),
    88  		FInt:           int(rand.Int()),
    89  		FUint8:         uint8(rand.Int()),
    90  		FUint16:        uint16(rand.Int()),
    91  		FUint64:        uint64(rand.Int()),
    92  		FUintptr:       uintptr(rand.Int()),
    93  		FFloat32:       rand.Float32(),
    94  		FFloat64:       rand.Float64(),
    95  		FComplex64:     complex(rand.Float32(), rand.Float32()),
    96  		FComplex128:    complex(rand.Float64(), rand.Float64()),
    97  		FString:        GetRandomString(20 + rand.Intn(256)),
    98  		FUnsafePointer: unsafe.Pointer(nil),
    99  	}
   100  }
   101  
   102  func genA() *A {
   103  	ml := 10
   104  	a := &A{
   105  		BirthDay: time.Now(),
   106  		Inter:    genBase(),
   107  		M:        make(map[string]*baseTyp),
   108  	}
   109  	a.Slice = make([]baseTyp, len(a.Array))
   110  	for i := 0; i < len(a.Array); i++ {
   111  		a.Array[i] = genBase()
   112  		a.Slice[i] = genBase()
   113  	}
   114  
   115  	for i := 0; i < ml; i++ {
   116  		b := genBase()
   117  		a.M[GetRandomString(10)] = &b
   118  	}
   119  	return a
   120  }
   121  
   122  func GetRandomString(l int) string {
   123  	bytes := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
   124  	result := make([]byte, l)
   125  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   126  	for i := 0; i < l; i++ {
   127  		result[i] = bytes[r.Intn(62)]
   128  	}
   129  	return string(result)
   130  }
   131  
   132  func BenchmarkDecodeUint64(b *testing.B) {
   133  	var ints = make([][]byte, 10000)
   134  	for i := 0; i < len(ints); i++ {
   135  		a := rand.Uint64()
   136  		ints[i] = Marshal(&a)
   137  	}
   138  	d := Decoder{}
   139  	b.ResetTimer()
   140  	for i := 0; i < b.N; i++ {
   141  		d.buf = ints[rand.Intn(10000)]
   142  		d.index = 0
   143  		d.decUint64()
   144  	}
   145  }
   146  
   147  func BenchmarkEncodeUint64(b *testing.B) {
   148  	e := Encoder{buf: make([]byte, 0, 600000000)}
   149  	b.ResetTimer()
   150  	for i := 0; i < b.N; i++ {
   151  		e.encUint64(rand.Uint64())
   152  	}
   153  }
   154  
   155  func BenchmarkEncodeBool(b *testing.B) {
   156  	l := 2000
   157  	e := Encoder{buf: make([]byte, 0, 600000000)}
   158  	b.ResetTimer()
   159  	for i := 0; i < b.N; i++ {
   160  		for j := 0; j < l*8; j++ {
   161  			e.encBool(i%2 == 0)
   162  		}
   163  	}
   164  }
   165  
   166  func BenchmarkDecodeBool(b *testing.B) {
   167  	l := 2000
   168  	var ints = make([][]byte, 10000)
   169  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   170  	for i := 0; i < len(ints); i++ {
   171  		s := make([]byte, l)
   172  		io.ReadFull(r, s)
   173  		ints[i] = s
   174  	}
   175  	d := Decoder{}
   176  	b.ResetTimer()
   177  	for i := 0; i < b.N; i++ {
   178  		d.buf = ints[rand.Intn(10000)]
   179  		d.boolBit = 0
   180  		d.boolPos = 0
   181  		d.index = 0
   182  		for j := 0; j < l*8; j++ {
   183  			d.decBool()
   184  		}
   185  	}
   186  }