github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zutil/buffpool_test.go (about) 1 package zutil_test 2 3 import ( 4 "bytes" 5 "strings" 6 "testing" 7 8 "github.com/sohaha/zlsgo" 9 "github.com/sohaha/zlsgo/zstring" 10 "github.com/sohaha/zlsgo/zutil" 11 ) 12 13 func TestBuff(tt *testing.T) { 14 t := zlsgo.NewTest(tt) 15 buffer := zutil.GetBuff() 16 buffer.WriteString("1") 17 buffer.WriteString("2") 18 t.EqualExit("12", buffer.String()) 19 zutil.PutBuff(buffer) 20 t.EqualExit("", buffer.String()) 21 buffer2 := zutil.GetBuff() 22 t.EqualExit("", buffer2.String()) 23 buffer2.Reset() 24 25 buffer3 := zutil.GetBuff(0) 26 tt.Log(buffer3.Len(), buffer3.Cap()) 27 zutil.PutBuff(buffer3) 28 29 buffer4 := zutil.GetBuff(104857609) 30 tt.Log(buffer4.Len(), buffer4.Cap()) 31 zutil.PutBuff(buffer4) 32 33 buffer5 := zutil.GetBuff(16) 34 tt.Log(buffer5.Len(), buffer5.Cap()) 35 buffer5.WriteString(strings.Repeat("0", 104857609)) 36 tt.Log(buffer5.Len(), buffer5.Cap()) 37 buffer5.Reset() 38 tt.Log(buffer5.Len(), buffer5.Cap()) 39 } 40 41 func BenchmarkPoolBytesPoolMinSize(b *testing.B) { 42 v := []byte("ok") 43 for i := 0; i < b.N; i++ { 44 var content = zutil.GetBuff(16) 45 content.Write(v) 46 str := content.Bytes() 47 zutil.PutBuff(content) 48 if string(v) != string(str) { 49 b.Fail() 50 } 51 } 52 } 53 54 func BenchmarkPoolBytesPoolMaxSize(b *testing.B) { 55 v := []byte("ok") 56 for i := 0; i < b.N; i++ { 57 var content = zutil.GetBuff(16) 58 content.Write(v) 59 str := content.Bytes() 60 zutil.PutBuff(content) 61 if string(v) != string(str) { 62 b.Fail() 63 } 64 } 65 } 66 67 func BenchmarkPoolBytesPool(b *testing.B) { 68 v := []byte("ok") 69 for i := 0; i < b.N; i++ { 70 var content = zutil.GetBuff() 71 content.Write(v) 72 str := content.Bytes() 73 zutil.PutBuff(content) 74 if string(v) != string(str) { 75 b.Fail() 76 } 77 } 78 } 79 80 func BenchmarkPoolBytes2(b *testing.B) { 81 v := []byte("ok") 82 for i := 0; i < b.N; i++ { 83 var str = &bytes.Buffer{} 84 str.Write(v) 85 if string(v) != str.String() { 86 b.Fail() 87 } 88 } 89 } 90 91 func BenchmarkPoolBytes3(b *testing.B) { 92 v := []byte("ok") 93 for i := 0; i < b.N; i++ { 94 str := zstring.Buffer() 95 str.Write(v) 96 if string(v) != str.String() { 97 b.Fail() 98 } 99 } 100 } 101 102 var sizeString = strings.Repeat("0", 1024*1024) 103 104 func BenchmarkPoolBytesPoolMinSize_max(b *testing.B) { 105 v := []byte(sizeString) 106 s := 16 107 for i := 0; i < b.N; i++ { 108 var content = zutil.GetBuff(s) 109 content.Write(v) 110 str := content.Bytes() 111 zutil.PutBuff(content) 112 if string(v) != string(str) { 113 b.Fail() 114 } 115 } 116 } 117 118 func BenchmarkPoolBytesPoolMaxSize_max(b *testing.B) { 119 v := []byte(sizeString) 120 s := 1024 * 1024 121 for i := 0; i < b.N; i++ { 122 var content = zutil.GetBuff(s) 123 content.Write(v) 124 str := content.Bytes() 125 zutil.PutBuff(content) 126 if string(v) != string(str) { 127 b.Fail() 128 } 129 } 130 } 131 132 func BenchmarkPoolBytesPool_max(b *testing.B) { 133 v := []byte(sizeString) 134 for i := 0; i < b.N; i++ { 135 var content = zutil.GetBuff() 136 content.Write(v) 137 str := content.Bytes() 138 zutil.PutBuff(content) 139 if string(v) != string(str) { 140 b.Fail() 141 } 142 } 143 } 144 145 func BenchmarkPoolBytes2_max(b *testing.B) { 146 v := []byte(sizeString) 147 for i := 0; i < b.N; i++ { 148 var str = &bytes.Buffer{} 149 str.Write(v) 150 if string(v) != str.String() { 151 b.Fail() 152 } 153 } 154 } 155 156 func BenchmarkPoolBytes3_max(b *testing.B) { 157 v := []byte(sizeString) 158 for i := 0; i < b.N; i++ { 159 str := zstring.Buffer() 160 str.Write(v) 161 if string(v) != str.String() { 162 b.Fail() 163 } 164 } 165 }