From 9ccf5c1c7dd13b9e7ac24979865941140ab35cc0 Mon Sep 17 00:00:00 2001 From: bigsheeper Date: Wed, 16 Sep 2020 10:39:09 +0800 Subject: [PATCH] Add simple tests to SDK Signed-off-by: bigsheeper --- sdk/examples/CMakeLists.txt | 1 + sdk/examples/insert.cpp | 81 ------------------------------ sdk/examples/search.cpp | 59 ---------------------- sdk/examples/simple/CMakeLists.txt | 17 +++++++ sdk/examples/simple/delete.cpp | 36 +++++++++++++ sdk/examples/simple/insert.cpp | 60 ++++++++++++++++++++++ sdk/examples/simple/ip.h | 16 ++++++ sdk/examples/simple/search.cpp | 68 +++++++++++++++++++++++++ 8 files changed, 198 insertions(+), 140 deletions(-) delete mode 100644 sdk/examples/insert.cpp delete mode 100644 sdk/examples/search.cpp create mode 100644 sdk/examples/simple/CMakeLists.txt create mode 100644 sdk/examples/simple/delete.cpp create mode 100644 sdk/examples/simple/insert.cpp create mode 100644 sdk/examples/simple/ip.h create mode 100644 sdk/examples/simple/search.cpp diff --git a/sdk/examples/CMakeLists.txt b/sdk/examples/CMakeLists.txt index 9bc6b9aa61..857750b0b6 100644 --- a/sdk/examples/CMakeLists.txt +++ b/sdk/examples/CMakeLists.txt @@ -11,6 +11,7 @@ # or implied. See the License for the specific language governing permissions and limitations under the License. #------------------------------------------------------------------------------- +add_subdirectory(simple) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/utils UTIL_SRC_FILES) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/common COMMON_SRC_FILES) diff --git a/sdk/examples/insert.cpp b/sdk/examples/insert.cpp deleted file mode 100644 index 9d0b311198..0000000000 --- a/sdk/examples/insert.cpp +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed 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. - -#include -#include -#include -#include -#include - -#include "examples/utils/Utils.h" - -#include "grpc/ClientProxy.h" -#include "interface/ConnectionImpl.h" - -const milvus::FieldValue GetData() { - milvus::FieldValue value_map; - std::vector char_data; - std::vector int32_data; - for (int i = 0; i < 20; i++) { - char_data.push_back(i); - int32_data.push_back(i); - } - std::vector vector_data; - for (int i = 0; i < 20; i++) { - std::vector float_data(10, 10.25); - milvus::VectorData vectorData; - vectorData.float_data = float_data; - vector_data.push_back(vectorData); - } - value_map.int8_value["INT8"] = char_data; - value_map.int32_value["INT32"] = int32_data; - value_map.vector_value["VECTOR"] = vector_data; - value_map.row_num = 20; - return value_map; -} - -milvus::Mapping -GetMapByInsertParam(milvus::grpc::InsertParam &insert_param) { - milvus::Mapping map; - for (int64_t i = 0; i < insert_param.schema().field_metas().size(); i++) { - auto grpc_field = insert_param.schema().field_metas()[i]; - milvus::FieldPtr field_ptr = std::make_shared(); - field_ptr->field_name = grpc_field.field_name(); - field_ptr->field_type = (milvus::DataType) grpc_field.type(); - field_ptr->dim = grpc_field.dim(); - map.fields.emplace_back(field_ptr); - } - return map; -} - -int -main(int argc, char* argv[]) { - printf("Client start...\n"); - TestParameters parameters = milvus_sdk::Utils::ParseTestParameters(argc, argv); - std::cout< ids_array; - auto data = GetData(); - for (int64_t i = 0; i < 20; i++) { - ids_array.push_back(i); - } - client.Insert("collection0", "tag01", data, ids_array); - - return 0; -} diff --git a/sdk/examples/search.cpp b/sdk/examples/search.cpp deleted file mode 100644 index f53c55a1ca..0000000000 --- a/sdk/examples/search.cpp +++ /dev/null @@ -1,59 +0,0 @@ - - -// Copyright (C) 2019-2020 Zilliz. All rights reserved. -// -// Licensed 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. -#include "interface/ConnectionImpl.h" -#include "include/MilvusApi.h" -#include "grpc/ClientProxy.h" -#include "interface/ConnectionImpl.h" -#include "utils/TimeRecorder.h" - -int main(int argc , char**argv) { - auto client = milvus::ConnectionImpl(); - milvus::ConnectParam connect_param; - connect_param.ip_address = "192.168.2.28"; - connect_param.port = "19530"; - client.Connect(connect_param); - std::vector ids_array; - std::vector partition_list; - partition_list.emplace_back("partition-1"); - partition_list.emplace_back("partition-2"); - partition_list.emplace_back("partition-3"); - - milvus::VectorParam vectorParam; - milvus::VectorData vectorData; - std::vector float_data; - std::vector binary_data; - for (int i = 0; i < 100; ++i) { - float_data.emplace_back(i); - binary_data.emplace_back(i); - } - - vectorData.float_data = float_data; - vectorData.binary_data = binary_data; - - std::vector vector_records; - for (int j = 0; j < 10; ++j) { - vector_records.emplace_back(vectorData); - } - - - vectorParam.json_param = "json_param"; - vectorParam.vector_records = vector_records; - - milvus::TopKQueryResult result; - - milvus_sdk::TimeRecorder test_search("search"); - auto status = client.Search("collection1", partition_list, "dsl", vectorParam, result); - - return 0; -} - diff --git a/sdk/examples/simple/CMakeLists.txt b/sdk/examples/simple/CMakeLists.txt new file mode 100644 index 0000000000..1d2345f6e5 --- /dev/null +++ b/sdk/examples/simple/CMakeLists.txt @@ -0,0 +1,17 @@ +add_executable(search search.cpp) +target_link_libraries(search milvus_sdk pthread) +install(TARGETS search DESTINATION test) + + + + +add_executable(insert insert.cpp) +target_link_libraries(insert milvus_sdk pthread) +install(TARGETS insert DESTINATION test) + + + + +add_executable(delete delete.cpp) +target_link_libraries(delete milvus_sdk pthread) +install(TARGETS delete DESTINATION test) \ No newline at end of file diff --git a/sdk/examples/simple/delete.cpp b/sdk/examples/simple/delete.cpp new file mode 100644 index 0000000000..a05982e80e --- /dev/null +++ b/sdk/examples/simple/delete.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2019-2020 Zilliz. All rights reserved. +// +// Licensed 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. + +#include +#include +#include +#include + +#include "interface/ConnectionImpl.h" +#include "ip.h" + +int +main(int argc, char *argv[]) { + auto client = milvus::ConnectionImpl(); + milvus::ConnectParam connect_param; + connect_param.ip_address = IP; + connect_param.port = "19530"; + client.Connect(connect_param); + + std::vector delete_ids; + delete_ids.push_back(1); + delete_ids.push_back(2); + delete_ids.push_back(3); + client.DeleteEntityByID("collection0", delete_ids); + + return 0; +} + diff --git a/sdk/examples/simple/insert.cpp b/sdk/examples/simple/insert.cpp new file mode 100644 index 0000000000..5d4261060c --- /dev/null +++ b/sdk/examples/simple/insert.cpp @@ -0,0 +1,60 @@ +// Copyright (C) 2019-2020 Zilliz. All rights reserved. +// +// Licensed 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. + +#include +#include +#include +#include +#include + +#include "examples/utils/Utils.h" + +#include "grpc/ClientProxy.h" +#include "interface/ConnectionImpl.h" +#include "ip.h" + +const int N = 100; +const int DIM = 16; + +const milvus::FieldValue GetData() { + milvus::FieldValue value_map; + std::vector int32_data; + for (int i = 0; i < N; i++) { + int32_data.push_back(i); + } + std::vector vector_data; + for (int i = 0; i < N; i++) { + std::vector float_data(DIM, 10.25); + milvus::VectorData vectorData; + vectorData.float_data = float_data; + vector_data.push_back(vectorData); + } + + value_map.int32_value["INT32"] = int32_data; + value_map.vector_value["VECTOR"] = vector_data; + value_map.row_num = N; + return value_map; +} + +int +main(int argc, char* argv[]) { + auto client = milvus::ConnectionImpl(); + milvus::ConnectParam connect_param; + connect_param.ip_address = IP; + connect_param.port = "19530"; + client.Connect(connect_param); + std::vector ids_array; + auto data = GetData(); + for (int64_t i = 0; i < N; i++) { + ids_array.push_back(i); + } + auto status = client.Insert("collection1", "tag01", data, ids_array); +} diff --git a/sdk/examples/simple/ip.h b/sdk/examples/simple/ip.h new file mode 100644 index 0000000000..1558e70bea --- /dev/null +++ b/sdk/examples/simple/ip.h @@ -0,0 +1,16 @@ +// Copyright (C) 2019-2020 Zilliz. All rights reserved. +// +// Licensed 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. + + + +const std::string IP = "localhost"; +//const std::string IP = "192.168.2.9"; +//const std::string IP = "192.168.2.28"; \ No newline at end of file diff --git a/sdk/examples/simple/search.cpp b/sdk/examples/simple/search.cpp new file mode 100644 index 0000000000..64222f54b5 --- /dev/null +++ b/sdk/examples/simple/search.cpp @@ -0,0 +1,68 @@ + + +// Copyright (C) 2019-2020 Zilliz. All rights reserved. +// +// Licensed 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. +#include +#include "interface/ConnectionImpl.h" +#include "include/MilvusApi.h" +#include "grpc/ClientProxy.h" +#include "interface/ConnectionImpl.h" +#include "ip.h" + +const int TOP_K = 10; + +int main(int argc , char**argv) { + auto client = milvus::ConnectionImpl(); + milvus::ConnectParam connect_param; + connect_param.ip_address = IP; + connect_param.port = "19530"; + client.Connect(connect_param); + std::vector ids_array; + std::vector partition_list; + partition_list.emplace_back("partition-1"); + partition_list.emplace_back("partition-2"); + partition_list.emplace_back("partition-3"); + + milvus::VectorParam vectorParam; + milvus::VectorData vectorData; + std::vector float_data; + for (int i = 0; i < 100; ++i) { + float_data.emplace_back(i); + } + + vectorData.float_data = float_data; + + std::vector vector_records; + for (int j = 0; j < 10; ++j) { + vector_records.emplace_back(vectorData); + } + + nlohmann::json vector_param_json; + vector_param_json["num_queries"] = 1; + vector_param_json["topK"] = TOP_K; + vector_param_json["field_name"] = "fakevec"; + std::string vector_param_json_string = vector_param_json.dump(); + + vectorParam.json_param = vector_param_json_string; + vectorParam.vector_records = vector_records; + + milvus::TopKQueryResult result; + + + auto t1 = std::chrono::high_resolution_clock::now(); + auto status = client.Search("collection1", partition_list, "dsl", vectorParam, result); + + auto t2 = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(t2 - t1).count(); + + std::cout << "Query run time: " << duration/1000.0 << "ms" << std::endl; + return 0; +}