db_tests.cpp 7.5 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>
X
Xu Peng 已提交
7 8
#include <thread>
#include <easylogging++.h>
G
groot 已提交
9

X
Xu Peng 已提交
10
#include "utils.h"
G
groot 已提交
11
#include "db/DB.h"
X
Xu Peng 已提交
12
#include "db/DBImpl.h"
X
Xu Peng 已提交
13
#include "db/MetaConsts.h"
G
groot 已提交
14 15 16

using namespace zilliz::vecwise;

X
Xu Peng 已提交
17 18
TEST_F(DBTest, CONFIG_TEST) {
    {
19 20
        ASSERT_ANY_THROW(engine::ArchiveConf conf("wrong"));
        /* EXPECT_DEATH(engine::ArchiveConf conf("wrong"), ""); */
X
Xu Peng 已提交
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
    }
    {
        engine::ArchiveConf conf("delete");
        ASSERT_EQ(conf.GetType(), "delete");
        auto criterias = conf.GetCriterias();
        ASSERT_TRUE(criterias.size() == 1);
        ASSERT_TRUE(criterias["disk"] == 512);
    }
    {
        engine::ArchiveConf conf("swap");
        ASSERT_EQ(conf.GetType(), "swap");
        auto criterias = conf.GetCriterias();
        ASSERT_TRUE(criterias.size() == 1);
        ASSERT_TRUE(criterias["disk"] == 512);
    }
    {
        ASSERT_ANY_THROW(engine::ArchiveConf conf1("swap", "disk:"));
        ASSERT_ANY_THROW(engine::ArchiveConf conf2("swap", "disk:a"));
        engine::ArchiveConf conf("swap", "disk:1024");
        auto criterias = conf.GetCriterias();
        ASSERT_TRUE(criterias.size() == 1);
        ASSERT_TRUE(criterias["disk"] == 1024);
    }
    {
        ASSERT_ANY_THROW(engine::ArchiveConf conf1("swap", "days:"));
        ASSERT_ANY_THROW(engine::ArchiveConf conf2("swap", "days:a"));
        engine::ArchiveConf conf("swap", "days:100");
        auto criterias = conf.GetCriterias();
        ASSERT_TRUE(criterias.size() == 1);
        ASSERT_TRUE(criterias["days"] == 100);
    }
    {
        ASSERT_ANY_THROW(engine::ArchiveConf conf1("swap", "days:"));
        ASSERT_ANY_THROW(engine::ArchiveConf conf2("swap", "days:a"));
        engine::ArchiveConf conf("swap", "days:100;disk:200");
        auto criterias = conf.GetCriterias();
        ASSERT_TRUE(criterias.size() == 2);
        ASSERT_TRUE(criterias["days"] == 100);
        ASSERT_TRUE(criterias["disk"] == 200);
    }
}

X
Xu Peng 已提交
63 64 65 66
TEST_F(DBTest2, ARHIVE_DISK_CHECK) {

    static const std::string group_name = "test_group";
    static const int group_dim = 256;
X
Xu Peng 已提交
67
    long size;
X
Xu Peng 已提交
68

69
    engine::meta::TableSchema group_info;
X
Xu Peng 已提交
70
    group_info.dimension = group_dim;
71
    group_info.table_id = group_name;
X
Xu Peng 已提交
72 73
    engine::Status stat = db_->add_group(group_info);

74
    engine::meta::TableSchema group_info_get;
75
    group_info_get.table_id = group_name;
X
Xu Peng 已提交
76 77 78 79 80 81 82
    stat = db_->get_group(group_info_get);
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    engine::IDNumbers vector_ids;
    engine::IDNumbers target_ids;

X
Xu Peng 已提交
83
    db_->size(size);
X
Xu Peng 已提交
84
    int d = 256;
X
Xu Peng 已提交
85
    int nb = 20;
X
Xu Peng 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    float *xb = new float[d * nb];
    for(int i = 0; i < nb; i++) {
        for(int j = 0; j < d; j++) xb[d * i + j] = drand48();
        xb[d * i] += i / 2000.;
    }

    int loop = 100000;

    for (auto i=0; i<loop; ++i) {
        db_->add_vectors(group_name, nb, xb, vector_ids);
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }

    std::this_thread::sleep_for(std::chrono::seconds(1));

    db_->size(size);
X
Xu Peng 已提交
102 103
    LOG(DEBUG) << "size=" << size;
    ASSERT_TRUE(size < 1 * engine::meta::G);
X
Xu Peng 已提交
104 105 106 107 108

    delete [] xb;
};


109 110
TEST_F(DBTest, DB_TEST) {

G
groot 已提交
111 112 113
    static const std::string group_name = "test_group";
    static const int group_dim = 256;

114
    engine::meta::TableSchema group_info;
G
groot 已提交
115
    group_info.dimension = group_dim;
116
    group_info.table_id = group_name;
X
Xu Peng 已提交
117
    engine::Status stat = db_->add_group(group_info);
G
groot 已提交
118

119
    engine::meta::TableSchema group_info_get;
120
    group_info_get.table_id = group_name;
X
Xu Peng 已提交
121
    stat = db_->get_group(group_info_get);
G
groot 已提交
122 123 124 125
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    engine::IDNumbers vector_ids;
X
Xu Peng 已提交
126 127 128
    engine::IDNumbers target_ids;

    int d = 256;
X
Xu Peng 已提交
129
    int nb = 50;
X
Xu Peng 已提交
130 131 132 133
    float *xb = new float[d * nb];
    for(int i = 0; i < nb; i++) {
        for(int j = 0; j < d; j++) xb[d * i + j] = drand48();
        xb[d * i] += i / 2000.;
G
groot 已提交
134 135
    }

X
Xu Peng 已提交
136
    int qb = 5;
X
Xu Peng 已提交
137 138 139 140 141 142
    float *qxb = new float[d * qb];
    for(int i = 0; i < qb; i++) {
        for(int j = 0; j < d; j++) qxb[d * i + j] = drand48();
        qxb[d * i] += i / 2000.;
    }

X
Xu Peng 已提交
143 144 145 146 147 148 149 150
    std::thread search([&]() {
        engine::QueryResults results;
        int k = 10;
        std::this_thread::sleep_for(std::chrono::seconds(2));

        INIT_TIMER;
        std::stringstream ss;
        long count = 0;
X
Xu Peng 已提交
151
        long prev_count = -1;
X
Xu Peng 已提交
152

X
Xu Peng 已提交
153
        for (auto j=0; j<10; ++j) {
X
Xu Peng 已提交
154
            ss.str("");
155
            db_->size(count);
X
Xu Peng 已提交
156
            prev_count = count;
X
Xu Peng 已提交
157 158

            START_TIMER;
X
Xu Peng 已提交
159
            stat = db_->search(group_name, k, qb, qxb, results);
160
            ss << "Search " << j << " With Size " << count/engine::meta::M << " M";
X
Xu Peng 已提交
161 162 163
            STOP_TIMER(ss.str());

            ASSERT_STATS(stat);
X
Xu Peng 已提交
164 165 166 167 168 169 170
            for (auto k=0; k<qb; ++k) {
                ASSERT_EQ(results[k][0], target_ids[k]);
                ss.str("");
                ss << "Result [" << k << "]:";
                for (auto result : results[k]) {
                    ss << result << " ";
                }
171
                /* LOG(DEBUG) << ss.str(); */
X
Xu Peng 已提交
172
            }
X
Xu Peng 已提交
173
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
174 175 176 177
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

X
Xu Peng 已提交
178
    int loop = 100000;
X
Xu Peng 已提交
179 180 181

    for (auto i=0; i<loop; ++i) {
        if (i==40) {
X
Xu Peng 已提交
182
            db_->add_vectors(group_name, qb, qxb, target_ids);
183
            ASSERT_EQ(target_ids.size(), qb);
X
Xu Peng 已提交
184
        } else {
X
Xu Peng 已提交
185
            db_->add_vectors(group_name, nb, xb, vector_ids);
X
Xu Peng 已提交
186
        }
X
Xu Peng 已提交
187
        std::this_thread::sleep_for(std::chrono::microseconds(1));
X
Xu Peng 已提交
188
    }
X
xj.lin 已提交
189

X
Xu Peng 已提交
190
    search.join();
X
Xu Peng 已提交
191 192 193

    delete [] xb;
    delete [] qxb;
194
};
X
xj.lin 已提交
195

196
TEST_F(DBTest, SEARCH_TEST) {
X
xj.lin 已提交
197 198 199
    static const std::string group_name = "test_group";
    static const int group_dim = 256;

200
    engine::meta::TableSchema group_info;
X
xj.lin 已提交
201
    group_info.dimension = group_dim;
202
    group_info.table_id = group_name;
X
Xu Peng 已提交
203
    engine::Status stat = db_->add_group(group_info);
X
xj.lin 已提交
204

205
    engine::meta::TableSchema group_info_get;
206
    group_info_get.table_id = group_name;
X
Xu Peng 已提交
207
    stat = db_->get_group(group_info_get);
X
xj.lin 已提交
208 209 210 211
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    // prepare raw data
X
xj.lin 已提交
212
    size_t nb = 250000;
X
xj.lin 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    size_t nq = 10;
    size_t k = 5;
    std::vector<float> xb(nb*group_dim);
    std::vector<float> xq(nq*group_dim);
    std::vector<long> ids(nb);

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis_xt(-1.0, 1.0);
    for (size_t i = 0; i < nb*group_dim; i++) {
        xb[i] = dis_xt(gen);
        if (i < nb){
            ids[i] = i;
        }
    }
    for (size_t i = 0; i < nq*group_dim; i++) {
        xq[i] = dis_xt(gen);
    }

    // result data
    //std::vector<long> nns_gt(k*nq);
    std::vector<long> nns(k*nq);  // nns = nearst neg search
    //std::vector<float> dis_gt(k*nq);
    std::vector<float> dis(k*nq);

    // insert data
    const int batch_size = 100;
    for (int j = 0; j < nb / batch_size; ++j) {
X
Xu Peng 已提交
241
        stat = db_->add_vectors(group_name, batch_size, xb.data()+batch_size*j*group_dim, ids);
X
xj.lin 已提交
242
        if (j == 200){ sleep(1);}
X
xj.lin 已提交
243 244 245
        ASSERT_STATS(stat);
    }

X
Xu Peng 已提交
246
    sleep(2); // wait until build index finish
X
xj.lin 已提交
247

G
groot 已提交
248
    engine::QueryResults results;
X
Xu Peng 已提交
249
    stat = db_->search(group_name, k, nq, xq.data(), results);
G
groot 已提交
250
    ASSERT_STATS(stat);
X
xj.lin 已提交
251 252

    // TODO(linxj): add groundTruth assert
253
};