github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/io_merger_test.go (about) 1 // Copyright 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 fileservice 16 17 import ( 18 "sync" 19 "testing" 20 ) 21 22 func TestIOMerger(t *testing.T) { 23 merger := NewIOMerger() 24 n := 1024 25 key := IOMergeKey{ 26 Path: "foo", 27 } 28 29 wg := new(sync.WaitGroup) 30 wg.Add(n) 31 var c int 32 var cs []int 33 for i := 0; i < n; i++ { 34 go func() { 35 defer wg.Done() 36 for { 37 done, wait := merger.Merge(key) 38 if done != nil { 39 cs = append(cs, c) 40 c++ 41 done() 42 return 43 } else { 44 wait() 45 } 46 } 47 }() 48 } 49 wg.Wait() 50 51 if c != 1024 { 52 t.Fatal() 53 } 54 55 // should be no race and sequential 56 for i, c := range cs { 57 if c != i { 58 t.Fatalf("got %v", cs) 59 } 60 } 61 } 62 63 func BenchmarkIOMergerNoContention(b *testing.B) { 64 merger := NewIOMerger() 65 key := IOMergeKey{ 66 Path: "foo", 67 } 68 b.ResetTimer() 69 for i := 0; i < b.N; i++ { 70 done, wait := merger.Merge(key) 71 if done != nil { 72 done() 73 } else { 74 wait() 75 } 76 } 77 } 78 79 func BenchmarkIOMergerParallel(b *testing.B) { 80 merger := NewIOMerger() 81 key := IOMergeKey{ 82 Path: "foo", 83 } 84 b.ResetTimer() 85 b.RunParallel(func(pb *testing.PB) { 86 for pb.Next() { 87 done, wait := merger.Merge(key) 88 if done != nil { 89 done() 90 } else { 91 wait() 92 } 93 } 94 }) 95 }