Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package badger |
| 18 | |
| 19 | import ( |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/stretchr/testify/require" |
| 23 | ) |
| 24 | |
| 25 | func 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 Tournoud | 543f353 | 2020-06-02 17:23:46 | [diff] [blame] | 30 | for i := rune(0); i < rune(entries); i++ { |
Ashish Goswami | e9447c9 | 2019-05-28 05:28:37 | [diff] [blame] | 31 | err := txn.SetEntry(NewEntry([]byte(string(i)), []byte("B"))) |
Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | } |
| 36 | return nil |
| 37 | }) |
| 38 | require.NoError(t, err) |
| 39 | |
Manish R Jain | ed1c357 | 2019-03-08 03:46:19 | [diff] [blame] | 40 | histogram := db.buildHistogram(nil) |
Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 41 | 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 Goswami | e9447c9 | 2019-05-28 05:28:37 | [diff] [blame] | 67 | if err := txn.SetEntry(NewEntry([]byte("A"), []byte("B"))); err != nil { |
Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 68 | return err |
| 69 | } |
| 70 | |
Ashish Goswami | e9447c9 | 2019-05-28 05:28:37 | [diff] [blame] | 71 | if err := txn.SetEntry(NewEntry([]byte("AA"), []byte("BB"))); err != nil { |
Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 72 | return err |
| 73 | } |
| 74 | |
Ashish Goswami | e9447c9 | 2019-05-28 05:28:37 | [diff] [blame] | 75 | return txn.SetEntry(NewEntry([]byte("AAA"), []byte("BBB"))) |
Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 76 | }) |
| 77 | require.NoError(t, err) |
| 78 | |
Manish R Jain | ed1c357 | 2019-03-08 03:46:19 | [diff] [blame] | 79 | histogram := db.buildHistogram(nil) |
Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 80 | 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 Rivera | a6381bb | 2019-04-19 00:58:51 | [diff] [blame] | 91 | // Length 1 key is in first bucket, length 2 and 3 are in the second |
Ibrahim Jarif | fd59907 | 2019-03-07 20:08:30 | [diff] [blame] | 92 | // 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 | } |