Skip to content

Commit

Permalink
Performance testing on android.
Browse files Browse the repository at this point in the history
Added data validation.
Fixed Android proxy.
Fixed network initialization.
  • Loading branch information
mykhailo-kuchma committed May 4, 2020
1 parent d5630ed commit b1088b4
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 78 deletions.
5 changes: 4 additions & 1 deletion external/boost/CMakeLists.txt.boost.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ include(ExternalProject)
ExternalProject_Add(boost-download
GIT_REPOSITORY @OLP_SDK_CPP_BOOST_URL@
GIT_TAG @OLP_SDK_CPP_BOOST_TAG@
GIT_SUBMODULES libs/any
GIT_SUBMODULES libs/algorithm
libs/any
libs/assert
libs/config
libs/container_hash
libs/core
libs/detail
libs/exception
libs/format
libs/function_types
libs/headers
Expand All @@ -43,6 +45,7 @@ ExternalProject_Add(boost-download
libs/predef
libs/preprocessor
libs/random
libs/range
libs/serialization
libs/smart_ptr
libs/static_assert
Expand Down
16 changes: 8 additions & 8 deletions olp-cpp-sdk-core/src/http/android/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ public Request(
this.proxyPort = proxyPort;

switch (proxyType) {
case 0:
case 1:
this.proxyType = Proxy.Type.HTTP;
break;
case 4:
case 2:
this.proxyType = Proxy.Type.SOCKS;
break;
case 3:
case 4:
case 5:
case 6:
case 7:
this.proxyType = Proxy.Type.SOCKS;
Log.w(
LOGTAG,
Expand Down Expand Up @@ -318,7 +318,7 @@ protected Void doInBackground(Request... requests) {
conn.setRequestProperty("Connection", "Close");
}

Log.d(LOGTAG, "Printing Request Headers...\n");
//Log.d(LOGTAG, "Printing Request Headers...\n");
uploadedContentSize += calculateHeadersSize(conn.getRequestProperties());

conn.setDoInput(true);
Expand All @@ -327,7 +327,7 @@ protected Void doInBackground(Request... requests) {
if (request.postData() != null) {
conn.setDoOutput(true);
conn.getOutputStream().write(request.postData());
Log.d(LOGTAG, "Uploaded data length:" + request.postData().length);
//Log.d(LOGTAG, "Uploaded data length:" + request.postData().length);
uploadedContentSize += request.postData().length;
} else {
conn.setDoOutput(false);
Expand Down Expand Up @@ -385,7 +385,7 @@ protected Void doInBackground(Request... requests) {
}
}

Log.d(LOGTAG, "Printing Response Headers...\n");
//Log.d(LOGTAG, "Printing Response Headers...\n");
downloadContentSize += calculateHeadersSize(conn.getHeaderFields());

int contentSize = conn.getContentLength();
Expand Down Expand Up @@ -560,7 +560,7 @@ private final int calculateHeadersSize(Map<String, List<String>> headers) throws
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String header = entry.getKey();
List<String> values = entry.getValue();
Log.d(LOGTAG, header + ":" + values);
//Log.d(LOGTAG, header + ":" + values);
if(header != null) {
size += header.length();
}
Expand Down
12 changes: 8 additions & 4 deletions olp-cpp-sdk-core/src/http/android/NetworkAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,15 @@ bool NetworkAndroid::Initialize() {
}

run_thread_ = std::make_unique<std::thread>(NetworkAndroid::Run, this);

initialized_ = true;

{
if (!started_) {
run_thread_ready_cv_.wait(lock);
}
}

initialized_ = true;
return true;
}

Expand Down Expand Up @@ -1048,18 +1050,20 @@ RequestId NetworkAndroid::GenerateNextRequestId() {
NetworkAndroid::RequestData::RequestData(
Network::Callback callback, Network::HeaderCallback header_callback,
Network::DataCallback data_callback, const std::string& url,
const std::shared_ptr<std::ostream>& payload)
Network::Payload payload)
: callback(callback),
header_callback(header_callback),
data_callback(data_callback),
url(url),
payload(payload),
obj(nullptr) {}
obj(nullptr),
count(0),
offset(0) {}

NetworkAndroid::ResponseData::ResponseData(
RequestId id, Network::Callback callback, int status, int uploaded_bytes,
int downloaded_bytes, const char* error, const char* content_type,
jlong count, jlong offset, std::shared_ptr<std::ostream> payload)
jlong count, jlong offset, Network::Payload payload)
: id(id),
callback(callback),
payload(payload),
Expand Down
8 changes: 4 additions & 4 deletions olp-cpp-sdk-core/src/http/android/NetworkAndroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class NetworkAndroid : public Network {
RequestData(Network::Callback callback,
Network::HeaderCallback header_callback,
Network::DataCallback data_callback, const std::string& url,
const std::shared_ptr<std::ostream>& payload);
Network::Payload payload);

void Reinitialize() {
obj = nullptr;
Expand All @@ -142,7 +142,7 @@ class NetworkAndroid : public Network {
Network::HeaderCallback header_callback;
Network::DataCallback data_callback;
std::string url;
std::shared_ptr<std::ostream> payload;
Network::Payload payload;
jobject obj;
jlong count;
jlong offset;
Expand All @@ -155,12 +155,12 @@ class NetworkAndroid : public Network {
ResponseData(RequestId id, Network::Callback callback, int status,
int uploaded_bytes, int downloaded_bytes, const char* error,
const char* content_type, jlong count, jlong offset,
std::shared_ptr<std::ostream> payload);
Network::Payload payload);
bool IsValid() const { return (callback != nullptr); }

RequestId id = static_cast<RequestId>(RequestIdConstants::RequestIdInvalid);
Network::Callback callback;
std::shared_ptr<std::ostream> payload;
Network::Payload payload;
std::string error;
std::string content_type;
int status = 0;
Expand Down
47 changes: 34 additions & 13 deletions tests/performance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
# SPDX-License-Identifier: Apache-2.0
# License-Filename: LICENSE

if (ANDROID OR IOS)
message(STATUS "Currently the performance test runner for mobile platforms is not supported")
return()
endif()

set(OLP_SDK_PERFORMANCE_TESTS_SOURCES
./MemoryTest.cpp
./MemoryTestBase.h
Expand All @@ -28,11 +23,37 @@ set(OLP_SDK_PERFORMANCE_TESTS_SOURCES
./PrefetchTest.cpp
)

add_executable(olp-cpp-sdk-performance-tests ${OLP_SDK_PERFORMANCE_TESTS_SOURCES})
target_link_libraries(olp-cpp-sdk-performance-tests
PRIVATE
custom-params
gtest_main
olp-cpp-sdk-authentication
olp-cpp-sdk-dataservice-read
)
if (ANDROID OR IOS)
set(OLP_SDK_PERFORMANCE_TESTS_LIB olp-cpp-sdk-performance-tests-lib)

add_library(${OLP_SDK_PERFORMANCE_TESTS_LIB} ${OLP_SDK_PERFORMANCE_TESTS_SOURCES})
target_link_libraries(${OLP_SDK_PERFORMANCE_TESTS_LIB}
PRIVATE
custom-params
gtest
olp-cpp-sdk-authentication
olp-cpp-sdk-dataservice-read
)

if (ANDROID)
include(${CMAKE_SOURCE_DIR}/cmake/android/gen_android_test.cmake)
gen_android_test_runner(olp-cpp-sdk-performance-tests ${OLP_SDK_PERFORMANCE_TESTS_LIB})
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/android ${CMAKE_CURRENT_BINARY_DIR}/android)
else()
include(${CMAKE_SOURCE_DIR}/cmake/ios/gen_ios_test.cmake)
gen_ios_test_runner(olp-cpp-sdk-performance-tests ${OLP_SDK_PERFORMANCE_TESTS_LIB})
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/ios ${CMAKE_CURRENT_BINARY_DIR}/ios)
endif()

else()

add_executable(olp-cpp-sdk-performance-tests ${OLP_SDK_PERFORMANCE_TESTS_SOURCES})
target_link_libraries(olp-cpp-sdk-performance-tests
PRIVATE
custom-params
gtest_main
olp-cpp-sdk-authentication
olp-cpp-sdk-dataservice-read
)

endif()
89 changes: 42 additions & 47 deletions tests/performance/MemoryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,52 @@
#include <chrono>
#include <memory>

#include <boost/uuid/detail/sha1.hpp>
#include <boost/algorithm/hex.hpp>

#include <gtest/gtest.h>
#include <olp/core/client/HRN.h>
#include <olp/core/logging/Log.h>
#include <olp/dataservice/read/VersionedLayerClient.h>

#include "MemoryTestBase.h"

std::string ComputeSha1(const char* buffer, size_t length) {
using boost::uuids::detail::sha1;
sha1 hash;
sha1::digest_type digest;
sha1::digest_type digest_rotated;

hash.process_bytes(buffer, length);
hash.get_digest(digest);

// rotate bytes
for (int i = 0; i < 5; ++i)
{
const char* tmp = reinterpret_cast<char*>(&digest);
char* res = reinterpret_cast<char*>(&digest_rotated);
res[i*4] = tmp[i*4+3];
res[i*4+1] = tmp[i*4+2];
res[i*4+2] = tmp[i*4+1];
res[i*4+3] = tmp[i*4];
}

const auto charDigest = reinterpret_cast<const char*>(&digest_rotated);
std::string result;
boost::algorithm::hex(charDigest, charDigest + sizeof(sha1::digest_type),
std::back_inserter(result));
return result;
}

bool Validate(const std::vector<unsigned char>& buffer) {
// 40 first bytes is a string representation of hash
const char* bytes = reinterpret_cast<const char*>( buffer.data() );
std::string hash(bytes, bytes + 40);
std::string computed_hash = ComputeSha1(bytes + 40, buffer.size() - 40);
OLP_SDK_LOG_CRITICAL_INFO_F("HASH", "HASHES: %s %s", hash.c_str(), computed_hash.c_str());
return hash == computed_hash;
}

namespace {
using TestFunction = std::function<void(uint8_t thread_id)>;

Expand Down Expand Up @@ -183,53 +222,9 @@ TEST_P(MemoryTest, ReadNPartitionsFromVersionedLayer) {
request, [&](olp::dataservice::read::DataResponse response) {
if (response.IsSuccessful()) {
success_responses_.fetch_add(1);
} else {
failed_responses_.fetch_add(1);
ReportError(response.GetError());
}
});

RandomlyCancel(std::move(token));

std::this_thread::sleep_for(
GetSleepPeriod(parameter.requests_per_second));
}
});
}

TEST_P(MemoryTest, PrefetchPartitionsFromVersionedLayer) {
// Enable only errors to have a short output.
olp::logging::Log::setLevel(olp::logging::Level::Warning);

const auto& parameter = GetParam();

auto settings = CreateCatalogClientSettings();

StartThreads([=](uint8_t /*thread_id*/) {
olp::dataservice::read::VersionedLayerClient service_client(
kCatalog, kVersionedLayerId, boost::none, settings);

const auto end_timestamp =
std::chrono::steady_clock::now() + parameter.runtime;

while (end_timestamp > std::chrono::steady_clock::now()) {
const auto level = 10;
const auto tile_count = 1 << level;

std::vector<olp::geo::TileKey> tile_keys = {
olp::geo::TileKey::FromRowColumnLevel(rand() % tile_count,
rand() % tile_count, level)};

auto request = olp::dataservice::read::PrefetchTilesRequest()
.WithMaxLevel(level + 2)
.WithMinLevel(level)
.WithTileKeys(tile_keys);
total_requests_.fetch_add(1);
auto token = service_client.PrefetchTiles(
std::move(request),
[&](olp::dataservice::read::PrefetchTilesResponse response) {
if (response.IsSuccessful()) {
success_responses_.fetch_add(1);
if (!Validate(*response.GetResult())) {
abort();
}
} else {
failed_responses_.fetch_add(1);
ReportError(response.GetError());
Expand Down
5 changes: 5 additions & 0 deletions tests/performance/MemoryTestBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ class MemoryTestBase : public ::testing::TestWithParam<Param> {
protected:
olp::http::NetworkProxySettings GetLocalhostProxySettings() {
return olp::http::NetworkProxySettings()
#if ANDROID
.WithHostname("10.0.2.2")
#else
.WithHostname("localhost")
#endif
.WithPort(3000)
.WithUsername("test_user")
.WithPassword("test_password")
Expand Down Expand Up @@ -78,6 +82,7 @@ class MemoryTestBase : public ::testing::TestWithParam<Param> {
client_settings.cache =
parameter.cache_factory ? parameter.cache_factory() : nullptr;
client_settings.retry_settings.timeout = 1;
//client_settings.retry_settings.max_attempts = 0;

return client_settings;
}
Expand Down
9 changes: 8 additions & 1 deletion tests/utils/olp_server/blob_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
* License-Filename: LICENSE
*/

var crypto = require('crypto')

function computeHash(data) {
return crypto.createHash('sha1').update(data).digest('hex').toUpperCase();
}

function generateGetBlobApiResponse(request) {
const layer = request[1]
const dataHandle = request[2] // ignored
Expand All @@ -30,7 +36,8 @@ function generateGetBlobApiResponse(request) {
response += characters.charAt(Math.floor(Math.random() * charactersLength));
}

return response;
//console.log(computeHash(response))
return computeHash(response) + response;
}

const methods = [
Expand Down

0 comments on commit b1088b4

Please sign in to comment.