Skip to content

Commit 2084d8f

Browse files
Added a random cancellation configuration for performance tests
Make a new network for every test run. Relates-To: OLPEDGE-1156 Signed-off-by: Mykhailo Kuchma <[email protected]>
1 parent 9944cf1 commit 2084d8f

File tree

2 files changed

+73
-53
lines changed

2 files changed

+73
-53
lines changed

tests/performance/MemoryTest.cpp

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ using TestFunction = std::function<void(uint8_t thread_id)>;
4242

4343
struct TestConfiguration {
4444
std::string configuration_name;
45-
std::uint16_t requests_per_second;
46-
std::uint8_t calling_thread_count;
47-
std::uint8_t task_scheduler_capacity;
48-
std::chrono::seconds runtime;
45+
std::uint16_t requests_per_second = 3;
46+
std::uint8_t calling_thread_count = 5;
47+
std::uint8_t task_scheduler_capacity = 5;
48+
std::chrono::seconds runtime = std::chrono::minutes(5);
4949
CacheFactory cache_factory;
50+
bool with_errors = false;
51+
bool with_timeouts = false;
52+
float cancelation_chance = 0.f;
5053
};
5154

5255
std::ostream& operator<<(std::ostream& os, const TestConfiguration& config) {
@@ -62,15 +65,20 @@ constexpr std::chrono::milliseconds GetSleepPeriod(uint32_t requests) {
6265
return std::chrono::milliseconds(1000 / requests);
6366
}
6467

68+
bool ShouldCancel(const TestConfiguration& configuration) {
69+
float cancel_chance = configuration.cancelation_chance;
70+
cancel_chance = std::min(cancel_chance, 1.f);
71+
cancel_chance = std::max(cancel_chance, 0.f);
72+
int thresold = static_cast<int>(cancel_chance * 100);
73+
return rand() % 100 <= thresold;
74+
}
75+
6576
constexpr auto kLogTag = "MemoryTest";
6677
const olp::client::HRN kCatalog("hrn:here:data::olp-here-test:testhrn");
6778
const std::string kVersionedLayerId("versioned_test_layer");
6879

6980
class MemoryTest : public ::testing::TestWithParam<TestConfiguration> {
7081
public:
71-
static void SetUpTestSuite();
72-
static void TearDownTestSuite();
73-
7482
olp::http::NetworkProxySettings GetLocalhostProxySettings();
7583
olp::client::OlpClientSettings CreateCatalogClientSettings();
7684

@@ -80,9 +88,9 @@ class MemoryTest : public ::testing::TestWithParam<TestConfiguration> {
8088
void StartThreads(TestFunction test_body);
8189
void ReportError(const olp::client::ApiError& error);
8290

83-
protected:
84-
static std::shared_ptr<olp::http::Network> s_network;
91+
void RandomlyCancel(olp::client::CancellationToken token);
8592

93+
protected:
8694
std::atomic<olp::http::RequestId> request_counter_;
8795
std::vector<std::thread> client_threads_;
8896

@@ -94,17 +102,6 @@ class MemoryTest : public ::testing::TestWithParam<TestConfiguration> {
94102
std::map<int, int> errors_;
95103
};
96104

97-
std::shared_ptr<olp::http::Network> MemoryTest::s_network;
98-
99-
void MemoryTest::SetUpTestSuite() {
100-
auto network = std::make_shared<Http2HttpNetworkWrapper>();
101-
network->WithErrors(false);
102-
network->WithTimeouts(false);
103-
s_network = std::move(network);
104-
}
105-
106-
void MemoryTest::TearDownTestSuite() { s_network.reset(); }
107-
108105
olp::http::NetworkProxySettings MemoryTest::GetLocalhostProxySettings() {
109106
return olp::http::NetworkProxySettings()
110107
.WithHostname("localhost")
@@ -115,7 +112,7 @@ olp::http::NetworkProxySettings MemoryTest::GetLocalhostProxySettings() {
115112
}
116113

117114
olp::client::OlpClientSettings MemoryTest::CreateCatalogClientSettings() {
118-
auto parameter = GetParam();
115+
const auto& parameter = GetParam();
119116

120117
std::shared_ptr<olp::thread::TaskScheduler> task_scheduler = nullptr;
121118
if (parameter.task_scheduler_capacity != 0) {
@@ -124,13 +121,17 @@ olp::client::OlpClientSettings MemoryTest::CreateCatalogClientSettings() {
124121
parameter.task_scheduler_capacity);
125122
}
126123

124+
auto network = std::make_shared<Http2HttpNetworkWrapper>();
125+
network->WithErrors(parameter.with_errors);
126+
network->WithTimeouts(parameter.with_timeouts);
127+
127128
olp::client::AuthenticationSettings auth_settings;
128129
auth_settings.provider = []() { return "invalid"; };
129130

130131
olp::client::OlpClientSettings client_settings;
131132
client_settings.authentication_settings = auth_settings;
132133
client_settings.task_scheduler = task_scheduler;
133-
client_settings.network_request_handler = s_network;
134+
client_settings.network_request_handler = std::move(network);
134135
client_settings.proxy_settings = GetLocalhostProxySettings();
135136
client_settings.cache =
136137
parameter.cache_factory ? parameter.cache_factory() : nullptr;
@@ -168,11 +169,6 @@ void MemoryTest::TearDown() {
168169

169170
size_t total_requests = success_responses_.load() + failed_responses_.load();
170171
EXPECT_EQ(total_requests_.load(), total_requests);
171-
172-
// Reset network flags
173-
auto& network = dynamic_cast<Http2HttpNetworkWrapper&>(*s_network);
174-
network.WithErrors(false);
175-
network.WithTimeouts(false);
176172
}
177173

178174
void MemoryTest::StartThreads(TestFunction test_body) {
@@ -195,6 +191,14 @@ void MemoryTest::ReportError(const olp::client::ApiError& error) {
195191
}
196192
}
197193

194+
void MemoryTest::RandomlyCancel(olp::client::CancellationToken token) {
195+
const auto& parameter = GetParam();
196+
if (ShouldCancel(parameter)) {
197+
std::this_thread::sleep_for(std::chrono::microseconds(rand() % 3000));
198+
token.Cancel();
199+
}
200+
}
201+
198202
///
199203
/// CatalogClient
200204
///
@@ -216,7 +220,7 @@ TEST_P(MemoryTest, ReadLatestVersionCatalogClient) {
216220
olp::dataservice::read::CatalogVersionRequest().WithStartVersion(
217221
start_version);
218222
total_requests_.fetch_add(1);
219-
service_client.GetLatestVersion(
223+
auto token = service_client.GetLatestVersion(
220224
request,
221225
[&](olp::dataservice::read::CatalogVersionResponse response) {
222226
if (response.IsSuccessful()) {
@@ -227,6 +231,8 @@ TEST_P(MemoryTest, ReadLatestVersionCatalogClient) {
227231
}
228232
});
229233

234+
RandomlyCancel(std::move(token));
235+
230236
std::this_thread::sleep_for(
231237
GetSleepPeriod(parameter.requests_per_second));
232238
}
@@ -246,7 +252,7 @@ TEST_P(MemoryTest, ReadMetadataCatalogClient) {
246252
while (end_timestamp > std::chrono::steady_clock::now()) {
247253
auto request = olp::dataservice::read::CatalogRequest();
248254
total_requests_.fetch_add(1);
249-
service_client.GetCatalog(
255+
auto token = service_client.GetCatalog(
250256
request, [&](olp::dataservice::read::CatalogResponse response) {
251257
if (response.IsSuccessful()) {
252258
success_responses_.fetch_add(1);
@@ -256,6 +262,8 @@ TEST_P(MemoryTest, ReadMetadataCatalogClient) {
256262
}
257263
});
258264

265+
RandomlyCancel(std::move(token));
266+
259267
std::this_thread::sleep_for(
260268
GetSleepPeriod(parameter.requests_per_second));
261269
}
@@ -294,7 +302,7 @@ TEST_P(MemoryTest, ReadNPartitionsFromVersionedLayer) {
294302
auto request = olp::dataservice::read::DataRequest().WithPartitionId(
295303
std::to_string(partition_id));
296304
total_requests_.fetch_add(1);
297-
service_client.GetData(
305+
auto token = service_client.GetData(
298306
request, [&](olp::dataservice::read::DataResponse response) {
299307
if (response.IsSuccessful()) {
300308
success_responses_.fetch_add(1);
@@ -304,6 +312,8 @@ TEST_P(MemoryTest, ReadNPartitionsFromVersionedLayer) {
304312
}
305313
});
306314

315+
RandomlyCancel(std::move(token));
316+
307317
std::this_thread::sleep_for(
308318
GetSleepPeriod(parameter.requests_per_second));
309319
}
@@ -338,7 +348,7 @@ TEST_P(MemoryTest, PrefetchPartitionsFromVersionedLayer) {
338348
.WithVersion(17)
339349
.WithTileKeys(tile_keys);
340350
total_requests_.fetch_add(1);
341-
service_client.PrefetchTiles(
351+
auto token = service_client.PrefetchTiles(
342352
std::move(request),
343353
[&](olp::dataservice::read::PrefetchTilesResponse response) {
344354
if (response.IsSuccessful()) {
@@ -349,6 +359,8 @@ TEST_P(MemoryTest, PrefetchPartitionsFromVersionedLayer) {
349359
}
350360
});
351361

362+
RandomlyCancel(std::move(token));
363+
352364
std::this_thread::sleep_for(
353365
GetSleepPeriod(parameter.requests_per_second));
354366
}
@@ -359,32 +371,36 @@ TEST_P(MemoryTest, PrefetchPartitionsFromVersionedLayer) {
359371
* 10 hours stability test with default constructed cache.
360372
*/
361373
TestConfiguration LongRunningTest() {
362-
return TestConfiguration{"10h_test", 3, 5, 5, std::chrono::hours(10),
363-
nullptr};
374+
TestConfiguration configuration;
375+
configuration.configuration_name = "10h_test";
376+
configuration.with_errors = true;
377+
configuration.with_timeouts = true;
378+
configuration.runtime = std::chrono::hours(10);
379+
configuration.cancelation_chance = 0.25f;
380+
return configuration;
364381
}
365382

366383
/*
367384
* Short 5 minutes test to collect SDK allocations without cache.
368385
*/
369386
TestConfiguration ShortRunningTestWithNullCache() {
370-
auto cache_factory = []() { return std::make_shared<NullCache>(); };
371-
return TestConfiguration{
372-
"short_test_null_cache", 3, 5, 5, std::chrono::minutes(5),
373-
std::move(cache_factory)};
387+
TestConfiguration configuration;
388+
configuration.configuration_name = "short_test_null_cache";
389+
configuration.cache_factory = []() { return std::make_shared<NullCache>(); };
390+
return configuration;
374391
}
375392

376393
/*
377394
* Short 5 minutes test to collect SDK allocations with in memory cache.
378395
*/
379396
TestConfiguration ShortRunningTestWithMemoryCache() {
380397
using namespace olp;
381-
auto cache_factory = []() {
382-
cache::CacheSettings settings;
383-
return client::OlpClientSettingsFactory::CreateDefaultCache(settings);
398+
TestConfiguration configuration;
399+
configuration.configuration_name = "short_test_memory_cache";
400+
configuration.cache_factory = []() {
401+
return client::OlpClientSettingsFactory::CreateDefaultCache({});
384402
};
385-
return TestConfiguration{
386-
"short_test_memory_cache", 3, 5, 5, std::chrono::minutes(5),
387-
std::move(cache_factory)};
403+
return configuration;
388404
}
389405

390406
/*
@@ -393,18 +409,20 @@ TestConfiguration ShortRunningTestWithMemoryCache() {
393409
*/
394410
TestConfiguration ShortRunningTestWithDiskCache() {
395411
using namespace olp;
396-
auto cache_factory = []() {
397-
cache::CacheSettings settings;
398-
auto location = CustomParameters::getArgument("cache_location");
399-
if (location.empty()) {
400-
location = utils::Dir::TempDirectory() + "/performance_test";
401-
}
402-
settings.disk_path = location;
412+
413+
cache::CacheSettings settings;
414+
auto location = CustomParameters::getArgument("cache_location");
415+
if (location.empty()) {
416+
location = utils::Dir::TempDirectory() + "/performance_test";
417+
}
418+
settings.disk_path = location;
419+
420+
TestConfiguration configuration;
421+
configuration.configuration_name = "short_test_disk_cache";
422+
configuration.cache_factory = [settings]() {
403423
return client::OlpClientSettingsFactory::CreateDefaultCache(settings);
404424
};
405-
return TestConfiguration{
406-
"short_test_disk_cache", 3, 5, 5, std::chrono::minutes(5),
407-
std::move(cache_factory)};
425+
return configuration;
408426
}
409427

410428
std::vector<TestConfiguration> Configurations() {

tests/performance/NetworkWrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#pragma once
2121

22+
#include <memory>
23+
2224
#include <olp/core/http/Network.h>
2325

2426
/*

0 commit comments

Comments
 (0)