Skip to content

Commit 0354d97

Browse files
Add prefetch memory tests. (#800)
Test performs a N parallel requests with configurable number of tiles Test is designed to estimate the memory consumption, and compare different cache settings. Relates-To: OLPEDGE-1804 Signed-off-by: Mykhailo Kuchma <ext-mykhailo.kuchma@here.com>
1 parent ee1732c commit 0354d97

File tree

4 files changed

+212
-8
lines changed

4 files changed

+212
-8
lines changed

tests/performance/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ set(OLP_SDK_PERFORMANCE_TESTS_SOURCES
2525
./MemoryTestBase.h
2626
./NullCache.h
2727
./NetworkWrapper.h
28+
./PrefetchTest.cpp
2829
)
2930

3031
add_executable(olp-cpp-sdk-performance-tests ${OLP_SDK_PERFORMANCE_TESTS_SOURCES})

tests/performance/MemoryTest.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@
2121
#include <memory>
2222

2323
#include <gtest/gtest.h>
24-
#include <olp/core/cache/KeyValueCache.h>
2524
#include <olp/core/client/HRN.h>
2625
#include <olp/core/logging/Log.h>
27-
#include <olp/core/porting/make_unique.h>
28-
#include <olp/dataservice/read/CatalogClient.h>
2926
#include <olp/dataservice/read/VersionedLayerClient.h>
3027

3128
#include "MemoryTestBase.h"

tests/performance/MemoryTestBase.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,23 @@ class MemoryTestBase : public ::testing::TestWithParam<Param> {
8686
/*
8787
* Set error flags to test configuration.
8888
*/
89-
void SetErrorFlags(TestBaseConfiguration& configuration) {
89+
inline void SetErrorFlags(TestBaseConfiguration& configuration) {
9090
configuration.with_http_errors = true;
9191
configuration.with_network_timeouts = true;
9292
}
9393

9494
/*
9595
* Set null cache configuration. Cache does not perform any operations.
9696
*/
97-
void SetNullCacheConfiguration(TestBaseConfiguration& configuration) {
97+
inline void SetNullCacheConfiguration(TestBaseConfiguration& configuration) {
9898
configuration.cache_factory = []() { return std::make_shared<NullCache>(); };
9999
}
100100

101101
/*
102102
* Set default cache configuration. A simple default cache with default
103103
* settings.
104104
*/
105-
void SetDefaultCacheConfiguration(TestBaseConfiguration& configuration) {
105+
inline void SetDefaultCacheConfiguration(TestBaseConfiguration& configuration) {
106106
configuration.cache_factory = []() {
107107
return olp::client::OlpClientSettingsFactory::CreateDefaultCache({});
108108
};
@@ -112,8 +112,8 @@ void SetDefaultCacheConfiguration(TestBaseConfiguration& configuration) {
112112
* Set a disk cache. Disk cache path is computed as `cache_location` env.
113113
* variable and /memory_test appended. Default cache settings.
114114
*/
115-
void SetDiskCacheConfiguration(TestBaseConfiguration& configuration) {
116-
olp::cache::CacheSettings settings;
115+
inline void SetDiskCacheConfiguration(TestBaseConfiguration& configuration,
116+
olp::cache::CacheSettings settings = {}) {
117117
auto location = CustomParameters::getArgument("cache_location");
118118
if (location.empty()) {
119119
location = olp::utils::Dir::TempDirectory() + "/memory_test";

tests/performance/PrefetchTest.cpp

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <chrono>
21+
#include <memory>
22+
23+
#include <gtest/gtest.h>
24+
#include <olp/core/client/HRN.h>
25+
#include <olp/core/logging/Log.h>
26+
#include <olp/dataservice/read/VersionedLayerClient.h>
27+
28+
#include "MemoryTestBase.h"
29+
30+
namespace {
31+
using TestFunction = std::function<void(uint8_t thread_id)>;
32+
33+
struct TestConfiguration : public TestBaseConfiguration {
34+
std::string configuration_name;
35+
std::uint8_t calling_thread_count = 5;
36+
std::uint16_t number_of_tiles = 100;
37+
};
38+
39+
std::ostream& operator<<(std::ostream& os, const TestConfiguration& config) {
40+
return os << "TestConfiguration("
41+
<< ".configuration_name=" << config.configuration_name
42+
<< ", .calling_thread_count=" << config.calling_thread_count
43+
<< ", .task_scheduler_capacity=" << config.task_scheduler_capacity
44+
<< ")";
45+
}
46+
47+
constexpr auto kLogTag = "PrefetchTest";
48+
const olp::client::HRN kCatalog("hrn:here:data::olp-here-test:testhrn");
49+
const std::string kVersionedLayerId("versioned_test_layer");
50+
51+
class PrefetchTest : public MemoryTestBase<TestConfiguration> {
52+
public:
53+
void SetUp() override;
54+
void TearDown() override;
55+
56+
void StartThreads(TestFunction test_body);
57+
void ReportError(const olp::client::ApiError& error);
58+
59+
protected:
60+
std::atomic<olp::http::RequestId> request_counter_;
61+
std::vector<std::thread> client_threads_;
62+
63+
std::atomic_size_t total_requests_{0};
64+
std::atomic_size_t success_responses_{0};
65+
std::atomic_size_t failed_responses_{0};
66+
67+
std::mutex errors_mutex_;
68+
std::map<int, int> errors_;
69+
};
70+
71+
void PrefetchTest::SetUp() {
72+
using namespace olp;
73+
request_counter_.store(
74+
static_cast<http::RequestId>(http::RequestIdConstants::RequestIdMin));
75+
total_requests_.store(0);
76+
success_responses_.store(0);
77+
failed_responses_.store(0);
78+
errors_.clear();
79+
}
80+
81+
void PrefetchTest::TearDown() {
82+
for (auto& thread : client_threads_) {
83+
thread.join();
84+
}
85+
client_threads_.clear();
86+
87+
OLP_SDK_LOG_CRITICAL_INFO_F(kLogTag,
88+
"Test finished, total requests %zu, succeed "
89+
"responses %zu, failed responses %zu",
90+
total_requests_.load(), success_responses_.load(),
91+
failed_responses_.load());
92+
93+
for (const auto& error : errors_) {
94+
OLP_SDK_LOG_CRITICAL_INFO_F(kLogTag, "error %d - count %d", error.first,
95+
error.second);
96+
}
97+
98+
size_t total_requests = success_responses_.load() + failed_responses_.load();
99+
EXPECT_EQ(total_requests_.load(), total_requests);
100+
}
101+
102+
void PrefetchTest::StartThreads(TestFunction test_body) {
103+
const auto& parameter = GetParam();
104+
105+
for (uint8_t i = 0; i < parameter.calling_thread_count; ++i) {
106+
client_threads_.emplace_back(std::thread(test_body, i));
107+
}
108+
}
109+
110+
void PrefetchTest::ReportError(const olp::client::ApiError& error) {
111+
const auto error_code = static_cast<int>(error.GetErrorCode());
112+
113+
std::lock_guard<std::mutex> lock(errors_mutex_);
114+
const auto error_it = errors_.find(error_code);
115+
if (error_it != errors_.end()) {
116+
error_it->second++;
117+
} else {
118+
errors_[error_code] = 1;
119+
}
120+
}
121+
122+
///
123+
/// VersionedLayerClient
124+
///
125+
TEST_P(PrefetchTest, PrefetchPartitionsFromVersionedLayer) {
126+
const auto& parameter = GetParam();
127+
128+
auto settings = CreateCatalogClientSettings();
129+
130+
StartThreads([=](uint8_t thread_id) {
131+
olp::dataservice::read::VersionedLayerClient service_client(
132+
kCatalog, kVersionedLayerId, boost::none, settings);
133+
134+
const auto level = 10;
135+
136+
// Generate N tiles with diagonal col/rows (unique for each thread id)
137+
std::vector<olp::geo::TileKey> tile_keys(parameter.number_of_tiles);
138+
const auto offset = thread_id * parameter.number_of_tiles;
139+
for (auto index = 0; index < parameter.number_of_tiles; ++index) {
140+
tile_keys[index] =
141+
olp::geo::TileKey::FromRowColumnLevel(offset + index, offset, level);
142+
}
143+
144+
auto request = olp::dataservice::read::PrefetchTilesRequest()
145+
.WithMaxLevel(level + 4)
146+
.WithMinLevel(level)
147+
.WithTileKeys(tile_keys);
148+
149+
total_requests_.fetch_add(1);
150+
auto result = service_client.PrefetchTiles(std::move(request));
151+
auto response = result.GetFuture().get();
152+
153+
if (response.IsSuccessful()) {
154+
success_responses_.fetch_add(1);
155+
} else {
156+
failed_responses_.fetch_add(1);
157+
ReportError(response.GetError());
158+
}
159+
});
160+
}
161+
162+
/*
163+
* Test to collect SDK allocations without cache.
164+
*/
165+
TestConfiguration ShortRunningTestWithNullCache() {
166+
TestConfiguration configuration;
167+
SetNullCacheConfiguration(configuration);
168+
configuration.configuration_name = "short_test_null_cache";
169+
return configuration;
170+
}
171+
172+
/*
173+
* Test to collect SDK allocations with in memory cache.
174+
*/
175+
TestConfiguration ShortRunningTestWithMemoryCache() {
176+
TestConfiguration configuration;
177+
SetDefaultCacheConfiguration(configuration);
178+
configuration.configuration_name = "short_test_memory_cache";
179+
return configuration;
180+
}
181+
182+
/*
183+
* Test to collect SDK allocations with both in memory cache and disk cache.
184+
*/
185+
TestConfiguration ShortRunningTestWithMutableCache() {
186+
TestConfiguration configuration;
187+
SetDiskCacheConfiguration(configuration);
188+
configuration.configuration_name = "short_test_disk_cache";
189+
return configuration;
190+
}
191+
192+
std::vector<TestConfiguration> Configurations() {
193+
std::vector<TestConfiguration> configurations;
194+
configurations.emplace_back(ShortRunningTestWithNullCache());
195+
configurations.emplace_back(ShortRunningTestWithMemoryCache());
196+
configurations.emplace_back(ShortRunningTestWithMutableCache());
197+
return configurations;
198+
}
199+
200+
std::string TestName(const testing::TestParamInfo<TestConfiguration>& info) {
201+
return info.param.configuration_name;
202+
}
203+
204+
INSTANTIATE_TEST_SUITE_P(MemoryUsage, PrefetchTest,
205+
::testing::ValuesIn(Configurations()), TestName);
206+
} // namespace

0 commit comments

Comments
 (0)