cache_test.cpp 4.8 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

G
groot 已提交
10
#include "wrapper/Index.h"
S
starlord 已提交
11
#include "wrapper/knowhere/vec_index.h"
G
groot 已提交
12

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

G
groot 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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 已提交
30 31 32 33 34 35 36 37 38 39 40
class MockVecIndex : public engine::VecIndex {
public:
    virtual void BuildAll(const long &nb,
                          const float *xb,
                          const long *ids,
                          const engine::Config &cfg,
                          const long &nt = 0,
                          const float *xt = nullptr) {

    }

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

S
starlord 已提交
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
    virtual void Add(const long &nb,
                     const float *xb,
                     const long *ids,
                     const engine::Config &cfg = engine::Config()) {

    }

    virtual void Search(const long &nq,
                        const float *xq,
                        float *dist,
                        long *ids,
                        const engine::Config &cfg = engine::Config()) {

    }

    virtual int64_t Dimension() {
        return dimension_;
    }

    virtual int64_t Count() {
        return ntotal_;
    }

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

    virtual void Load(const zilliz::knowhere::BinarySet &index_binary) {

    }

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

G
groot 已提交
82 83
}

G
groot 已提交
84 85
TEST(CacheTest, CPU_CACHE_TEST) {
    cache::CacheMgr *cpu_mgr = cache::CpuCacheMgr::GetInstance();
G
groot 已提交
86

G
groot 已提交
87 88
    const int64_t gbyte = 1 << 30;
    int64_t g_num = 16;
G
groot 已提交
89
    int64_t cap = g_num * gbyte;
G
groot 已提交
90 91 92 93 94
    cpu_mgr->SetCapacity(cap);
    ASSERT_EQ(cpu_mgr->CacheCapacity(), cap);

    const int dim = 256;

G
groot 已提交
95
    for (int i = 0; i < 20; i++) {
S
starlord 已提交
96 97 98
        MockVecIndex* mock_index = new MockVecIndex();
        mock_index->ntotal_ = 1000000;//less 1G per index
        engine::Index_ptr index(mock_index);
G
groot 已提交
99 100 101 102 103

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

G
groot 已提交
104 105 106 107 108 109
    auto obj = cpu_mgr->GetIndex("index_0");
    ASSERT_TRUE(obj == nullptr);

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

G
groot 已提交
110 111 112 113 114 115 116 117 118 119 120
    {
        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 已提交
121 122 123
        MockVecIndex* mock_index = new MockVecIndex();
        mock_index->ntotal_ = 6000000;//6G less
        engine::Index_ptr index(mock_index);
G
groot 已提交
124

G
groot 已提交
125 126 127
        cpu_mgr->InsertItem("index_6g", index);
        ASSERT_EQ(cpu_mgr->ItemCount(), 0);//data greater than capacity can not be inserted sucessfully
    }
G
groot 已提交
128 129 130 131 132 133 134 135 136 137

    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 已提交
138 139 140
        MockVecIndex* mock_index = new MockVecIndex();
        mock_index->ntotal_ = 1000;
        engine::Index_ptr index(mock_index);
G
groot 已提交
141 142 143 144 145 146 147 148 149 150

        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 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

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 已提交
173 174 175
            MockVecIndex* mock_index = new MockVecIndex();
            mock_index->ntotal_ = 2;
            engine::Index_ptr index(mock_index);
G
groot 已提交
176 177 178 179 180 181

            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 已提交
182
}