| 1 |
// Copyright (C) 2019-2020 Zilliz. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance |
| 4 |
// with the License. You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software distributed under the License |
| 9 |
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 10 |
// or implied. See the License for the specific language governing permissions and limitations under the License |
| 11 |
|
| 12 |
#include <benchmark/benchmark.h> |
| 13 |
#include <tuple> |
| 14 |
#include <map> |
| 15 |
#include <google/protobuf/text_format.h> |
| 16 |
#include <knowhere/comp/index_param.h> |
| 17 |
|
| 18 |
#include "pb/index_cgo_msg.pb.h" |
| 19 |
#include "indexbuilder/VecIndexCreator.h" |
| 20 |
#include "indexbuilder/index_c.h" |
| 21 |
#include "test_utils/indexbuilder_test_utils.h" |
| 22 |
#include "common/Consts.h" |
| 23 |
|
| 24 |
constexpr int64_t NB = 1000000; |
| 25 |
|
| 26 |
namespace indexcgo = milvus::proto::indexcgo; |
| 27 |
|
| 28 |
auto index_type_collections = [] { |
| 29 |
static std::map<int, knowhere::IndexType> collections{ |
| 30 |
{0, knowhere::IndexEnum::INDEX_FAISS_IVFFLAT}, |
| 31 |
}; |
| 32 |
return collections; |
| 33 |
}(); |
| 34 |
|
| 35 |
auto metric_type_collections = [] { |
| 36 |
static std::map<int, knowhere::MetricType> collections{ |
| 37 |
{0, knowhere::metric::L2}, |
| 38 |
}; |
| 39 |
return collections; |
| 40 |
}(); |
| 41 |
|
| 42 |
static void |
| 43 |
IndexBuilder_build(benchmark::State& state) { |
| 44 |
auto index_type = index_type_collections.at(state.range(0)); |
| 45 |
auto metric_type = metric_type_collections.at(state.range(0)); |
| 46 |
|
| 47 |
indexcgo::TypeParams type_params; |
| 48 |
indexcgo::IndexParams index_params; |
| 49 |
|
| 50 |
std::tie(type_params, index_params) = |
| 51 |
generate_params(index_type, metric_type); |
| 52 |
|
| 53 |
milvus::Config config; |
| 54 |
for (auto i = 0; i < type_params.params_size(); ++i) { |
| 55 |
const auto& param = type_params.params(i); |
| 56 |
config[param.key()] = param.value(); |
| 57 |
} |
| 58 |
|
| 59 |
for (auto i = 0; i < index_params.params_size(); ++i) { |
| 60 |
const auto& param = index_params.params(i); |
| 61 |
config[param.key()] = param.value(); |
| 62 |
} |
| 63 |
config[milvus::index::INDEX_ENGINE_VERSION] = |
| 64 |
std::to_string(knowhere::Version::GetCurrentVersion().VersionNumber()); |
| 65 |
|
| 66 |
auto is_binary = state.range(2); |
| 67 |
auto dataset = GenDataset(NB, metric_type, is_binary); |
| 68 |
auto xb_data = dataset.get_col<float>(milvus::FieldId(START_USER_FIELDID)); |
| 69 |
auto xb_dataset = knowhere::GenDataSet(NB, DIM, xb_data.data()); |
| 70 |
|
| 71 |
for (auto _ : state) { |
| 72 |
auto index = std::make_unique<milvus::indexbuilder::VecIndexCreator>( |
| 73 |
milvus::DataType::VECTOR_FLOAT, config); |
| 74 |
index->Build(xb_dataset); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
IndexBuilder_build_and_codec(benchmark::State& state) { |
| 80 |
auto index_type = index_type_collections.at(state.range(0)); |
| 81 |
auto metric_type = metric_type_collections.at(state.range(0)); |
| 82 |
|
| 83 |
indexcgo::TypeParams type_params; |
| 84 |
indexcgo::IndexParams index_params; |
| 85 |
|
| 86 |
std::tie(type_params, index_params) = |
| 87 |
generate_params(index_type, metric_type); |
| 88 |
|
| 89 |
milvus::Config config; |
| 90 |
for (auto i = 0; i < type_params.params_size(); ++i) { |
| 91 |
const auto& param = type_params.params(i); |
| 92 |
config[param.key()] = param.value(); |
| 93 |
} |
| 94 |
|
| 95 |
for (auto i = 0; i < index_params.params_size(); ++i) { |
| 96 |
const auto& param = index_params.params(i); |
| 97 |
config[param.key()] = param.value(); |
| 98 |
} |
| 99 |
|
| 100 |
auto is_binary = state.range(2); |
| 101 |
auto dataset = GenDataset(NB, metric_type, is_binary); |
| 102 |
auto xb_data = dataset.get_col<float>(milvus::FieldId(100)); |
| 103 |
auto xb_dataset = knowhere::GenDataSet(NB, DIM, xb_data.data()); |
| 104 |
|
| 105 |
for (auto _ : state) { |
| 106 |
auto index = std::make_unique<milvus::indexbuilder::VecIndexCreator>( |
| 107 |
milvus::DataType::VECTOR_FLOAT, config); |
| 108 |
|
| 109 |
index->Build(xb_dataset); |
| 110 |
index->Serialize(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// IVF_FLAT, L2, VectorFloat |
| 115 |
BENCHMARK(IndexBuilder_build)->Args({0, 0, false}); |
| 116 |
|
| 117 |
// IVF_FLAT, L2, VectorFloat |
| 118 |
BENCHMARK(IndexBuilder_build_and_codec)->Args({0, 0, false}); |