aboutsummaryrefslogtreecommitdiffstats
path: root/src/benchmark/benchmark.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/benchmark/benchmark.cpp')
-rw-r--r--src/benchmark/benchmark.cpp77
1 files changed, 46 insertions, 31 deletions
diff --git a/src/benchmark/benchmark.cpp b/src/benchmark/benchmark.cpp
index dbccd45c1..a9b84f252 100644
--- a/src/benchmark/benchmark.cpp
+++ b/src/benchmark/benchmark.cpp
@@ -1,6 +1,6 @@
/*
* Runtime benchmarking
-* (C) 2008 Jack Lloyd
+* (C) 2008-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -11,33 +11,40 @@
#include <botan/stream_cipher.h>
#include <botan/hash.h>
#include <botan/mac.h>
-#include <botan/time.h>
#include <memory>
+#include <vector>
+#include <chrono>
namespace Botan {
namespace {
+typedef std::chrono::high_resolution_clock benchmark_clock;
+
/**
* Benchmark Buffered_Computation (hash or MAC)
*/
std::pair<u64bit, u64bit> bench_buf_comp(Buffered_Computation* buf_comp,
- u64bit nanoseconds_max,
+ std::chrono::nanoseconds max_time,
const byte buf[], size_t buf_len)
{
u64bit reps = 0;
- u64bit nanoseconds_used = 0;
- while(nanoseconds_used < nanoseconds_max)
+ std::chrono::nanoseconds time_used(0);
+
+ while(time_used < max_time)
{
- const u64bit start = get_nanoseconds_clock();
+ auto start = benchmark_clock::now();
buf_comp->update(buf, buf_len);
- nanoseconds_used += get_nanoseconds_clock() - start;
+ time_used += std::chrono::duration_cast<std::chrono::nanoseconds>(benchmark_clock::now() - start);
++reps;
}
- return std::make_pair(reps * buf_len, nanoseconds_used);
+ u64bit ns_taken =
+ std::chrono::duration_cast<std::chrono::nanoseconds>(time_used).count();
+
+ return std::make_pair(reps * buf_len, ns_taken);
}
/**
@@ -45,27 +52,32 @@ std::pair<u64bit, u64bit> bench_buf_comp(Buffered_Computation* buf_comp,
*/
std::pair<u64bit, u64bit>
bench_block_cipher(BlockCipher* block_cipher,
- u64bit nanoseconds_max,
+ std::chrono::nanoseconds max_time,
byte buf[], size_t buf_len)
{
const size_t in_blocks = buf_len / block_cipher->block_size();
u64bit reps = 0;
- u64bit nanoseconds_used = 0;
+
+ std::chrono::nanoseconds time_used(0);
block_cipher->set_key(buf, block_cipher->maximum_keylength());
- while(nanoseconds_used < nanoseconds_max)
+ while(time_used < max_time)
{
- const u64bit start = get_nanoseconds_clock();
+ auto start = benchmark_clock::now();
block_cipher->encrypt_n(buf, buf, in_blocks);
- nanoseconds_used += get_nanoseconds_clock() - start;
+ time_used += std::chrono::duration_cast<std::chrono::nanoseconds>(benchmark_clock::now() - start);
+ //time_used += benchmark_clock::now() - start;
++reps;
}
+ u64bit ns_taken =
+ std::chrono::duration_cast<std::chrono::nanoseconds>(time_used).count();
+
return std::make_pair(reps * in_blocks * block_cipher->block_size(),
- nanoseconds_used);
+ ns_taken);
}
/**
@@ -73,24 +85,28 @@ bench_block_cipher(BlockCipher* block_cipher,
*/
std::pair<u64bit, u64bit>
bench_stream_cipher(StreamCipher* stream_cipher,
- u64bit nanoseconds_max,
+ std::chrono::nanoseconds max_time,
byte buf[], size_t buf_len)
{
u64bit reps = 0;
- u64bit nanoseconds_used = 0;
stream_cipher->set_key(buf, stream_cipher->maximum_keylength());
- while(nanoseconds_used < nanoseconds_max)
+ std::chrono::nanoseconds time_used(0);
+
+ while(time_used < max_time)
{
- const u64bit start = get_nanoseconds_clock();
+ auto start = benchmark_clock::now();
stream_cipher->cipher1(buf, buf_len);
- nanoseconds_used += get_nanoseconds_clock() - start;
+ time_used += benchmark_clock::now() - start;
++reps;
}
- return std::make_pair(reps * buf_len, nanoseconds_used);
+ u64bit ns_taken =
+ std::chrono::duration_cast<std::chrono::nanoseconds>(time_used).count();
+
+ return std::make_pair(reps * buf_len, ns_taken);
}
/**
@@ -98,10 +114,10 @@ bench_stream_cipher(StreamCipher* stream_cipher,
*/
std::pair<u64bit, u64bit>
bench_hash(HashFunction* hash,
- u64bit nanoseconds_max,
+ std::chrono::nanoseconds max_time,
const byte buf[], size_t buf_len)
{
- return bench_buf_comp(hash, nanoseconds_max, buf, buf_len);
+ return bench_buf_comp(hash, max_time, buf, buf_len);
}
/**
@@ -109,11 +125,11 @@ bench_hash(HashFunction* hash,
*/
std::pair<u64bit, u64bit>
bench_mac(MessageAuthenticationCode* mac,
- u64bit nanoseconds_max,
+ std::chrono::nanoseconds max_time,
const byte buf[], size_t buf_len)
{
mac->set_key(buf, mac->maximum_keylength());
- return bench_buf_comp(mac, nanoseconds_max, buf, buf_len);
+ return bench_buf_comp(mac, max_time, buf, buf_len);
}
}
@@ -122,7 +138,7 @@ std::map<std::string, double>
algorithm_benchmark(const std::string& name,
Algorithm_Factory& af,
RandomNumberGenerator& rng,
- u32bit milliseconds,
+ std::chrono::milliseconds milliseconds,
size_t buf_size)
{
std::vector<std::string> providers = af.providers_of(name);
@@ -131,8 +147,7 @@ algorithm_benchmark(const std::string& name,
if(providers.empty()) // no providers, nothing to do
return all_results;
- const u64bit ns_per_provider =
- (static_cast<u64bit>(milliseconds) * 1000 * 1000) / providers.size();
+ std::chrono::nanoseconds ns_per_provider = milliseconds / providers.size();
std::vector<byte> buf(buf_size * 1024);
rng.randomize(&buf[0], buf.size());
@@ -146,7 +161,7 @@ algorithm_benchmark(const std::string& name,
if(const BlockCipher* proto =
af.prototype_block_cipher(name, provider))
{
- std::auto_ptr<BlockCipher> block_cipher(proto->clone());
+ std::unique_ptr<BlockCipher> block_cipher(proto->clone());
results = bench_block_cipher(block_cipher.get(),
ns_per_provider,
&buf[0], buf.size());
@@ -154,7 +169,7 @@ algorithm_benchmark(const std::string& name,
else if(const StreamCipher* proto =
af.prototype_stream_cipher(name, provider))
{
- std::auto_ptr<StreamCipher> stream_cipher(proto->clone());
+ std::unique_ptr<StreamCipher> stream_cipher(proto->clone());
results = bench_stream_cipher(stream_cipher.get(),
ns_per_provider,
&buf[0], buf.size());
@@ -162,14 +177,14 @@ algorithm_benchmark(const std::string& name,
else if(const HashFunction* proto =
af.prototype_hash_function(name, provider))
{
- std::auto_ptr<HashFunction> hash(proto->clone());
+ std::unique_ptr<HashFunction> hash(proto->clone());
results = bench_hash(hash.get(), ns_per_provider,
&buf[0], buf.size());
}
else if(const MessageAuthenticationCode* proto =
af.prototype_mac(name, provider))
{
- std::auto_ptr<MessageAuthenticationCode> mac(proto->clone());
+ std::unique_ptr<MessageAuthenticationCode> mac(proto->clone());
results = bench_mac(mac.get(), ns_per_provider,
&buf[0], buf.size());
}