github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/prolly/tree/stats_test.go (about) 1 // Copyright 2023 Dolthub, Inc. 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 tree 16 17 import ( 18 "context" 19 "fmt" 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 24 "github.com/dolthub/dolt/go/store/val" 25 ) 26 27 func TestStatsLevel(t *testing.T) { 28 ctx := context.Background() 29 30 tests := []struct { 31 count int 32 level int 33 }{ 34 { 35 count: 10, 36 level: 0, 37 }, 38 { 39 count: 1e3, 40 level: 0, 41 }, 42 { 43 count: 1e6, 44 level: 1, 45 }, 46 } 47 lowBucketCnt := 20 48 hashFanout := 200 49 highBucketCnt := lowBucketCnt * hashFanout 50 for _, tt := range tests { 51 t.Run(fmt.Sprintf("stats histogram level test count: %d", tt.count), func(t *testing.T) { 52 root, _, ns := randomTree(t, tt.count*2) 53 m := StaticMap[val.Tuple, val.Tuple, val.TupleDesc]{ 54 Root: root, 55 NodeStore: ns, 56 Order: keyDesc, 57 } 58 levelNodes, err := GetHistogramLevel(ctx, m, lowBucketCnt) 59 require.NoError(t, err) 60 61 require.Equal(t, tt.level, levelNodes[0].Level()) 62 63 if root.level == 0 { 64 require.Equal(t, 1, len(levelNodes)) 65 } else if root.level == 1 { 66 require.Equal(t, root.Count(), len(levelNodes)) 67 } else { 68 if len(levelNodes) < lowBucketCnt || len(levelNodes) >= highBucketCnt { 69 t.Errorf("expected histogram bucket level to be in range: [%d,%d); found: %d", lowBucketCnt, highBucketCnt, len(levelNodes)) 70 } 71 } 72 require.Equal(t, tt.count, histLevelCount(t, levelNodes)) 73 }) 74 } 75 } 76 77 func histLevelCount(t *testing.T, nodes []Node) int { 78 cnt := 0 79 for _, n := range nodes { 80 switch n.level { 81 case 0: 82 cnt += n.Count() 83 default: 84 n, err := n.loadSubtrees() 85 require.NoError(t, err) 86 for i := 0; i < n.Count(); i++ { 87 subCnt, err := n.getSubtreeCount(i) 88 require.NoError(t, err) 89 cnt += int(subCnt) 90 } 91 } 92 } 93 return cnt 94 }