github.com/dubbogo/gost@v1.14.0/bytes/bytes_pool_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package gxbytes 19 20 import ( 21 "fmt" 22 "testing" 23 ) 24 25 import ( 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func Test_findIndex(t *testing.T) { 30 bp := NewBytesPool([]int{16, 4 << 10, 16 << 10, 32 << 10, 64 << 10}) 31 32 type args struct { 33 size int 34 } 35 tests := []struct { 36 args args 37 want int 38 }{ 39 {args{1}, 0}, 40 {args{15}, 0}, 41 {args{16}, 0}, 42 {args{17}, 1}, 43 {args{4095}, 1}, 44 {args{4096}, 1}, 45 {args{4097}, 2}, 46 {args{16 << 10}, 2}, 47 {args{64 << 10}, 4}, 48 {args{64 << 11}, 5}, 49 } 50 for _, tt := range tests { 51 t.Run(fmt.Sprint(tt.args.size), func(t *testing.T) { 52 if got := bp.findIndex(tt.args.size); got != tt.want { 53 t.Errorf("[%v] findIndex() = %v, want %v", tt.args.size, got, tt.want) 54 } 55 }) 56 } 57 } 58 59 func BenchmarkAcquireBytesSize32(b *testing.B) { benchmarkAcquireBytes(b, 32) } 60 func BenchmarkAcquireBytesSize10k(b *testing.B) { benchmarkAcquireBytes(b, 10000) } 61 func BenchmarkAcquireBytesSize60k(b *testing.B) { benchmarkAcquireBytes(b, 60000) } 62 func BenchmarkAcquireBytesSize70k(b *testing.B) { benchmarkAcquireBytes(b, 70000) } 63 64 func benchmarkAcquireBytes(b *testing.B, size int) { 65 for i := 0; i < b.N; i++ { 66 bytes := AcquireBytes(size) 67 ReleaseBytes(bytes) 68 } 69 } 70 71 func BenchmarkFindIndexSize8(b *testing.B) { benchmarkfindIndex(b, 8) } 72 func BenchmarkFindIndexSize60k(b *testing.B) { benchmarkfindIndex(b, 60000) } 73 74 func benchmarkfindIndex(b *testing.B, size int) { 75 for i := 0; i < b.N; i++ { 76 defaultBytesPool.findIndex(size) 77 } 78 } 79 80 func TestAcquireBytes(t *testing.T) { 81 bytes := AcquireBytes(10) 82 assert.Equal(t, 10, len(*bytes)) 83 assert.Equal(t, 512, cap(*bytes)) 84 85 bytes3 := AcquireBytes(1000000) 86 assert.Equal(t, 1000000, cap(*bytes3)) 87 assert.Equal(t, 1000000, cap(*bytes3)) 88 }