db_tests.cpp 6.2 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 12 13 14
#include "db/DB.h"

using namespace zilliz::vecwise;

X
Xu Peng 已提交
15 16 17 18 19 20 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
TEST_F(DBTest, CONFIG_TEST) {
    {
        EXPECT_DEATH(engine::ArchiveConf conf("wrong"), "");
    }
    {
        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);
    }
}

60 61
TEST_F(DBTest, DB_TEST) {

G
groot 已提交
62 63 64 65 66 67
    static const std::string group_name = "test_group";
    static const int group_dim = 256;

    engine::meta::GroupSchema group_info;
    group_info.dimension = group_dim;
    group_info.group_id = group_name;
X
Xu Peng 已提交
68
    engine::Status stat = db_->add_group(group_info);
G
groot 已提交
69 70 71

    engine::meta::GroupSchema group_info_get;
    group_info_get.group_id = group_name;
X
Xu Peng 已提交
72
    stat = db_->get_group(group_info_get);
G
groot 已提交
73 74 75 76
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    engine::IDNumbers vector_ids;
X
Xu Peng 已提交
77 78 79
    engine::IDNumbers target_ids;

    int d = 256;
X
Xu Peng 已提交
80
    int nb = 50;
X
Xu Peng 已提交
81 82 83 84
    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 已提交
85 86
    }

X
Xu Peng 已提交
87
    int qb = 5;
X
Xu Peng 已提交
88 89 90 91 92 93
    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 已提交
94 95 96 97 98 99 100 101
    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 已提交
102
        long prev_count = -1;
X
Xu Peng 已提交
103

X
Xu Peng 已提交
104
        for (auto j=0; j<10; ++j) {
X
Xu Peng 已提交
105
            ss.str("");
X
Xu Peng 已提交
106
            db_->count(group_name, count);
X
Xu Peng 已提交
107
            prev_count = count;
X
Xu Peng 已提交
108 109

            START_TIMER;
X
Xu Peng 已提交
110
            stat = db_->search(group_name, k, qb, qxb, results);
X
Xu Peng 已提交
111
            ss << "Search " << j << " With Size " << (float)(count*group_dim*sizeof(float))/(1024*1024) << " M";
X
Xu Peng 已提交
112 113 114
            STOP_TIMER(ss.str());

            ASSERT_STATS(stat);
X
Xu Peng 已提交
115 116 117 118 119 120 121
            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 << " ";
                }
122
                /* LOG(DEBUG) << ss.str(); */
X
Xu Peng 已提交
123
            }
X
Xu Peng 已提交
124
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
125 126 127 128
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

X
Xu Peng 已提交
129
    int loop = 100000;
X
Xu Peng 已提交
130 131 132

    for (auto i=0; i<loop; ++i) {
        if (i==40) {
X
Xu Peng 已提交
133
            db_->add_vectors(group_name, qb, qxb, target_ids);
134
            ASSERT_EQ(target_ids.size(), qb);
X
Xu Peng 已提交
135
        } else {
X
Xu Peng 已提交
136
            db_->add_vectors(group_name, nb, xb, vector_ids);
X
Xu Peng 已提交
137
        }
X
Xu Peng 已提交
138
        std::this_thread::sleep_for(std::chrono::microseconds(1));
X
Xu Peng 已提交
139
    }
X
xj.lin 已提交
140

X
Xu Peng 已提交
141
    search.join();
X
Xu Peng 已提交
142 143 144

    delete [] xb;
    delete [] qxb;
145
};
X
xj.lin 已提交
146

147
TEST_F(DBTest, SEARCH_TEST) {
X
xj.lin 已提交
148 149 150 151 152 153
    static const std::string group_name = "test_group";
    static const int group_dim = 256;

    engine::meta::GroupSchema group_info;
    group_info.dimension = group_dim;
    group_info.group_id = group_name;
X
Xu Peng 已提交
154
    engine::Status stat = db_->add_group(group_info);
X
xj.lin 已提交
155 156 157

    engine::meta::GroupSchema group_info_get;
    group_info_get.group_id = group_name;
X
Xu Peng 已提交
158
    stat = db_->get_group(group_info_get);
X
xj.lin 已提交
159 160 161 162
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    // prepare raw data
X
xj.lin 已提交
163
    size_t nb = 250000;
X
xj.lin 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    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 已提交
192
        stat = db_->add_vectors(group_name, batch_size, xb.data()+batch_size*j*group_dim, ids);
X
xj.lin 已提交
193
        if (j == 200){ sleep(1);}
X
xj.lin 已提交
194 195 196
        ASSERT_STATS(stat);
    }

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

G
groot 已提交
199
    engine::QueryResults results;
X
Xu Peng 已提交
200
    stat = db_->search(group_name, k, nq, xq.data(), results);
G
groot 已提交
201
    ASSERT_STATS(stat);
X
xj.lin 已提交
202 203

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