db_tests.cpp 4.6 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;

15 16
TEST_F(DBTest, DB_TEST) {

G
groot 已提交
17 18 19 20 21 22
    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 已提交
23
    engine::Status stat = db_->add_group(group_info);
G
groot 已提交
24 25 26

    engine::meta::GroupSchema group_info_get;
    group_info_get.group_id = group_name;
X
Xu Peng 已提交
27
    stat = db_->get_group(group_info_get);
G
groot 已提交
28 29 30 31
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    engine::IDNumbers vector_ids;
X
Xu Peng 已提交
32 33 34
    engine::IDNumbers target_ids;

    int d = 256;
X
Xu Peng 已提交
35
    int nb = 50;
X
Xu Peng 已提交
36 37 38 39
    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 已提交
40 41
    }

X
Xu Peng 已提交
42
    int qb = 5;
X
Xu Peng 已提交
43 44 45 46 47 48
    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 已提交
49 50 51 52 53 54 55 56
    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 已提交
57
        long prev_count = -1;
X
Xu Peng 已提交
58

X
Xu Peng 已提交
59
        for (auto j=0; j<10; ++j) {
X
Xu Peng 已提交
60
            ss.str("");
X
Xu Peng 已提交
61
            db_->count(group_name, count);
X
Xu Peng 已提交
62
            prev_count = count;
X
Xu Peng 已提交
63 64

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

            ASSERT_STATS(stat);
X
Xu Peng 已提交
70 71 72 73 74 75 76
            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 << " ";
                }
77
                /* LOG(DEBUG) << ss.str(); */
X
Xu Peng 已提交
78
            }
X
Xu Peng 已提交
79
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
80 81 82 83
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

X
Xu Peng 已提交
84
    int loop = 100000;
X
Xu Peng 已提交
85 86 87

    for (auto i=0; i<loop; ++i) {
        if (i==40) {
X
Xu Peng 已提交
88
            db_->add_vectors(group_name, qb, qxb, target_ids);
89
            ASSERT_EQ(target_ids.size(), qb);
X
Xu Peng 已提交
90
        } else {
X
Xu Peng 已提交
91
            db_->add_vectors(group_name, nb, xb, vector_ids);
X
Xu Peng 已提交
92
        }
X
Xu Peng 已提交
93
        std::this_thread::sleep_for(std::chrono::microseconds(1));
X
Xu Peng 已提交
94
    }
X
xj.lin 已提交
95

X
Xu Peng 已提交
96
    search.join();
X
Xu Peng 已提交
97 98 99

    delete [] xb;
    delete [] qxb;
100
};
X
xj.lin 已提交
101

102
TEST_F(DBTest, SEARCH_TEST) {
X
xj.lin 已提交
103 104 105 106 107 108
    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 已提交
109
    engine::Status stat = db_->add_group(group_info);
X
xj.lin 已提交
110 111 112

    engine::meta::GroupSchema group_info_get;
    group_info_get.group_id = group_name;
X
Xu Peng 已提交
113
    stat = db_->get_group(group_info_get);
X
xj.lin 已提交
114 115 116 117
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    // prepare raw data
X
xj.lin 已提交
118
    size_t nb = 250000;
X
xj.lin 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    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 已提交
147
        stat = db_->add_vectors(group_name, batch_size, xb.data()+batch_size*j*group_dim, ids);
X
xj.lin 已提交
148
        if (j == 200){ sleep(1);}
X
xj.lin 已提交
149 150 151
        ASSERT_STATS(stat);
    }

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

G
groot 已提交
154
    engine::QueryResults results;
X
Xu Peng 已提交
155
    stat = db_->search(group_name, k, nq, xq.data(), results);
G
groot 已提交
156
    ASSERT_STATS(stat);
X
xj.lin 已提交
157 158

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