test_engine.cpp 3.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18 19
#include <gtest/gtest.h>
#include <boost/filesystem.hpp>
S
starlord 已提交
20
#include <vector>
S
starlord 已提交
21 22 23

#include "db/engine/EngineFactory.h"
#include "db/engine/ExecutionEngineImpl.h"
S
starlord 已提交
24
#include "db/utils.h"
S
starlord 已提交
25

S
starlord 已提交
26
TEST_F(EngineTest, FACTORY_TEST) {
S
starlord 已提交
27
    {
S
starlord 已提交
28
        auto engine_ptr = milvus::engine::EngineFactory::Build(
S
starlord 已提交
29 30
                512,
                "/tmp/milvus_index_1",
S
starlord 已提交
31 32
                milvus::engine::EngineType::INVALID,
                milvus::engine::MetricType::IP,
S
starlord 已提交
33
                1024);
S
starlord 已提交
34 35 36 37 38

        ASSERT_TRUE(engine_ptr == nullptr);
    }

    {
S
starlord 已提交
39
        auto engine_ptr = milvus::engine::EngineFactory::Build(
S
starlord 已提交
40 41
                512,
                "/tmp/milvus_index_1",
S
starlord 已提交
42 43
                milvus::engine::EngineType::FAISS_IDMAP,
                milvus::engine::MetricType::IP,
S
starlord 已提交
44
                1024);
S
starlord 已提交
45 46 47 48 49

        ASSERT_TRUE(engine_ptr != nullptr);
    }

    {
S
starlord 已提交
50
        auto engine_ptr = milvus::engine::EngineFactory::Build(
S
starlord 已提交
51 52
                512,
                "/tmp/milvus_index_1",
S
starlord 已提交
53 54
                milvus::engine::EngineType::FAISS_IVFFLAT,
                milvus::engine::MetricType::IP,
S
starlord 已提交
55
                1024);
S
starlord 已提交
56 57 58 59 60

        ASSERT_TRUE(engine_ptr != nullptr);
    }

    {
S
starlord 已提交
61
        auto engine_ptr = milvus::engine::EngineFactory::Build(
S
starlord 已提交
62 63
                512,
                "/tmp/milvus_index_1",
S
starlord 已提交
64 65
                milvus::engine::EngineType::FAISS_IVFSQ8,
                milvus::engine::MetricType::IP,
S
starlord 已提交
66
                1024);
S
starlord 已提交
67 68 69 70 71

        ASSERT_TRUE(engine_ptr != nullptr);
    }

    {
S
starlord 已提交
72
        auto engine_ptr = milvus::engine::EngineFactory::Build(
S
starlord 已提交
73 74
                512,
                "/tmp/milvus_index_1",
S
starlord 已提交
75 76
                milvus::engine::EngineType::NSG_MIX,
                milvus::engine::MetricType::IP,
S
starlord 已提交
77
                1024);
S
starlord 已提交
78 79 80 81 82

        ASSERT_TRUE(engine_ptr != nullptr);
    }
}

S
starlord 已提交
83
TEST_F(EngineTest, ENGINE_IMPL_TEST) {
S
starlord 已提交
84 85
    uint16_t dimension = 64;
    std::string file_path = "/tmp/milvus_index_1";
S
starlord 已提交
86
    auto engine_ptr = milvus::engine::EngineFactory::Build(
S
starlord 已提交
87 88
            dimension,
            file_path,
S
starlord 已提交
89 90
            milvus::engine::EngineType::FAISS_IVFFLAT,
            milvus::engine::MetricType::IP,
S
starlord 已提交
91
            1024);
S
starlord 已提交
92 93

    std::vector<float> data;
S
starlord 已提交
94
    std::vector<int64_t> ids;
S
starlord 已提交
95 96 97
    const int row_count = 10000;
    data.reserve(row_count*dimension);
    ids.reserve(row_count);
S
starlord 已提交
98
    for (int64_t i = 0; i < row_count; i++) {
S
starlord 已提交
99
        ids.push_back(i);
S
starlord 已提交
100
        for (uint16_t k = 0; k < dimension; k++) {
S
starlord 已提交
101 102 103 104
            data.push_back(i*dimension + k);
        }
    }

S
starlord 已提交
105
    auto status = engine_ptr->AddWithIds((int64_t)ids.size(), data.data(), ids.data());
S
starlord 已提交
106 107 108 109 110
    ASSERT_TRUE(status.ok());

    ASSERT_EQ(engine_ptr->Dimension(), dimension);
    ASSERT_EQ(engine_ptr->Count(), ids.size());

Y
Yu Kun 已提交
111 112 113 114 115 116 117 118 119 120 121 122
    status = engine_ptr->CopyToGpu(0, true);
    status = engine_ptr->CopyToGpu(0, false);
    //ASSERT_TRUE(status.ok());

    auto new_engine = engine_ptr->Clone();
    ASSERT_EQ(new_engine->Dimension(), dimension);
    ASSERT_EQ(new_engine->Count(), ids.size());
    status = new_engine->CopyToCpu();
    //ASSERT_TRUE(status.ok());

    auto engine_build = new_engine->BuildIndex("/tmp/milvus_index_2", milvus::engine::EngineType::FAISS_IVFSQ8);
    //ASSERT_TRUE(status.ok());
S
starlord 已提交
123
}