github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/transformer/z_util_io_test.go (about)

     1  package transformer_test
     2  
     3  import (
     4  	"io"
     5  	"strings"
     6  	"testing"
     7  
     8  	. "github.com/onsi/gomega"
     9  
    10  	. "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/transformer"
    11  )
    12  
    13  func BenchmarkBuffers(b *testing.B) {
    14  	b.Run("StringReaders", func(b *testing.B) {
    15  		inputs := strings.Split(strings.Repeat("1", 10), "")
    16  
    17  		for i := 0; i < b.N; i++ {
    18  			buffers := NewStringReaders(inputs)
    19  			for i := 0; i < buffers.Len(); i++ {
    20  				r := buffers.NextReader()
    21  				_, _ = io.ReadAll(r)
    22  			}
    23  		}
    24  	})
    25  
    26  	b.Run("StringBuilders", func(b *testing.B) {
    27  		for i := 0; i < b.N; i++ {
    28  			builders := NewStringBuilders()
    29  			builders.SetN(10)
    30  
    31  			for i := 0; i < 10; i++ {
    32  				w := builders.NextWriter()
    33  				_, _ = w.Write([]byte("1"))
    34  			}
    35  
    36  			_ = builders.StringSlice()
    37  		}
    38  	})
    39  }
    40  
    41  func TestNewBuffers(t *testing.T) {
    42  	inputs := strings.Split(strings.Repeat("1", 10), "")
    43  
    44  	t.Run("StringReaders", func(t *testing.T) {
    45  		buffers := NewStringReaders(inputs)
    46  		results := make([]string, 0)
    47  		for i := 0; i < buffers.Len(); i++ {
    48  			r := buffers.NextReader()
    49  			data, _ := io.ReadAll(r)
    50  			results = append(results, string(data))
    51  		}
    52  		NewWithT(t).Expect(results).To(Equal(inputs))
    53  	})
    54  
    55  	t.Run("StringBuilders", func(t *testing.T) {
    56  		builders := NewStringBuilders()
    57  		builders.SetN(10)
    58  		for i := 0; i < 10; i++ {
    59  			w := builders.NextWriter()
    60  			_, _ = w.Write([]byte("1"))
    61  		}
    62  		NewWithT(t).Expect(builders.StringSlice()).To(Equal(inputs))
    63  	})
    64  }