blob: c27240026d2bbbac425d122662666eadd32c6858 [file] [log] [blame]
Ibrahim Jariffd599072019-03-07 20:08:301/*
2 * Copyright 2019 Dgraph Labs, Inc. and Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package badger
18
19import (
20 "testing"
21
22 "github.com/stretchr/testify/require"
23)
24
25func TestBuildKeyValueSizeHistogram(t *testing.T) {
26 t.Run("All same size key-values", func(t *testing.T) {
27 runBadgerTest(t, nil, func(t *testing.T, db *DB) {
28 entries := int64(40)
29 err := db.Update(func(txn *Txn) error {
Damien Tournoud543f3532020-06-02 17:23:4630 for i := rune(0); i < rune(entries); i++ {
Ashish Goswamie9447c92019-05-28 05:28:3731 err := txn.SetEntry(NewEntry([]byte(string(i)), []byte("B")))
Ibrahim Jariffd599072019-03-07 20:08:3032 if err != nil {
33 return err
34 }
35 }
36 return nil
37 })
38 require.NoError(t, err)
39
Manish R Jained1c3572019-03-08 03:46:1940 histogram := db.buildHistogram(nil)
Ibrahim Jariffd599072019-03-07 20:08:3041 keyHistogram := histogram.keySizeHistogram
42 valueHistogram := histogram.valueSizeHistogram
43
44 require.Equal(t, entries, keyHistogram.totalCount)
45 require.Equal(t, entries, valueHistogram.totalCount)
46
47 // Each entry is of size one. So the sum of sizes should be the same
48 // as number of entries
49 require.Equal(t, entries, valueHistogram.sum)
50 require.Equal(t, entries, keyHistogram.sum)
51
52 // All value sizes are same. The first bin should have all the values.
53 require.Equal(t, entries, valueHistogram.countPerBin[0])
54 require.Equal(t, entries, keyHistogram.countPerBin[0])
55
56 require.Equal(t, int64(1), keyHistogram.max)
57 require.Equal(t, int64(1), keyHistogram.min)
58 require.Equal(t, int64(1), valueHistogram.max)
59 require.Equal(t, int64(1), valueHistogram.min)
60 })
61 })
62
63 t.Run("different size key-values", func(t *testing.T) {
64 runBadgerTest(t, nil, func(t *testing.T, db *DB) {
65 entries := int64(3)
66 err := db.Update(func(txn *Txn) error {
Ashish Goswamie9447c92019-05-28 05:28:3767 if err := txn.SetEntry(NewEntry([]byte("A"), []byte("B"))); err != nil {
Ibrahim Jariffd599072019-03-07 20:08:3068 return err
69 }
70
Ashish Goswamie9447c92019-05-28 05:28:3771 if err := txn.SetEntry(NewEntry([]byte("AA"), []byte("BB"))); err != nil {
Ibrahim Jariffd599072019-03-07 20:08:3072 return err
73 }
74
Ashish Goswamie9447c92019-05-28 05:28:3775 return txn.SetEntry(NewEntry([]byte("AAA"), []byte("BBB")))
Ibrahim Jariffd599072019-03-07 20:08:3076 })
77 require.NoError(t, err)
78
Manish R Jained1c3572019-03-08 03:46:1979 histogram := db.buildHistogram(nil)
Ibrahim Jariffd599072019-03-07 20:08:3080 keyHistogram := histogram.keySizeHistogram
81 valueHistogram := histogram.valueSizeHistogram
82
83 require.Equal(t, entries, keyHistogram.totalCount)
84 require.Equal(t, entries, valueHistogram.totalCount)
85
86 // Each entry is of size one. So the sum of sizes should be the same
87 // as number of entries
88 require.Equal(t, int64(6), valueHistogram.sum)
89 require.Equal(t, int64(6), keyHistogram.sum)
90
Martin Martinez Riveraa6381bb2019-04-19 00:58:5191 // Length 1 key is in first bucket, length 2 and 3 are in the second
Ibrahim Jariffd599072019-03-07 20:08:3092 // bucket
93 require.Equal(t, int64(1), valueHistogram.countPerBin[0])
94 require.Equal(t, int64(2), valueHistogram.countPerBin[1])
95 require.Equal(t, int64(1), keyHistogram.countPerBin[0])
96 require.Equal(t, int64(2), keyHistogram.countPerBin[1])
97
98 require.Equal(t, int64(3), keyHistogram.max)
99 require.Equal(t, int64(1), keyHistogram.min)
100 require.Equal(t, int64(3), valueHistogram.max)
101 require.Equal(t, int64(1), valueHistogram.min)
102 })
103 })
104}