github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/buffer_pool_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package utils_test
     4  
     5  import (
     6  	"bytes"
     7  	"github.com/TeaOSLab/EdgeNode/internal/utils"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestNewBufferPool(t *testing.T) {
    13  	var pool = utils.NewBufferPool()
    14  	var b = pool.Get()
    15  	b.WriteString("Hello, World")
    16  	t.Log(b.String())
    17  
    18  	pool.Put(b)
    19  	t.Log(b.String())
    20  
    21  	b = pool.Get()
    22  	t.Log(b.String())
    23  }
    24  
    25  func BenchmarkNewBufferPool1(b *testing.B) {
    26  	var data = []byte(strings.Repeat("Hello", 1024))
    27  
    28  	b.RunParallel(func(pb *testing.PB) {
    29  		for pb.Next() {
    30  			var buffer = &bytes.Buffer{}
    31  			buffer.Write(data)
    32  		}
    33  	})
    34  }
    35  
    36  func BenchmarkNewBufferPool2(b *testing.B) {
    37  	var pool = utils.NewBufferPool()
    38  	var data = []byte(strings.Repeat("Hello", 1024))
    39  
    40  	b.RunParallel(func(pb *testing.PB) {
    41  		for pb.Next() {
    42  			var buffer = pool.Get()
    43  			buffer.Write(data)
    44  			pool.Put(buffer)
    45  		}
    46  	})
    47  }
    48  
    49  func BenchmarkNewBufferPool3(b *testing.B) {
    50  	var pool = utils.NewBufferPool()
    51  	var dataString = strings.Repeat("Hello", 1024)
    52  
    53  	b.RunParallel(func(pb *testing.PB) {
    54  		for pb.Next() {
    55  			var buffer = pool.Get()
    56  			buffer.Write([]byte(dataString))
    57  			pool.Put(buffer)
    58  		}
    59  	})
    60  }
    61  
    62  func BenchmarkNewBufferPool4(b *testing.B) {
    63  	var pool = utils.NewBufferPool()
    64  	var dataString = strings.Repeat("Hello", 1024)
    65  
    66  	b.RunParallel(func(pb *testing.PB) {
    67  		for pb.Next() {
    68  			var buffer = pool.Get()
    69  			buffer.WriteString(dataString)
    70  			pool.Put(buffer)
    71  		}
    72  	})
    73  }