cache_test.cpp 5.1 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
G
groot 已提交
7 8
#include "cache/CpuCacheMgr.h"
#include "cache/GpuCacheMgr.h"
G
groot 已提交
9

Y
yudong.cai 已提交
10
#include "utils/Error.h"
G
groot 已提交
11
#include "wrapper/Index.h"
S
starlord 已提交
12
#include "wrapper/knowhere/vec_index.h"
G
groot 已提交
13

J
jinhai 已提交
14
using namespace zilliz::milvus;
G
groot 已提交
15

G
groot 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
namespace {

class InvalidCacheMgr : public cache::CacheMgr {
public:
    InvalidCacheMgr() {
    }
};

class LessItemCacheMgr : public cache::CacheMgr {
public:
    LessItemCacheMgr() {
        cache_ = std::make_shared<cache::Cache>(1UL << 12, 10);
    }
};

S
starlord 已提交
31 32
class MockVecIndex : public engine::VecIndex {
public:
Y
yudong.cai 已提交
33
    virtual server::KnowhereError BuildAll(const long &nb,
S
starlord 已提交
34 35 36 37 38 39 40 41
                          const float *xb,
                          const long *ids,
                          const engine::Config &cfg,
                          const long &nt = 0,
                          const float *xt = nullptr) {

    }

X
xj.lin 已提交
42 43 44 45
    engine::IndexType GetType() override {
        return engine::IndexType::INVALID;
    }

Y
yudong.cai 已提交
46
    virtual server::KnowhereError Add(const long &nb,
S
starlord 已提交
47 48 49 50 51 52
                     const float *xb,
                     const long *ids,
                     const engine::Config &cfg = engine::Config()) {

    }

Y
yudong.cai 已提交
53
    virtual server::KnowhereError Search(const long &nq,
S
starlord 已提交
54 55 56 57 58 59 60
                        const float *xq,
                        float *dist,
                        long *ids,
                        const engine::Config &cfg = engine::Config()) {

    }

X
xj.lin 已提交
61 62 63 64 65 66 67 68
    engine::VecIndexPtr CopyToGpu(const int64_t &device_id, const engine::Config &cfg) override {

    }

    engine::VecIndexPtr CopyToCpu(const engine::Config &cfg) override {

    }

S
starlord 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
    virtual int64_t Dimension() {
        return dimension_;
    }

    virtual int64_t Count() {
        return ntotal_;
    }

    virtual zilliz::knowhere::BinarySet Serialize() {
        zilliz::knowhere::BinarySet binset;
        return binset;
    }

Y
yudong.cai 已提交
82
    virtual server::KnowhereError Load(const zilliz::knowhere::BinarySet &index_binary) {
S
starlord 已提交
83 84 85 86 87 88 89 90

    }

public:
    int64_t dimension_ = 512;
    int64_t ntotal_ = 0;
};

G
groot 已提交
91 92
}

G
groot 已提交
93 94
TEST(CacheTest, CPU_CACHE_TEST) {
    cache::CacheMgr *cpu_mgr = cache::CpuCacheMgr::GetInstance();
G
groot 已提交
95

G
groot 已提交
96 97
    const int64_t gbyte = 1 << 30;
    int64_t g_num = 16;
G
groot 已提交
98
    int64_t cap = g_num * gbyte;
G
groot 已提交
99 100 101 102 103
    cpu_mgr->SetCapacity(cap);
    ASSERT_EQ(cpu_mgr->CacheCapacity(), cap);

    const int dim = 256;

G
groot 已提交
104
    for (int i = 0; i < 20; i++) {
S
starlord 已提交
105 106 107
        MockVecIndex* mock_index = new MockVecIndex();
        mock_index->ntotal_ = 1000000;//less 1G per index
        engine::Index_ptr index(mock_index);
G
groot 已提交
108 109 110 111 112

        cpu_mgr->InsertItem("index_" + std::to_string(i), index);
    }
    ASSERT_LT(cpu_mgr->ItemCount(), g_num);

G
groot 已提交
113 114 115 116 117 118
    auto obj = cpu_mgr->GetIndex("index_0");
    ASSERT_TRUE(obj == nullptr);

    obj = cpu_mgr->GetIndex("index_19");
    ASSERT_TRUE(obj != nullptr);

G
groot 已提交
119 120 121 122 123 124 125 126 127 128 129
    {
        std::string item = "index_15";
        ASSERT_TRUE(cpu_mgr->ItemExists(item));
        cpu_mgr->EraseItem(item);
        ASSERT_FALSE(cpu_mgr->ItemExists(item));
    }

    {
        g_num = 5;
        cpu_mgr->SetCapacity(g_num * gbyte);

S
starlord 已提交
130 131 132
        MockVecIndex* mock_index = new MockVecIndex();
        mock_index->ntotal_ = 6000000;//6G less
        engine::Index_ptr index(mock_index);
G
groot 已提交
133

G
groot 已提交
134 135 136
        cpu_mgr->InsertItem("index_6g", index);
        ASSERT_EQ(cpu_mgr->ItemCount(), 0);//data greater than capacity can not be inserted sucessfully
    }
G
groot 已提交
137 138 139 140 141 142 143 144 145 146

    cpu_mgr->PrintInfo();
}

TEST(CacheTest, GPU_CACHE_TEST) {
    cache::CacheMgr* gpu_mgr = cache::GpuCacheMgr::GetInstance();

    const int dim = 256;

    for(int i = 0; i < 20; i++) {
S
starlord 已提交
147 148 149
        MockVecIndex* mock_index = new MockVecIndex();
        mock_index->ntotal_ = 1000;
        engine::Index_ptr index(mock_index);
G
groot 已提交
150 151 152 153 154 155 156 157 158 159

        cache::DataObjPtr obj = std::make_shared<cache::DataObj>(index);

        gpu_mgr->InsertItem("index_" + std::to_string(i), obj);
    }

    auto obj = gpu_mgr->GetItem("index_0");

    gpu_mgr->ClearCache();
    ASSERT_EQ(gpu_mgr->ItemCount(), 0);
G
groot 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

TEST(CacheTest, INVALID_TEST) {
    {
        InvalidCacheMgr mgr;
        ASSERT_EQ(mgr.ItemCount(), 0);
        ASSERT_FALSE(mgr.ItemExists("test"));
        ASSERT_EQ(mgr.GetItem("test"), nullptr);

        mgr.InsertItem("test", cache::DataObjPtr());
        mgr.InsertItem("test", engine::Index_ptr(nullptr));
        mgr.EraseItem("test");
        mgr.PrintInfo();
        mgr.ClearCache();
        mgr.SetCapacity(100);
        ASSERT_EQ(mgr.CacheCapacity(), 0);
        ASSERT_EQ(mgr.CacheUsage(), 0);
    }

    {
        LessItemCacheMgr mgr;
        for(int i = 0; i < 20; i++) {
S
starlord 已提交
182 183 184
            MockVecIndex* mock_index = new MockVecIndex();
            mock_index->ntotal_ = 2;
            engine::Index_ptr index(mock_index);
G
groot 已提交
185 186 187 188 189 190

            cache::DataObjPtr obj = std::make_shared<cache::DataObj>(index);
            mgr.InsertItem("index_" + std::to_string(i), obj);
        }
        ASSERT_EQ(mgr.GetItem("index_0"), nullptr);
    }
G
groot 已提交
191
}