diff --git a/CMakeLists.txt b/CMakeLists.txt index f7fc0dc..ecfb51b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,11 @@ add_library (cruft-crypto STATIC ${sources}) target_link_libraries (cruft-crypto INTERFACE cruft) +##----------------------------------------------------------------------------- +add_executable (crypto_bench tools/bench.cpp) +target_link_libraries (crypto_bench cruft-crypto) + + ##----------------------------------------------------------------------------- add_executable (crypto_hash tools/hash.cpp) target_link_libraries (crypto_hash cruft-crypto) diff --git a/tools/bench.cpp b/tools/bench.cpp new file mode 100644 index 0000000..729fd94 --- /dev/null +++ b/tools/bench.cpp @@ -0,0 +1,76 @@ +#include "cruft/crypto/hash/blake.hpp" +#include "cruft/crypto/hash/blake2.hpp" +#include "cruft/crypto/hash/md2.hpp" +#include "cruft/crypto/hash/md4.hpp" +#include "cruft/crypto/hash/md5.hpp" +#include "cruft/crypto/hash/md6.hpp" +#include "cruft/crypto/hash/ripemd.hpp" +#include "cruft/crypto/hash/sha1.hpp" +#include "cruft/crypto/hash/sha2.hpp" + +#include +#include +#include +#include + +#include +#include +#include +#include + + +/////////////////////////////////////////////////////////////////////////////// +template +float +time_hash (cruft::view data) +{ + auto const start = std::chrono::high_resolution_clock::now (); + HashT obj; + auto const res = obj (data); + cruft::debug::escape (res); + auto const finish = std::chrono::high_resolution_clock::now (); + + std::chrono::duration const duration = finish - start; + return duration.count (); +} + + +/////////////////////////////////////////////////////////////////////////////// +int +main (void) +{ + // 1 GiB of random data; + static constexpr std::size_t SOURCE_SIZE = 1024u * 1024u * 10; + std::vector source (SOURCE_SIZE); + + std::cerr << "generating data\n"; +#if 1 + std::fill (std::begin (source), std::end (source), 0u); +#else + std::generate_n ( + std::begin (source), + SOURCE_SIZE, + [] () { return cruft::random::uniform (); } + ); +#endif + + f32 const gib_multiplier = f32 (SOURCE_SIZE) / (1024 * 1024 * 1024); + + std::cerr << "starting benchmark\n"; + #define BENCH_HASH(KLASS) \ + do { \ + auto const elapsed = time_hash (cruft::view (source)); \ + std::cout << #KLASS ": " << gib_multiplier / elapsed << " GiB/s\n"; \ + } while (0); + + MAP0 (BENCH_HASH, + MD2, + MD4, + MD5, + RIPEMD, + SHA1, + SHA256, + blake<512>, + blake2 + ) +} \ No newline at end of file