aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/documentmetastore/lid_allocator/lid_allocator_test.cpp
blob: 2d675e82db2265ede6216d443c760cb17383ca09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/searchcore/proton/documentmetastore/lid_allocator.h>
#include <vespa/vespalib/util/generationholder.h>
#include <vespa/vespalib/util/time.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <iostream>

using vespalib::GenerationHolder;
using vespalib::Timer;

namespace proton {

using documentmetastore::LidAllocator;

class LidAllocatorTest : public ::testing::Test
{
protected:
    GenerationHolder _gen_hold;
    LidAllocator     _allocator;

    LidAllocatorTest()
        : ::testing::Test(),
          _gen_hold(),
          _allocator(100, 100, _gen_hold)
    {
    }

    ~LidAllocatorTest()
    {
        _gen_hold.clearHoldLists();
    }

    uint32_t get_size() { return _allocator.getActiveLids().size(); }

    void construct_free_list() {
        _allocator.constructFreeList(_allocator.getActiveLids().size());
        _allocator.setFreeListConstructed();
    }

    void register_lids(const std::vector<uint32_t>& lids) {
        for (uint32_t lid : lids) {
            _allocator.registerLid(lid);
        }
    }

    std::vector<uint32_t> alloc_lids(uint32_t count) {
        std::vector<uint32_t> result;
        for (uint32_t i = 0; i < count; ++i) {
            result.emplace_back(_allocator.getFreeLid(get_size()));
        }
        return result;
    }

    void activate_lids(const std::vector<uint32_t>& lids, bool active) {
        for (uint32_t lid : lids) {
            _allocator.updateActiveLids(lid, active);
        }
    }
    
    void unregister_lids(const std::vector<uint32_t>& lids) {
         _allocator.unregister_lids(lids);
    }

    void hold_lids(const std::vector<uint32_t>& lids) {
        _allocator.holdLids(lids, get_size(), 0);
    }

    void trim_hold_lists() {
        _allocator.trimHoldLists(1);
    }
    
    std::vector<uint32_t> get_valid_lids() {
        std::vector<uint32_t> result;
        auto size = get_size();
        for (uint32_t lid = 1; lid < size; ++lid) {
            if (_allocator.validLid(lid)) {
                result.emplace_back(lid);
            }
        }
        return result;
    }

    std::vector<uint32_t> get_active_lids() {
        std::vector<uint32_t> result;
        const auto &active_lids = _allocator.getActiveLids();
        uint32_t lid = active_lids.getNextTrueBit(1);
        while (lid < active_lids.size()) {
            if (active_lids.testBit(lid)) {
                result.emplace_back(lid);
            }
            lid = active_lids.getNextTrueBit(lid + 1);
        }
        return result;
    }

    void
    assert_valid_lids(const std::vector<uint32_t>& exp_lids) {
        EXPECT_EQ(exp_lids, get_valid_lids());
    }

    void
    assert_active_lids(const std::vector<uint32_t>& exp_lids) {
        EXPECT_EQ(exp_lids, get_active_lids());
    }

};

TEST_F(LidAllocatorTest, unregister_lids)
{
    register_lids({ 1, 2, 3, 4, 5, 6 });
    activate_lids({ 4, 5, 6 }, true);
    assert_valid_lids({1, 2, 3, 4, 5, 6});
    assert_active_lids({4, 5, 6});
    construct_free_list();
    unregister_lids({1, 3, 5});
    assert_valid_lids({2, 4, 6});
    assert_active_lids({4, 6});
    hold_lids({1, 3, 5});
    trim_hold_lists();
    EXPECT_EQ((std::vector<uint32_t>{1, 3, 5, 7, 8}), alloc_lids(5));
}

class LidAllocatorPerformanceTest : public LidAllocatorTest,
                                    public testing::WithParamInterface<bool>
{
};

TEST_P(LidAllocatorPerformanceTest, unregister_lids_performance)
{
    constexpr uint32_t test_size = 1000000;
    _allocator.ensureSpace(test_size + 1, test_size + 1);
    std::vector<std::vector<uint32_t>> buckets;
    buckets.resize(1000);
    auto reserve_size = (test_size + (buckets.size() - 1)) / buckets.size(); 
for (auto& bucket : buckets) {
    bucket.reserve(reserve_size);
}
    for (uint32_t i = 0; i < test_size; ++i) {
        _allocator.registerLid(i + 1);
        buckets[i % buckets.size()].emplace_back(i + 1);
    }
    construct_free_list();
    Timer timer;
    for (auto& bucket: buckets) {
        if (GetParam()) {
            unregister_lids(bucket);
        } else {
            for (auto lid : bucket) {
                _allocator.unregisterLid(lid);
            }
        }
    }
    auto rate = test_size / vespalib::to_s(timer.elapsed());
    std::cout << "Unregister rate: " << std::fixed << rate << std::endl;
}

VESPA_GTEST_INSTANTIATE_TEST_SUITE_P(LidAllocatorParameterizedPerformanceTest, LidAllocatorPerformanceTest, testing::Values(false, true));

}

GTEST_MAIN_RUN_ALL_TESTS()