db_tests.cpp 4.3 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 43 44 45 46 47 48
    int qb = 1;
    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 70
            STOP_TIMER(ss.str());

            ASSERT_STATS(stat);
            ASSERT_EQ(results[0][0], target_ids[0]);
X
Xu Peng 已提交
71
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
72 73 74 75
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

X
Xu Peng 已提交
76
    int loop = 100000;
X
Xu Peng 已提交
77 78 79

    for (auto i=0; i<loop; ++i) {
        if (i==40) {
X
Xu Peng 已提交
80
            db_->add_vectors(group_name, qb, qxb, target_ids);
X
Xu Peng 已提交
81
        } else {
X
Xu Peng 已提交
82
            db_->add_vectors(group_name, nb, xb, vector_ids);
X
Xu Peng 已提交
83
        }
X
Xu Peng 已提交
84
        std::this_thread::sleep_for(std::chrono::microseconds(1));
X
Xu Peng 已提交
85
    }
X
xj.lin 已提交
86

X
Xu Peng 已提交
87
    search.join();
X
Xu Peng 已提交
88 89 90

    delete [] xb;
    delete [] qxb;
91
};
X
xj.lin 已提交
92

93
TEST_F(DBTest, SEARCH_TEST) {
X
xj.lin 已提交
94 95 96 97 98 99
    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 已提交
100
    engine::Status stat = db_->add_group(group_info);
X
xj.lin 已提交
101 102 103

    engine::meta::GroupSchema group_info_get;
    group_info_get.group_id = group_name;
X
Xu Peng 已提交
104
    stat = db_->get_group(group_info_get);
X
xj.lin 已提交
105 106 107 108
    ASSERT_STATS(stat);
    ASSERT_EQ(group_info_get.dimension, group_dim);

    // prepare raw data
X
xj.lin 已提交
109
    size_t nb = 250000;
X
xj.lin 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    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 已提交
138
        stat = db_->add_vectors(group_name, batch_size, xb.data()+batch_size*j*group_dim, ids);
X
xj.lin 已提交
139
        if (j == 200){ sleep(1);}
X
xj.lin 已提交
140 141 142
        ASSERT_STATS(stat);
    }

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

G
groot 已提交
145
    engine::QueryResults results;
X
Xu Peng 已提交
146
    stat = db_->search(group_name, k, nq, xq.data(), results);
G
groot 已提交
147
    ASSERT_STATS(stat);
X
xj.lin 已提交
148 149

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