github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/bytes_buffer_test.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  	"sync"
     7  	"testing"
     8  )
     9  
    10  func BenchmarkBytes(b *testing.B) {
    11  	count := 10
    12  	pc := 50000
    13  	b.Run("buffer string", func(b *testing.B) {
    14  		b.ReportAllocs()
    15  		for i := 0; i < b.N; i++ {
    16  			var wg sync.WaitGroup
    17  			wg.Add(pc)
    18  			for i := 0; i < pc; i++ {
    19  				go func() {
    20  					var buffer ByteBuffer
    21  					buffer.InitCap(190)
    22  					for i := 0; i < count; i++ {
    23  						buffer.WStringNoLen(strconv.Itoa(i))
    24  					}
    25  					buffer.Dispose()
    26  					wg.Done()
    27  				}()
    28  			}
    29  			wg.Wait()
    30  		}
    31  	})
    32  	b.Run("builder string", func(b *testing.B) {
    33  		b.ReportAllocs()
    34  		for i := 0; i < b.N; i++ {
    35  			var wg sync.WaitGroup
    36  			wg.Add(pc)
    37  			for i := 0; i < pc; i++ {
    38  				go func() {
    39  					builder := bytes.NewBuffer(nil)
    40  					for i := 0; i < count; i++ {
    41  						builder.WriteString(strconv.Itoa(i))
    42  					}
    43  					wg.Done()
    44  				}()
    45  			}
    46  			wg.Wait()
    47  		}
    48  	})
    49  }