github.com/lxt1045/json@v0.0.0-20231013032136-54d6b1d6e525/try_bench_test.go (about)

     1  package json
     2  
     3  import (
     4  	"reflect"
     5  	"runtime"
     6  	"sync"
     7  	"testing"
     8  	"unsafe"
     9  )
    10  
    11  // TODO: slice
    12  
    13  var pgStr *string
    14  
    15  func BenchmarkBatch(b *testing.B) {
    16  	const N = 100
    17  	batch := NewBatch[string]()
    18  	for i := 0; i < 2; i++ {
    19  		runtime.GC()
    20  		b.Run("batch-BatchGet", func(b *testing.B) {
    21  			for i := 0; i < b.N*N; i++ {
    22  				pgStr = BatchGet(batch)
    23  			}
    24  		})
    25  		runtime.GC()
    26  		b.Run("batch", func(b *testing.B) {
    27  			for i := 0; i < b.N*N; i++ {
    28  				pgStr = batch.Get()
    29  			}
    30  		})
    31  		runtime.GC()
    32  		b.Run("batch-GetN", func(b *testing.B) {
    33  			for i := 0; i < b.N*N; i++ {
    34  				pgStr = batch.GetN(1)
    35  			}
    36  		})
    37  		runtime.GC()
    38  		b.Run("batch-GetN4", func(b *testing.B) {
    39  			for i := 0; i < b.N*N; i++ {
    40  				pgStr = batch.GetN(4)
    41  			}
    42  		})
    43  
    44  		bObj := NewBatchObj(reflect.TypeOf(""))
    45  		runtime.GC()
    46  		b.Run("BatchObj-Get", func(b *testing.B) {
    47  			for i := 0; i < b.N*N; i++ {
    48  				pgStr = (*string)(bObj.Get())
    49  			}
    50  		})
    51  		runtime.GC()
    52  		b.Run("BatchObj-GetN", func(b *testing.B) {
    53  			for i := 0; i < b.N*N; i++ {
    54  				pgStr = (*string)(bObj.GetN(1))
    55  			}
    56  		})
    57  		runtime.GC()
    58  		b.Run("BatchObj-GetN4", func(b *testing.B) {
    59  			for i := 0; i < b.N*N; i++ {
    60  				pgStr = (*string)(bObj.GetN(4))
    61  			}
    62  		})
    63  	}
    64  	stringPool := sync.Pool{
    65  		New: func() any {
    66  			s := make([]string, 512, 512) //8*1024/16
    67  			return &s
    68  		},
    69  	}
    70  	runtime.GC()
    71  	b.Run("pool", func(b *testing.B) {
    72  		for i := 0; i < b.N*N; i++ {
    73  			pstrs := stringPool.Get().(*[]string)
    74  			pgStr = &(*pstrs)[0]
    75  			if len(*pstrs) > 1 {
    76  				*pstrs = (*pstrs)[1:]
    77  				stringPool.Put(pstrs)
    78  			}
    79  		}
    80  	})
    81  	runtime.GC()
    82  	b.Run("new", func(b *testing.B) {
    83  		for i := 0; i < b.N*N; i++ {
    84  			pgStr = new(string)
    85  		}
    86  	})
    87  	l := sync.Mutex{}
    88  	runtime.GC()
    89  	b.Run("Lock", func(b *testing.B) {
    90  		for i := 0; i < b.N*N; i++ {
    91  			l.Lock()
    92  			l.Unlock()
    93  		}
    94  	})
    95  	runtime.GC()
    96  	b.Run("reflect.StringHeader", func(b *testing.B) {
    97  		for i := 0; i < b.N*N; i++ {
    98  			h := reflect.StringHeader{}
    99  			pgStr = (*string)((unsafe.Pointer)(&h))
   100  		}
   101  	})
   102  	runtime.GC()
   103  	b.Run("lxt.StringHeader", func(b *testing.B) {
   104  		for i := 0; i < b.N*N; i++ {
   105  			h := StringHeader{}
   106  			pgStr = (*string)((unsafe.Pointer)(&h))
   107  		}
   108  	})
   109  	runtime.GC()
   110  }
   111  
   112  func BenchmarkNewGC(b *testing.B) {
   113  	const N = 100
   114  	type StringGC struct {
   115  		Data *byte
   116  		Len  int
   117  	}
   118  	type String struct {
   119  		Data int
   120  		Len  int
   121  	}
   122  	batch := NewBatch[string]()
   123  	for i := 0; i < 5; i++ {
   124  		runtime.GC()
   125  		b.Run("batch-BatchGet", func(b *testing.B) {
   126  			for i := 0; i < b.N*N; i++ {
   127  				pgStr = BatchGet(batch)
   128  			}
   129  		})
   130  		runtime.GC()
   131  		b.Run("reflect.String", func(b *testing.B) {
   132  			for i := 0; i < b.N*N; i++ {
   133  				h := String{}
   134  				pgStr = (*string)((unsafe.Pointer)(&h))
   135  			}
   136  		})
   137  		runtime.GC()
   138  		b.Run("lxt.StringGC", func(b *testing.B) {
   139  			for i := 0; i < b.N*N; i++ {
   140  				h := StringGC{}
   141  				pgStr = (*string)((unsafe.Pointer)(&h))
   142  			}
   143  		})
   144  		runtime.GC()
   145  	}
   146  }