Added test case for parallel hashing

This commit is contained in:
Peter Conrad 2018-09-27 16:51:04 +02:00
parent 3131c1df43
commit 40b2843d15
2 changed files with 55 additions and 0 deletions

View file

@ -2,6 +2,8 @@
#include <fc/fwd.hpp> #include <fc/fwd.hpp>
#include <fc/string.hpp> #include <fc/string.hpp>
#include <functional>
namespace fc{ namespace fc{
class sha1 class sha1
@ -82,3 +84,6 @@ namespace std
} }
}; };
} }
#include <fc/reflect/reflect.hpp>
FC_REFLECT_TYPENAME( fc::sha1 )

View file

@ -24,7 +24,13 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <fc/crypto/ripemd160.hpp>
#include <fc/crypto/sha1.hpp>
#include <fc/crypto/sha224.hpp>
#include <fc/crypto/sha256.hpp>
#include <fc/crypto/sha512.hpp>
#include <fc/thread/parallel.hpp> #include <fc/thread/parallel.hpp>
#include <fc/time.hpp>
BOOST_AUTO_TEST_SUITE(parallel_tests) BOOST_AUTO_TEST_SUITE(parallel_tests)
@ -72,4 +78,48 @@ BOOST_AUTO_TEST_CASE( do_something_parallel )
} }
} }
const std::string TEXT = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"$%&/()=?,.-#+´{[]}`*'_:;<>|";
template<typename Hash>
class hash_test {
public:
std::string _hashname = fc::get_typename<Hash>::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<fc::future<std::string>> 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<fc::ripemd160>().run();
hash_test<fc::sha1>().run();
hash_test<fc::sha224>().run();
hash_test<fc::sha256>().run();
hash_test<fc::sha512>().run();
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()