Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Added a basic benchmark.

Kishore Nallanc302acb15902
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 88d2fbb4..c6990dd5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,8 +37,10 @@ link_directories(${CMAKE_SOURCE_DIR}/external/${ROCKSDB_NAME})
add_executable(typesense_test test/forarray_test.cpp test/art_test.cpp test/collection_test.cpp test/topster_test.cpp
${SRC_FILES})
add_executable(search ${HEADER_FILES} ${SRC_FILES} src/main/main.cpp)
+add_executable(benchmark ${HEADER_FILES} ${SRC_FILES} src/main/benchmark.cpp)
add_executable(typesense-server ${HEADER_FILES} ${SRC_FILES} src/main/server.cpp)
target_link_libraries(typesense_test for rocksdb gtest gtest_main)
target_link_libraries(search for pthread rocksdb)
+target_link_libraries(benchmark for pthread rocksdb)
target_link_libraries(typesense-server for curl h2o-evloop pthread rocksdb ssl crypto)
\ No newline at end of file
diff --git a/src/main/benchmark.cpp b/src/main/benchmark.cpp
new file mode 100644
index 00000000..87f9d4c4
--- /dev/null
+++ b/src/main/benchmark.cpp
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include <iostream>
+#include <fstream>
+#include <vector>
+#include <numeric>
+#include <chrono>
+#include <art.h>
+#include <unordered_map>
+#include <queue>
+#include "string_utils.h"
+#include "collection.h"
+#include "collection_manager.h"
+
+using namespace std;
+
+int main() {
+ system("rm -rf /tmp/typesense-data && mkdir -p /tmp/typesense-data");
+
+ std::vector<field> fields_to_index = {field("title", field_types::STRING)};
+ std::vector<std::string> rank_fields = {"points"};
+ Store *store = new Store("/tmp/typesense-data");
+ CollectionManager & collectionManager = CollectionManager::get_instance();
+ collectionManager.init(store);
+
+ Collection *collection = collectionManager.get_collection("collection");
+ if(collection == nullptr) {
+ collection = collectionManager.create_collection("collection", fields_to_index, rank_fields);
+ }
+
+ std::ifstream infile("/Users/kishore/Downloads/hnstories.jsonl");
+
+ std::string json_line;
+
+ while (std::getline(infile, json_line)) {
+ collection->add(json_line);
+ }
+
+ infile.close();
+ cout << "FINISHED INDEXING!" << endl << flush;
+
+ std::vector<std::string> search_fields = {"title"};
+
+ std::vector<string> queries = {"the", "and", "to", "of", "in"};
+ auto counter = 0;
+ uint64_t results_total = 0; // to prevent optimizations!
+
+ auto begin = std::chrono::high_resolution_clock::now();
+
+ while(counter < 3000) {
+ auto i = counter % 5;
+ auto results = collection->search(queries[i], search_fields, 1, 100);
+ results_total += results.size();
+ counter++;
+ }
+
+ long long int timeMillis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - begin).count();
+ cout << "Time taken: " << timeMillis << "ms" << endl;
+ cout << "Total: " << results_total << endl;
+ return 0;
+}
\ No newline at end of file