implemented chachastream performance tests(results are pretty good: only 2% slowdown of load/save operations on wallet)

This commit is contained in:
cryptozoidberg 2020-07-18 22:27:17 +02:00
parent 579e4826ed
commit a4b31c081c
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
5 changed files with 203 additions and 4 deletions

View file

@ -153,7 +153,7 @@ public:
template<typename Source>
std::streamsize read(Source& src, char* s, std::streamsize n)
{
if(m_buff.size() >= n)
if(m_buff.size() >= static_cast<size_t>(n))
{
return withdraw_to_read_buff(s, n);
}
@ -228,7 +228,8 @@ private:
std::streamsize withdraw_to_read_buff(char* s, std::streamsize n)
{
size_t copy_size = m_buff.size() > n ? n : m_buff.size();
size_t copy_size = m_buff.size() > static_cast<size_t>(n) ? static_cast<size_t>(n) : m_buff.size();
std::memcpy(s, m_buff.data(), copy_size);
m_buff.erase(0, copy_size);
return copy_size;

View file

@ -0,0 +1,186 @@
// Copyright (c) 2014-2015 The Boolberry developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <cstdint>
#include <vector>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/invert.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include "common/encryption_filter.h"
#include "crypto/crypto.h"
#include "currency_core/blockchain_storage_basic.h"
#include "currency_core/blockchain_storage_boost_serialization.h"
#include "common/boost_serialization_helper.h"
bool perform_crypt_stream_iteration(const std::list<currency::block_extended_info>& test_list, const crypto::chacha8_iv& iv)
{
std::list<currency::block_extended_info> verification_list;
boost::filesystem::ofstream store_data_file;
store_data_file.open("./test.bin", std::ios_base::binary | std::ios_base::out | std::ios::trunc);
encrypt_chacha_out_filter encrypt_filter("pass", iv);
boost::iostreams::filtering_ostream out;
out.push(encrypt_filter);
out.push(store_data_file);
//out << buff;
bool res = tools::portble_serialize_obj_to_stream(test_list, out);
out.flush();
store_data_file.close();
boost::filesystem::ifstream data_file;
data_file.open("./test.bin", std::ios_base::binary | std::ios_base::in);
encrypt_chacha_in_filter decrypt_filter("pass", iv);
boost::iostreams::filtering_istream in;
in.push(decrypt_filter);
in.push(data_file);
try {
bool res2 = tools::portable_unserialize_obj_from_stream(verification_list, in);
CHECK_AND_ASSERT_MES(res, false, "Failed to unserialize wallet");
size_t i = 0;
CHECK_AND_ASSERT_MES(test_list.size() == verification_list.size(), false, "restored list is wrong size");
return true;
}
catch (std::exception& err)
{
LOG_ERROR("Exception: " << err.what());
return false;
}
return true;
}
bool perform_just_substream_stream_iteration(const std::list<currency::block_extended_info>& test_list, const crypto::chacha8_iv& iv)
{
std::list<currency::block_extended_info> verification_list;
boost::filesystem::ofstream store_data_file;
store_data_file.open("./test3.bin", std::ios_base::binary | std::ios_base::out | std::ios::trunc);
//encrypt_chacha_out_filter encrypt_filter("pass", iv);
boost::iostreams::filtering_ostream out;
//out.push(encrypt_filter);
out.push(store_data_file);
//out << buff;
bool res = tools::portble_serialize_obj_to_stream(test_list, out);
out.flush();
store_data_file.close();
boost::filesystem::ifstream data_file;
data_file.open("./test3.bin", std::ios_base::binary | std::ios_base::in);
//encrypt_chacha_in_filter decrypt_filter("pass", iv);
boost::iostreams::filtering_istream in;
//in.push(decrypt_filter);
in.push(data_file);
try {
bool res2 = tools::portable_unserialize_obj_from_stream(verification_list, in);
CHECK_AND_ASSERT_MES(res, false, "Failed to unserialize wallet");
size_t i = 0;
CHECK_AND_ASSERT_MES(test_list.size() == verification_list.size(), false, "restored list is wrong size");
return true;
}
catch (std::exception& err)
{
LOG_ERROR("Exception: " << err.what());
return false;
}
return true;
}
bool perform_no_crypt_stream_iteration(const std::list<currency::block_extended_info>& test_list, const crypto::chacha8_iv& iv)
{
std::list<currency::block_extended_info> verification_list;
boost::filesystem::ofstream store_data_file;
store_data_file.open("./test2.bin", std::ios_base::binary | std::ios_base::out | std::ios::trunc);
// encrypt_chacha_out_filter encrypt_filter("pass", iv);
// boost::iostreams::filtering_ostream out;
// out.push(encrypt_filter);
// out.push(store_data_file);
//out << buff;
bool res = tools::portble_serialize_obj_to_stream(test_list, store_data_file);
store_data_file.flush();
store_data_file.close();
boost::filesystem::ifstream data_file;
data_file.open("./test2.bin", std::ios_base::binary | std::ios_base::in);
// encrypt_chacha_in_filter decrypt_filter("pass", iv);
// boost::iostreams::filtering_istream in;
// in.push(decrypt_filter);
// in.push(data_file);
try {
bool res2 = tools::portable_unserialize_obj_from_stream(verification_list, data_file);
CHECK_AND_ASSERT_MES(res, false, "Failed to unserialize wallet");
size_t i = 0;
CHECK_AND_ASSERT_MES(test_list.size() == verification_list.size(), false, "restored list is wrong size");
return true;
}
catch (std::exception& err)
{
LOG_ERROR("Exception: " << err.what());
return false;
}
}
bool do_chacha_stream_performance_test()
{
LOG_PRINT_L0("chacha_stream_test");
std::list<currency::block_extended_info> test_list;
for (size_t i = 0; i != 10000; i++) {
test_list.push_back(currency::block_extended_info());
test_list.back().height = i;
test_list.back().this_block_tx_fee_median = i;
}
crypto::chacha8_iv iv = crypto::rand<crypto::chacha8_iv>();
LOG_PRINT_L0("Running substream stream performance tests...");
TIME_MEASURE_START(substream_version);
for (size_t i = 0; i != 100; i++)
{
bool r = perform_just_substream_stream_iteration(test_list, iv);
CHECK_AND_ASSERT_MES(r, false, "Failed to perform_crypt_stream_iteration");
}
TIME_MEASURE_FINISH(substream_version);
LOG_PRINT_L0("OK.time: " << substream_version);
LOG_PRINT_L0("Running crypt stream performance tests...");
TIME_MEASURE_START(crypted_version);
for (size_t i = 0; i != 100; i++)
{
bool r = perform_crypt_stream_iteration(test_list, iv);
CHECK_AND_ASSERT_MES(r, false, "Failed to perform_crypt_stream_iteration");
}
TIME_MEASURE_FINISH(crypted_version);
LOG_PRINT_L0("OK.time: " << crypted_version);
LOG_PRINT_L0("Running non-crypt stream performance tests...");
TIME_MEASURE_START(non_crypted_version);
for (size_t i = 0; i != 100; i++)
{
bool r = perform_no_crypt_stream_iteration(test_list, iv);
CHECK_AND_ASSERT_MES(r, false, "Failed to perform_crypt_stream_iteration");
}
TIME_MEASURE_FINISH(non_crypted_version);
LOG_PRINT_L0("OK.time: " << non_crypted_version);
}

View file

@ -0,0 +1,7 @@
// Copyright (c) 2014-2015 The Boolberry developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
bool do_chacha_stream_performance_test();

View file

@ -17,6 +17,7 @@
#include "is_out_to_acc.h"
#include "core_market_performance_test.h"
#include "serialization_performance_test.h"
#include "chacha_stream_performance_test.h"
#include "keccak_test.h"
#include "blake2_test.h"
#include "print_struct_to_json.h"
@ -38,9 +39,10 @@ int main(int argc, char** argv)
set_process_affinity(1);
set_thread_high_priority();
do_chacha_stream_performance_test();
//test_blake2();
free_space_check();
//free_space_check();
//print_struct_to_json();

View file

@ -108,7 +108,7 @@ TEST(chacha_stream_test, diversity_test_on_different_stream_behaviour)
try {
size_t offset = 0;
while (offset < buff_size)
while (offset < buff_size+1)
{
std::streamsize count = std::rand() % 100;
// if (count + offset > buff_size)
@ -123,6 +123,9 @@ TEST(chacha_stream_test, diversity_test_on_different_stream_behaviour)
ASSERT_TRUE(count + offset > buff_size || readed_sz == count);
}
offset += readed_sz;
if (!in) {
break;
}
}
if (in) {
ASSERT_TRUE(false);