cache_test.cpp 4.7 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 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
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) {

    }

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

G
groot 已提交
80 81
TEST(CacheTest, CPU_CACHE_TEST) {
    cache::CacheMgr *cpu_mgr = cache::CpuCacheMgr::GetInstance();
G
groot 已提交
82

G
groot 已提交
83 84
    const int64_t gbyte = 1 << 30;
    int64_t g_num = 16;
G
groot 已提交
85
    int64_t cap = g_num * gbyte;
G
groot 已提交
86 87 88 89 90
    cpu_mgr->SetCapacity(cap);
    ASSERT_EQ(cpu_mgr->CacheCapacity(), cap);

    const int dim = 256;

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

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

G
groot 已提交
100 101 102 103 104 105
    auto obj = cpu_mgr->GetIndex("index_0");
    ASSERT_TRUE(obj == nullptr);

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

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

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

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

        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 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

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

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