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