Added a basic benchmark.

This commit is contained in:
Kishore Nallan 2017-01-11 20:48:04 +05:30
parent 554534f406
commit c302acb159
2 changed files with 62 additions and 0 deletions

View File

@ -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)

60
src/main/benchmark.cpp Normal file
View File

@ -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;
}