github.com/matrixorigin/matrixone@v1.2.0/pkg/common/buffer/buffer_test.go (about) 1 // Copyright 2021 - 2023 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package buffer 16 17 import ( 18 "sync" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/pb/plan" 22 ) 23 24 const ( 25 NCPU = 100 26 Loop = 10 27 Query = 10 28 ) 29 30 func TestBuffer(t *testing.T) { 31 buf := New() 32 for i := 0; i < Loop; i++ { 33 n := Alloc[plan.Node](buf) 34 n.Limit = Alloc[plan.Expr](buf) 35 Free(buf, n.Limit) 36 Free(buf, n) 37 } 38 for i := 0; i < Loop; i++ { 39 ss := MakeSlice[*plan.Node](buf, 0, Query) 40 FreeSlice(buf, ss) 41 } 42 buf.Free() 43 } 44 45 func BenchmarkBuffer(b *testing.B) { 46 var wg sync.WaitGroup 47 48 buf := New() 49 for j := 0; j < NCPU; j++ { 50 wg.Add(1) 51 go func() { 52 defer wg.Done() 53 qry := make([]*plan.Node, 0, Query) 54 for i := 0; i < Loop; i++ { 55 qry = qry[:0] 56 for k := 0; k < Query; k++ { 57 n := Alloc[plan.Node](buf) 58 n.Limit = Alloc[plan.Expr](buf) 59 qry = append(qry, n) 60 } 61 for k := 0; k < Query; k++ { 62 Free(buf, qry[k].Limit) 63 Free(buf, qry[k]) 64 } 65 } 66 }() 67 } 68 wg.Wait() 69 buf.Free() 70 } 71 72 func BenchmarkNew(b *testing.B) { 73 var wg sync.WaitGroup 74 75 for j := 0; j < NCPU; j++ { 76 wg.Add(1) 77 go func() { 78 defer wg.Done() 79 qry := make([]*plan.Node, 0, Query) 80 for i := 0; i < Loop; i++ { 81 qry = qry[:0] 82 for k := 0; k < Query; k++ { 83 n := new(plan.Node) 84 n.Limit = new(plan.Expr) 85 qry = append(qry, n) 86 } 87 } 88 }() 89 } 90 wg.Wait() 91 }