github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/stores/tsdb/index/postingsstats_test.go (about) 1 // Copyright 2019 The Prometheus Authors 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 package index 14 15 import ( 16 "testing" 17 18 "github.com/stretchr/testify/require" 19 ) 20 21 func TestPostingsStats(t *testing.T) { 22 stats := &maxHeap{} 23 max := 3000000 24 heapLength := 10 25 stats.init(heapLength) 26 for i := 0; i < max; i++ { 27 item := Stat{ 28 Name: "Label-da", 29 Count: uint64(i), 30 } 31 stats.push(item) 32 } 33 stats.push(Stat{Name: "Stuff", Count: 3000000}) 34 35 data := stats.get() 36 require.Equal(t, 10, len(data)) 37 for i := 0; i < heapLength; i++ { 38 require.Equal(t, uint64(max-i), data[i].Count) 39 } 40 } 41 42 func TestPostingsStats2(t *testing.T) { 43 stats := &maxHeap{} 44 heapLength := 10 45 46 stats.init(heapLength) 47 stats.push(Stat{Name: "Stuff", Count: 10}) 48 stats.push(Stat{Name: "Stuff", Count: 11}) 49 stats.push(Stat{Name: "Stuff", Count: 1}) 50 stats.push(Stat{Name: "Stuff", Count: 6}) 51 52 data := stats.get() 53 54 require.Equal(t, 4, len(data)) 55 require.Equal(t, uint64(11), data[0].Count) 56 } 57 58 func BenchmarkPostingStatsMaxHep(b *testing.B) { 59 stats := &maxHeap{} 60 max := 9000000 61 heapLength := 10 62 b.ResetTimer() 63 for n := 0; n < b.N; n++ { 64 stats.init(heapLength) 65 for i := 0; i < max; i++ { 66 item := Stat{ 67 Name: "Label-da", 68 Count: uint64(i), 69 } 70 stats.push(item) 71 } 72 stats.get() 73 } 74 }