aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/datastore/buffer_type/buffer_type_test.cpp
blob: 4cd192f602f48c6487f477742d19f580f68cbadf (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
163
164
165
166
167
168
169
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/datastore/buffer_type.h>
#include <vespa/vespalib/testkit/testapp.h>

using namespace vespalib::datastore;

using IntBufferType = BufferType<int>;
constexpr uint32_t ARRAYS_SIZE(4);
constexpr uint32_t MAX_ARRAYS(128);
constexpr uint32_t NUM_ARRAYS_FOR_NEW_BUFFER(0);

struct Setup {
    uint32_t  _minArrays;
    ElemCount _usedElems;
    ElemCount _neededElems;
    ElemCount _deadElems;
    uint32_t  _bufferId;
    float     _allocGrowFactor;
    bool      _resizing;
    Setup()
        : _minArrays(0),
          _usedElems(0),
          _neededElems(0),
          _deadElems(0),
          _bufferId(1),
          _allocGrowFactor(0.5),
          _resizing(false)
    {}
    Setup &minArrays(uint32_t value) { _minArrays = value; return *this; }
    Setup &used(size_t value) { _usedElems = value; return *this; }
    Setup &needed(size_t value) { _neededElems = value; return *this; }
    Setup &dead(size_t value) { _deadElems = value; return *this; }
    Setup &bufferId(uint32_t value) { _bufferId = value; return *this; }
    Setup &resizing(bool value) { _resizing = value; return *this; }
};

struct Fixture {
    std::vector<Setup> setups;
    IntBufferType bufferType;
    int buffer[ARRAYS_SIZE];
    Fixture(const Setup &setup_)
        : setups(),
          bufferType(ARRAYS_SIZE, setup_._minArrays, MAX_ARRAYS, NUM_ARRAYS_FOR_NEW_BUFFER, setup_._allocGrowFactor),
          buffer()
    {
        setups.reserve(4);
        setups.push_back(setup_);
    }
    ~Fixture() {
        for (auto& setup : setups) {
            bufferType.onHold(setup._bufferId, &setup._usedElems, &setup._deadElems);
            bufferType.onFree(setup._usedElems);
        }
    }
    Setup& curr_setup() {
        return setups.back();
    }
    void add_setup(const Setup& setup_in) {
        // The buffer type stores pointers to ElemCount (from Setup) and we must ensure these do not move in memory.
        assert(setups.size() < setups.capacity());
        setups.push_back(setup_in);
    }
    void onActive() {
        bufferType.onActive(curr_setup()._bufferId, &curr_setup()._usedElems, &curr_setup()._deadElems, &buffer[0]);
    }
    size_t arraysToAlloc() {
        return bufferType.calcArraysToAlloc(curr_setup()._bufferId, curr_setup()._neededElems, curr_setup()._resizing);
    }
    void assertArraysToAlloc(size_t exp) {
        onActive();
        EXPECT_EQUAL(exp, arraysToAlloc());
    }
};

void
assertArraysToAlloc(size_t exp, const Setup &setup)
{
    Fixture f(setup);
    f.assertArraysToAlloc(exp);
}

TEST("require that complete arrays are allocated")
{
    TEST_DO(assertArraysToAlloc(1, Setup().needed(1)));
    TEST_DO(assertArraysToAlloc(1, Setup().needed(2)));
    TEST_DO(assertArraysToAlloc(1, Setup().needed(3)));
    TEST_DO(assertArraysToAlloc(1, Setup().needed(4)));
    TEST_DO(assertArraysToAlloc(2, Setup().needed(5)));
}

TEST("require that reserved elements are taken into account when not resizing")
{
    TEST_DO(assertArraysToAlloc(2, Setup().needed(1).bufferId(0)));
    TEST_DO(assertArraysToAlloc(2, Setup().needed(4).bufferId(0)));
    TEST_DO(assertArraysToAlloc(3, Setup().needed(5).bufferId(0)));
}

TEST("require that arrays to alloc is based on currently used elements (no resizing)")
{
    TEST_DO(assertArraysToAlloc(2, Setup().used(4 * 4).needed(4)));
    TEST_DO(assertArraysToAlloc(4, Setup().used(8 * 4).needed(4)));
}

TEST("require that arrays to alloc is based on currently used elements (with resizing)")
{
    TEST_DO(assertArraysToAlloc(4 + 2, Setup().used(4 * 4).needed(4).resizing(true)));
    TEST_DO(assertArraysToAlloc(8 + 4, Setup().used(8 * 4).needed(4).resizing(true)));
    TEST_DO(assertArraysToAlloc(4 + 3, Setup().used(4 * 4).needed(3 * 4).resizing(true)));
}

TEST("require that arrays to alloc always contain elements needed")
{
    TEST_DO(assertArraysToAlloc(2, Setup().used(4 * 4).needed(2 * 4)));
    TEST_DO(assertArraysToAlloc(3, Setup().used(4 * 4).needed(3 * 4)));
    TEST_DO(assertArraysToAlloc(4, Setup().used(4 * 4).needed(4 * 4)));
}

TEST("require that arrays to alloc is capped to max arrays")
{
    TEST_DO(assertArraysToAlloc(127, Setup().used(254 * 4).needed(4)));
    TEST_DO(assertArraysToAlloc(128, Setup().used(256 * 4).needed(4)));
    TEST_DO(assertArraysToAlloc(128, Setup().used(258 * 4).needed(8)));
}

TEST("require that arrays to alloc is capped to min arrays")
{
    TEST_DO(assertArraysToAlloc(16, Setup().used(30 * 4).needed(4).minArrays(16)));
    TEST_DO(assertArraysToAlloc(16, Setup().used(32 * 4).needed(4).minArrays(16)));
    TEST_DO(assertArraysToAlloc(17, Setup().used(34 * 4).needed(4).minArrays(16)));
}

TEST("arrays to alloc considers used elements across all active buffers of same type (no resizing)")
{
    Fixture f(Setup().used(6 * 4));
    f.assertArraysToAlloc(6 * 0.5);
    f.add_setup(Setup().used(8 * 4).bufferId(2));
    f.assertArraysToAlloc((6 + 8) * 0.5);
    f.add_setup(Setup().used(10 * 4).bufferId(3));
    f.assertArraysToAlloc((6 + 8 + 10) * 0.5);
}

TEST("arrays to alloc considers used elements across all active buffers of same type when resizing")
{
    Fixture f(Setup().used(6 * 4));
    f.assertArraysToAlloc(6 * 0.5);
    f.add_setup(Setup().used(8 * 4).resizing(true).bufferId(2));
    f.assertArraysToAlloc(8 + (6 + 8) * 0.5);
}

TEST("arrays to alloc considers (and subtracts) dead elements across all active buffers of same type (no resizing)")
{
    Fixture f(Setup().used(6 * 4).dead(2 * 4));
    f.assertArraysToAlloc((6 - 2) * 0.5);
    f.add_setup(Setup().used(12 * 4).dead(4 * 4).bufferId(2));
    f.assertArraysToAlloc((6 - 2 + 12 - 4) * 0.5);
    f.add_setup(Setup().used(20 * 4).dead(6 * 4).bufferId(3));
    f.assertArraysToAlloc((6 - 2 + 12 - 4 + 20 - 6) * 0.5);
}

TEST("arrays to alloc considers (and subtracts) dead elements across all active buffers of same type when resizing")
{
    Fixture f(Setup().used(6 * 4).dead(2 * 4));
    f.assertArraysToAlloc((6 - 2) * 0.5);
    f.add_setup(Setup().used(12 * 4).dead(4 * 4).resizing(true).bufferId(2));
    f.assertArraysToAlloc(12 + (6 - 2 + 12 - 4) * 0.5);
}

TEST_MAIN() { TEST_RUN_ALL(); }