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;
G
groot 已提交
70 71
    group_info.dimension_ = group_dim;
    group_info.table_id_ = group_name;
X
Xu Peng 已提交
72
    engine::Status stat = db_->CreateTable(group_info);
X
Xu Peng 已提交
73

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

    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
    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) {
X
Xu Peng 已提交
95
        db_->InsertVectors(group_name, nb, xb, vector_ids);
X
Xu Peng 已提交
96 97 98 99 100
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }

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

X
Xu Peng 已提交
101
    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) {

Y
c  
yu yunfeng 已提交
111 112


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

116
    engine::meta::TableSchema group_info;
G
groot 已提交
117 118
    group_info.dimension_ = group_dim;
    group_info.table_id_ = group_name;
X
Xu Peng 已提交
119
    engine::Status stat = db_->CreateTable(group_info);
G
groot 已提交
120

121
    engine::meta::TableSchema group_info_get;
G
groot 已提交
122
    group_info_get.table_id_ = group_name;
X
Xu Peng 已提交
123
    stat = db_->DescribeTable(group_info_get);
G
groot 已提交
124
    ASSERT_STATS(stat);
G
groot 已提交
125
    ASSERT_EQ(group_info_get.dimension_, group_dim);
G
groot 已提交
126 127

    engine::IDNumbers vector_ids;
X
Xu Peng 已提交
128 129 130
    engine::IDNumbers target_ids;

    int d = 256;
X
Xu Peng 已提交
131
    int nb = 50;
X
Xu Peng 已提交
132 133 134 135
    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 已提交
136 137
    }

X
Xu Peng 已提交
138
    int qb = 5;
X
Xu Peng 已提交
139 140 141 142 143 144
    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 已提交
145 146 147 148 149 150 151 152
    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 已提交
153
        long prev_count = -1;
X
Xu Peng 已提交
154

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

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

            ASSERT_STATS(stat);
X
Xu Peng 已提交
166 167 168 169 170 171 172
            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 << " ";
                }
173
                /* LOG(DEBUG) << ss.str(); */
X
Xu Peng 已提交
174
            }
X
Xu Peng 已提交
175
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
176 177 178 179
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

X
Xu Peng 已提交
180
    int loop = 100000;
X
Xu Peng 已提交
181 182 183

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

X
Xu Peng 已提交
192
    search.join();
X
Xu Peng 已提交
193 194 195

    delete [] xb;
    delete [] qxb;
196
};
X
xj.lin 已提交
197

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

202
    engine::meta::TableSchema group_info;
G
groot 已提交
203 204
    group_info.dimension_ = group_dim;
    group_info.table_id_ = group_name;
X
Xu Peng 已提交
205
    engine::Status stat = db_->CreateTable(group_info);
X
xj.lin 已提交
206

207
    engine::meta::TableSchema group_info_get;
G
groot 已提交
208
    group_info_get.table_id_ = group_name;
X
Xu Peng 已提交
209
    stat = db_->DescribeTable(group_info_get);
X
xj.lin 已提交
210
    ASSERT_STATS(stat);
G
groot 已提交
211
    ASSERT_EQ(group_info_get.dimension_, group_dim);
X
xj.lin 已提交
212 213

    // prepare raw data
X
xj.lin 已提交
214
    size_t nb = 250000;
X
xj.lin 已提交
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 241 242
    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 已提交
243
        stat = db_->InsertVectors(group_name, batch_size, xb.data()+batch_size*j*group_dim, ids);
X
xj.lin 已提交
244
        if (j == 200){ sleep(1);}
X
xj.lin 已提交
245 246 247
        ASSERT_STATS(stat);
    }

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

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

    // TODO(linxj): add groundTruth assert
255
};
Y
c  
yu yunfeng 已提交
256