Debian Codegraph @

thirdparty/milvus/internal/core/unittest/bench/bench_search.cpp

raw
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 <cstdint>
13 #include <benchmark/benchmark.h>
14 #include <string>
15 #include "segcore/SegmentGrowing.h"
16 #include "segcore/SegmentSealed.h"
17 #include "test_utils/DataGen.h"
18
19 using namespace milvus;
20 using namespace milvus::query;
21 using namespace milvus::segcore;
22
23 static int dim = 768;
24
25 const auto schema = []() {
26 auto schema = std::make_shared<Schema>();
27 schema->AddDebugField(
28 "fakevec", DataType::VECTOR_FLOAT, dim, knowhere::metric::L2);
29 auto i64_fid = schema->AddDebugField("age", DataType::INT64);
30 schema->set_primary_field_id(i64_fid);
31 return schema;
32 }();
33
34 const auto search_plan = [] {
35 const char* raw_plan = R"(vector_anns: <
36 field_id: 100
37 query_info: <
38 topk: 5
39 round_decimal: -1
40 metric_type: "L2"
41 search_params: "{\"nprobe\": 10}"
42 >
43 placeholder_tag: "$0"
44 >)";
45 auto plan_str = translate_text_plan_to_binary_plan(raw_plan);
46 auto plan =
47 CreateSearchPlanByExpr(*schema, plan_str.data(), plan_str.size());
48 return plan;
49 }();
50 auto ph_group = [] {
51 auto num_queries = 10;
52 auto ph_group_raw = CreatePlaceholderGroup(num_queries, dim, 1024);
53 auto ph_group = ParsePlaceholderGroup(search_plan.get(),
54 ph_group_raw.SerializeAsString());
55 return ph_group;
56 }();
57
58 static void
59 Search_GrowingIndex(benchmark::State& state) {
60 // schema->AddDebugField("age", DataType::FLOAT);
61
62 static int64_t N = 1024 * 32;
63 const auto dataset_ = [] {
64 auto dataset_ = DataGen(schema, N);
65 return dataset_;
66 }();
67
68 auto chunk_rows = state.range(1) * 1024;
69 auto segconf = SegcoreConfig::default_config();
70 segconf.set_chunk_rows(chunk_rows);
71
72 std::map<std::string, std::string> index_params = {
73 {"index_type", "IVF_FLAT"}, {"metric_type", "L2"}, {"nlist", "128"}};
74 std::map<std::string, std::string> type_params = {{"dim", "128"}};
75 FieldIndexMeta fieldIndexMeta(schema->get_field_id(FieldName("fakevec")),
76 std::move(index_params),
77 std::move(type_params));
78 segconf.set_enable_interim_segment_index(true);
79 std::map<FieldId, FieldIndexMeta> filedMap = {
80 {schema->get_field_id(FieldName("fakevec")), fieldIndexMeta}};
81 IndexMetaPtr metaPtr =
82 std::make_shared<CollectionIndexMeta>(226985, std::move(filedMap));
83
84 auto segment = CreateGrowingSegment(schema, metaPtr, -1, segconf);
85
86 segment->PreInsert(N);
87 segment->Insert(0,
88 N,
89 dataset_.row_ids_.data(),
90 dataset_.timestamps_.data(),
91 dataset_.raw_);
92
93 Timestamp ts = 10000000;
94
95 for (auto _ : state) {
96 auto qr = segment->Search(search_plan.get(), ph_group.get(), ts);
97 }
98 }
99
100 BENCHMARK(Search_GrowingIndex)
101 ->MinTime(5)
102 ->ArgsProduct({{true, false}, {8, 16, 32}});
103
104 static void
105 Search_Sealed(benchmark::State& state) {
106 auto segment = CreateSealedSegment(schema);
107 static int64_t N = 1024 * 1024;
108 const auto dataset_ = [] {
109 auto dataset_ = DataGen(schema, N);
110 return dataset_;
111 }();
112 SealedLoadFieldData(dataset_, *segment);
113 auto choice = state.range(0);
114 if (choice == 0) {
115 // Brute Force
116 } else if (choice == 1) {
117 // hnsw
118 auto vec = dataset_.get_col<float>(milvus::FieldId(100));
119 auto indexing =
120 GenVecIndexing(N, dim, vec.data(), knowhere::IndexEnum::INDEX_HNSW);
121 segcore::LoadIndexInfo info;
122 info.index = std::move(indexing);
123 info.field_id = (*schema)[FieldName("fakevec")].get_id().get();
124 info.index_params["index_type"] = "HNSW";
125 info.index_params["metric_type"] = knowhere::metric::L2;
126 segment->DropFieldData(milvus::FieldId(100));
127 segment->LoadIndex(info);
128 }
129
130 Timestamp ts = 10000000;
131
132 for (auto _ : state) {
133 auto qr = segment->Search(search_plan.get(), ph_group.get(), ts);
134 }
135 }
136
137 BENCHMARK(Search_Sealed)->MinTime(5)->Arg(1)->Arg(0);