diff --git a/src/common/encryption_filter.h b/src/common/encryption_filter.h index b8eba750..838fcc95 100644 --- a/src/common/encryption_filter.h +++ b/src/common/encryption_filter.h @@ -153,7 +153,7 @@ public: template std::streamsize read(Source& src, char* s, std::streamsize n) { - if(m_buff.size() >= n) + if(m_buff.size() >= static_cast(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(n) ? static_cast(n) : m_buff.size(); std::memcpy(s, m_buff.data(), copy_size); m_buff.erase(0, copy_size); return copy_size; diff --git a/tests/performance_tests/chacha_stream_performance_test.cpp b/tests/performance_tests/chacha_stream_performance_test.cpp new file mode 100644 index 00000000..71e14b01 --- /dev/null +++ b/tests/performance_tests/chacha_stream_performance_test.cpp @@ -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 +#include +#include +#include +#include + +#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& test_list, const crypto::chacha8_iv& iv) +{ + std::list 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& test_list, const crypto::chacha8_iv& iv) +{ + std::list 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& test_list, const crypto::chacha8_iv& iv) +{ + std::list 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 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(); + 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); + + + +} + + + + diff --git a/tests/performance_tests/chacha_stream_performance_test.h b/tests/performance_tests/chacha_stream_performance_test.h new file mode 100644 index 00000000..791d0bd5 --- /dev/null +++ b/tests/performance_tests/chacha_stream_performance_test.h @@ -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(); diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index a490ad7b..d4043276 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -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(); diff --git a/tests/unit_tests/chacha_stream_test.cpp b/tests/unit_tests/chacha_stream_test.cpp index e146e413..129baf6b 100644 --- a/tests/unit_tests/chacha_stream_test.cpp +++ b/tests/unit_tests/chacha_stream_test.cpp @@ -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);