|
| 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 <gtest/gtest.h> |
| 13 | + |
| 14 | +#include <thread> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include "benchmark_knowhere.h" |
| 18 | +#include "knowhere/comp/index_param.h" |
| 19 | +#include "knowhere/comp/knowhere_config.h" |
| 20 | +#include "knowhere/dataset.h" |
| 21 | + |
| 22 | +class Benchmark_svs_float_qps : public Benchmark_knowhere, public ::testing::Test { |
| 23 | + public: |
| 24 | + template <typename T> |
| 25 | + void |
| 26 | + test_flat(const knowhere::Json& cfg) { |
| 27 | + auto conf = cfg; |
| 28 | + |
| 29 | + float expected_recall = 1.0f; |
| 30 | + conf[knowhere::meta::TOPK] = topk_; |
| 31 | + |
| 32 | + std::string data_type_str = get_data_type_name<T>(); |
| 33 | + printf("\n[%0.3f s] %s | %s(%s) | k=%d, R@=%.4f\n", get_time_diff(), ann_test_name_.c_str(), |
| 34 | + index_type_.c_str(), data_type_str.c_str(), topk_, expected_recall); |
| 35 | + printf("================================================================================\n"); |
| 36 | + for (auto thread_num : THREAD_NUMs_) { |
| 37 | + CALC_TIME_SPAN(task<T>(conf, thread_num, nq_)); |
| 38 | + printf(" thread_num = %2d, elapse = %6.3fs, VPS = %.3f\n", thread_num, TDIFF_, nq_ / TDIFF_); |
| 39 | + std::fflush(stdout); |
| 40 | + } |
| 41 | + printf("================================================================================\n"); |
| 42 | + printf("[%.3f s] Test '%s/%s' done\n\n", get_time_diff(), ann_test_name_.c_str(), index_type_.c_str()); |
| 43 | + } |
| 44 | + |
| 45 | + private: |
| 46 | + template <typename T> |
| 47 | + void |
| 48 | + task(const knowhere::Json& conf, int32_t worker_num, int32_t nq_total) { |
| 49 | + auto worker = [&](int32_t idx_start, int32_t num) { |
| 50 | + num = std::min(num, nq_total - idx_start); |
| 51 | + for (int32_t i = 0; i < num; i++) { |
| 52 | + auto ds_ptr = knowhere::GenDataSet(1, dim_, (const float*)xq_ + (idx_start + i) * dim_); |
| 53 | + auto query = knowhere::ConvertToDataTypeIfNeeded<T>(ds_ptr); |
| 54 | + index_.value().Search(query, conf, nullptr); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + std::vector<std::thread> thread_vector(worker_num); |
| 59 | + for (int32_t i = 0; i < worker_num; i++) { |
| 60 | + int32_t idx_start, req_num; |
| 61 | + req_num = nq_total / worker_num; |
| 62 | + if (nq_total % worker_num != 0) { |
| 63 | + req_num++; |
| 64 | + } |
| 65 | + idx_start = req_num * i; |
| 66 | + thread_vector[i] = std::thread(worker, idx_start, req_num); |
| 67 | + } |
| 68 | + for (int32_t i = 0; i < worker_num; i++) { |
| 69 | + thread_vector[i].join(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + protected: |
| 74 | + void |
| 75 | + SetUp() override { |
| 76 | + T0_ = elapsed(); |
| 77 | + set_ann_test_name("sift-128-euclidean"); |
| 78 | + parse_ann_test_name(); |
| 79 | + load_hdf5_data<knowhere::fp32>(); |
| 80 | + |
| 81 | + cfg_[knowhere::meta::METRIC_TYPE] = metric_type_; |
| 82 | + knowhere::KnowhereConfig::SetSimdType(knowhere::KnowhereConfig::SimdType::AVX2); |
| 83 | + knowhere::KnowhereConfig::SetBuildThreadPoolSize(default_build_thread_num); |
| 84 | + knowhere::KnowhereConfig::SetSearchThreadPoolSize(default_search_thread_num); |
| 85 | + printf("faiss::distance_compute_blas_threshold: %ld\n", knowhere::KnowhereConfig::GetBlasThreshold()); |
| 86 | + } |
| 87 | + |
| 88 | + void |
| 89 | + TearDown() override { |
| 90 | + free_all(); |
| 91 | + } |
| 92 | + |
| 93 | + protected: |
| 94 | + const int32_t topk_ = 100; |
| 95 | + const std::vector<int32_t> THREAD_NUMs_ = {8}; |
| 96 | +}; |
| 97 | + |
| 98 | +#define TEST_INDEX(NAME, T, X) \ |
| 99 | + index_file_name = get_index_name<T>(X); \ |
| 100 | + create_index<T>(index_file_name, conf); \ |
| 101 | + test_##NAME<T>(conf) |
| 102 | + |
| 103 | +TEST_F(Benchmark_svs_float_qps, TEST_FAISS_FLAT) { |
| 104 | + index_type_ = knowhere::IndexEnum::INDEX_FAISS_IDMAP; |
| 105 | + |
| 106 | + std::string index_file_name; |
| 107 | + knowhere::Json conf = cfg_; |
| 108 | + std::vector<int32_t> params = {}; |
| 109 | + |
| 110 | + TEST_INDEX(flat, knowhere::fp32, params); |
| 111 | +} |
| 112 | + |
| 113 | +#ifdef KNOWHERE_WITH_SVS |
| 114 | +TEST_F(Benchmark_svs_float_qps, TEST_SVS_FLAT) { |
| 115 | + index_type_ = knowhere::IndexEnum::INDEX_SVS_FLAT; |
| 116 | + |
| 117 | + std::string index_file_name; |
| 118 | + knowhere::Json conf = cfg_; |
| 119 | + std::vector<int32_t> params = {}; |
| 120 | + |
| 121 | + TEST_INDEX(flat, knowhere::fp32, params); |
| 122 | +} |
| 123 | +#endif |
0 commit comments