github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/fifocache/bench_test.go (about) 1 // Copyright 2024 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 fifocache 16 17 import ( 18 "testing" 19 ) 20 21 func BenchmarkSequentialSet(b *testing.B) { 22 size := 65536 23 cache := New[int, int](size, nil, ShardInt[int]) 24 nElements := size * 16 25 b.ResetTimer() 26 for i := 0; i < b.N; i++ { 27 cache.Set(i%nElements, i, 1+i%3) 28 } 29 } 30 31 func BenchmarkParallelSet(b *testing.B) { 32 size := 65536 33 cache := New[int, int](size, nil, ShardInt[int]) 34 nElements := size * 16 35 b.ResetTimer() 36 b.RunParallel(func(pb *testing.PB) { 37 for i := 0; pb.Next(); i++ { 38 cache.Set(i%nElements, i, 1+i%3) 39 } 40 }) 41 } 42 43 func BenchmarkGet(b *testing.B) { 44 size := 65536 45 cache := New[int, int](size, nil, ShardInt[int]) 46 nElements := size * 16 47 for i := 0; i < nElements; i++ { 48 cache.Set(i, i, 1+i%3) 49 } 50 b.ResetTimer() 51 for i := 0; i < b.N; i++ { 52 cache.Get(i % nElements) 53 } 54 } 55 56 func BenchmarkParallelGet(b *testing.B) { 57 size := 65536 58 cache := New[int, int](size, nil, ShardInt[int]) 59 nElements := size * 16 60 for i := 0; i < nElements; i++ { 61 cache.Set(i, i, 1+i%3) 62 } 63 b.ResetTimer() 64 b.RunParallel(func(pb *testing.PB) { 65 for i := 0; pb.Next(); i++ { 66 cache.Get(i % nElements) 67 } 68 }) 69 } 70 71 func BenchmarkParallelGetOrSet(b *testing.B) { 72 size := 65536 73 cache := New[int, int](size, nil, ShardInt[int]) 74 nElements := size * 16 75 b.ResetTimer() 76 b.RunParallel(func(pb *testing.PB) { 77 for i := 0; pb.Next(); i++ { 78 if i%2 == 0 { 79 cache.Get(i % nElements) 80 } else { 81 cache.Set(i%nElements, i, 1+i%3) 82 } 83 } 84 }) 85 }