Created
October 27, 2017 09:49
-
-
Save beeender/a2e5d02257df61eadf70909583358ec8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEST_CASE("results: benchmark") { | |
InMemoryTestFile config; | |
config.cache = false; | |
config.schema = Schema{ | |
{"object", { | |
{"int", PropertyType::Int}, | |
{"intIndexed", PropertyType::Int, false, true}, | |
{"string", PropertyType::String}, | |
{"stringIndexed", PropertyType::String, false, true}, | |
}}, | |
}; | |
auto realm = Realm::get_shared_realm(config); | |
auto table = realm->read_group().get_table("class_object"); | |
auto query_no_condition = table->where(); | |
auto query_condition = table->where().greater(0, 500); | |
auto query_condition_indexed = table->where().greater(1, 500); | |
auto query_string_condition = table->where().begins_with(2, "0"); | |
std::function<void(char*, const int)> gen_random = [](char* s, const int len) { | |
static const char alphanum[] = "0123456789" | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz"; | |
for (int i = 0; i < len; ++i) { | |
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; | |
} | |
s[len] = 0; | |
}; | |
std::function<void(int)> populate_data = [&](int data_size) { | |
realm->begin_transaction(); | |
table->add_empty_row(data_size); | |
for (int i = 0; i < data_size; i++) { | |
auto rand_num = (rand() % 10) * 100; | |
table->set_int(0, i, rand_num); | |
table->set_int(1, i, rand_num); | |
char rand_str[100+1]; | |
gen_random(rand_str, 100); | |
auto str = util::format("%1_%2", i, rand_str); | |
table->set_string(2, i, str); | |
table->set_string(3, i, str); | |
} | |
realm->commit_transaction(); | |
}; | |
std::function<void(std::function<void(void)>, std::function<void(void)>, size_t)> benchmark = | |
[](auto before_func, auto func, auto times) { | |
using namespace std::chrono; | |
milliseconds elapsed(0); | |
for (size_t i = 0; i < times; i++) { | |
milliseconds start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()); | |
before_func(); | |
func(); | |
milliseconds end = duration_cast<milliseconds>(system_clock::now().time_since_epoch()); | |
elapsed += end - start; | |
} | |
std::cout << "Time cost: " << elapsed.count() << std::endl; | |
}; | |
size_t loops = 1000; | |
size_t data_size = 3000; | |
std::cout << std::endl << "## data set: " << data_size << " loops: " << loops << std::endl; | |
populate_data(data_size); | |
SECTION("No condition query") { | |
std::cout << "### No condition query" << std::endl; | |
SECTION("size") | |
{ | |
std::cout << "#### size" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_no_condition); }, [&] { results.size(); }, loops); | |
} | |
SECTION("get(0)") | |
{ | |
std::cout << "#### get(0)" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_no_condition); }, [&] { results.get(0); }, loops); | |
} | |
} | |
SECTION("Conditions query, no index") { | |
std::cout << "### Conditions query, no index" << std::endl; | |
SECTION("size") | |
{ | |
std::cout << "#### size" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_condition); }, [&] { results.size(); }, loops); | |
} | |
SECTION("get(0)") | |
{ | |
std::cout << "#### get(0)" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_condition); }, [&] { results.get(0); }, loops); | |
} | |
} | |
SECTION("Conditions query, indexed") { | |
std::cout << "### Conditions query, indexed" << std::endl; | |
SECTION("size") | |
{ | |
std::cout << "#### size" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_condition_indexed); }, [&] { results.size(); }, loops); | |
} | |
SECTION("get(0)") | |
{ | |
std::cout << "#### get(0)" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_condition_indexed); }, [&] { results.get(0); }, loops); | |
} | |
} | |
SECTION("Conditions string query") { | |
std::cout << "### Conditions string query" << std::endl; | |
SECTION("size") | |
{ | |
std::cout << "#### size" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_string_condition); }, [&] { results.size(); }, loops); | |
} | |
SECTION("get(0)") | |
{ | |
std::cout << "#### get(0)" << std::endl; | |
Results results; | |
benchmark([&] { results = Results(realm, query_string_condition); }, [&] { results.get(0); }, loops); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment