From 40b2843d15272b814d5f8950d35c26aa2608ca6a Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 27 Sep 2018 16:51:04 +0200 Subject: [PATCH] Added test case for parallel hashing --- include/fc/crypto/sha1.hpp | 5 ++++ tests/thread/parallel_tests.cpp | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/fc/crypto/sha1.hpp b/include/fc/crypto/sha1.hpp index a269797..01c6ca8 100644 --- a/include/fc/crypto/sha1.hpp +++ b/include/fc/crypto/sha1.hpp @@ -2,6 +2,8 @@ #include #include +#include + namespace fc{ class sha1 @@ -82,3 +84,6 @@ namespace std } }; } + +#include +FC_REFLECT_TYPENAME( fc::sha1 ) diff --git a/tests/thread/parallel_tests.cpp b/tests/thread/parallel_tests.cpp index 67ec6ae..b5b56f0 100644 --- a/tests/thread/parallel_tests.cpp +++ b/tests/thread/parallel_tests.cpp @@ -24,7 +24,13 @@ #include +#include +#include +#include +#include +#include #include +#include BOOST_AUTO_TEST_SUITE(parallel_tests) @@ -72,4 +78,48 @@ BOOST_AUTO_TEST_CASE( do_something_parallel ) } } +const std::string TEXT = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"$%&/()=?,.-#+´{[]}`*'_:;<>|"; + +template +class hash_test { + public: + std::string _hashname = fc::get_typename::name(); + + void run_single_threaded() { + const std::string first = Hash::hash(TEXT).str(); + fc::time_point start = fc::time_point::now(); + for( int i = 0; i < 1000; i++ ) + BOOST_CHECK_EQUAL( first, Hash::hash(TEXT).str() ); + fc::time_point end = fc::time_point::now(); + ilog( "${c} single-threaded ${h}'s in ${t}µs", ("c",1000)("h",_hashname)("t",end-start) ); + } + + void run_multi_threaded() { + const std::string first = Hash::hash(TEXT).str(); + std::vector> results; + results.reserve( 10000 ); + fc::time_point start = fc::time_point::now(); + for( int i = 0; i < 10000; i++ ) + results.push_back( fc::do_parallel( [] () { return Hash::hash(TEXT).str(); } ) ); + for( auto& result: results ) + BOOST_CHECK_EQUAL( first, result.wait() ); + fc::time_point end = fc::time_point::now(); + ilog( "${c} multi-threaded ${h}'s in ${t}µs", ("c",10000)("h",_hashname)("t",end-start) ); + } + + void run() { + run_single_threaded(); + run_multi_threaded(); + } +}; + +BOOST_AUTO_TEST_CASE( hash_parallel ) +{ + hash_test().run(); + hash_test().run(); + hash_test().run(); + hash_test().run(); + hash_test().run(); +} + BOOST_AUTO_TEST_SUITE_END()