aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-09-16 15:56:55 +0000
committerlloyd <[email protected]>2009-09-16 15:56:55 +0000
commit69e8cc40848b95a644fb952c73788e825c1683ba (patch)
treedaf20f3b26d0fee6c5e2f26854b07ff34eadc86d
parentaaf319491523ae103da2fd459cd94f7018c1cbce (diff)
Use <chrono> in the runtime benchmarking code instead of the local
timers.
-rw-r--r--doc/examples/bench.cpp3
-rw-r--r--src/benchmark/benchmark.cpp76
-rw-r--r--src/benchmark/benchmark.h26
-rw-r--r--src/benchmark/info.txt1
4 files changed, 48 insertions, 58 deletions
diff --git a/doc/examples/bench.cpp b/doc/examples/bench.cpp
index 0aedc699d..7d53e0557 100644
--- a/doc/examples/bench.cpp
+++ b/doc/examples/bench.cpp
@@ -66,7 +66,6 @@ int main()
u32bit milliseconds = 1000;
AutoSeeded_RNG rng;
- Default_Benchmark_Timer timer;
Algorithm_Factory& af = global_state().algorithm_factory();
@@ -75,7 +74,7 @@ int main()
std::string algo = algos[i];
std::map<std::string, double> speeds =
- algorithm_benchmark(algos[i], milliseconds, timer, rng, af);
+ algorithm_benchmark(algos[i], milliseconds, rng, af);
std::cout << algo << ":";
diff --git a/src/benchmark/benchmark.cpp b/src/benchmark/benchmark.cpp
index 3bbc1f883..ff7519c09 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
*/
@@ -13,31 +13,40 @@
#include <botan/mac.h>
#include <botan/util.h>
#include <memory>
+#include <vector>
+#include <chrono>
namespace Botan {
namespace {
+typedef std::chrono::high_resolution_clock benchmark_clock;
+
/**
* Benchmark BufferedComputation (hash or MAC)
*/
std::pair<u64bit, u64bit> bench_buf_comp(BufferedComputation* buf_comp,
- Timer& timer,
u64bit nanoseconds_max,
const byte buf[], u32bit buf_len)
{
- const u64bit start = timer.clock();
- u64bit nanoseconds_used = 0;
u64bit reps = 0;
- while(nanoseconds_used < nanoseconds_max)
+ std::chrono::nanoseconds max_time(nanoseconds_max);
+ std::chrono::nanoseconds time_used(0);
+
+ auto start = benchmark_clock::now();
+
+ while(time_used < max_time)
{
buf_comp->update(buf, buf_len);
++reps;
- nanoseconds_used = timer.clock() - start;
+ time_used = benchmark_clock::now() - start;
}
- 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,26 +54,30 @@ std::pair<u64bit, u64bit> bench_buf_comp(BufferedComputation* buf_comp,
*/
std::pair<u64bit, u64bit>
bench_block_cipher(BlockCipher* block_cipher,
- Timer& timer,
u64bit nanoseconds_max,
byte buf[], u32bit buf_len)
{
- const u64bit start = timer.clock();
- u64bit nanoseconds_used = 0;
+ const u32bit in_blocks = buf_len / block_cipher->BLOCK_SIZE;
+
u64bit reps = 0;
- const u32bit in_blocks = buf_len / block_cipher->BLOCK_SIZE;
+ std::chrono::nanoseconds max_time(nanoseconds_max);
+ std::chrono::nanoseconds time_used(0);
- while(nanoseconds_used < nanoseconds_max)
+ auto start = benchmark_clock::now();
+
+ while(time_used < max_time)
{
block_cipher->encrypt_n(buf, buf, in_blocks);
-
++reps;
- nanoseconds_used = timer.clock() - start;
+ time_used = benchmark_clock::now() - start;
}
+ 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);
}
/**
@@ -72,33 +85,38 @@ bench_block_cipher(BlockCipher* block_cipher,
*/
std::pair<u64bit, u64bit>
bench_stream_cipher(StreamCipher* stream_cipher,
- Timer& timer,
u64bit nanoseconds_max,
byte buf[], u32bit buf_len)
{
- const u64bit start = timer.clock();
- u64bit nanoseconds_used = 0;
u64bit reps = 0;
- while(nanoseconds_used < nanoseconds_max)
+ std::chrono::nanoseconds max_time(nanoseconds_max);
+ std::chrono::nanoseconds time_used(0);
+
+ auto start = benchmark_clock::now();
+
+ while(time_used < max_time)
{
stream_cipher->encrypt(buf, buf_len);
++reps;
- nanoseconds_used = timer.clock() - start;
+ time_used = benchmark_clock::now() - start;
}
- 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);
}
/**
* Benchmark hash
*/
std::pair<u64bit, u64bit>
-bench_hash(HashFunction* hash, Timer& timer,
+bench_hash(HashFunction* hash,
u64bit nanoseconds_max,
const byte buf[], u32bit buf_len)
{
- return bench_buf_comp(hash, timer, nanoseconds_max, buf, buf_len);
+ return bench_buf_comp(hash, nanoseconds_max, buf, buf_len);
}
/**
@@ -106,12 +124,11 @@ bench_hash(HashFunction* hash, Timer& timer,
*/
std::pair<u64bit, u64bit>
bench_mac(MessageAuthenticationCode* mac,
- Timer& timer,
u64bit nanoseconds_max,
const byte buf[], u32bit buf_len)
{
mac->set_key(buf, mac->MAXIMUM_KEYLENGTH);
- return bench_buf_comp(mac, timer, nanoseconds_max, buf, buf_len);
+ return bench_buf_comp(mac, nanoseconds_max, buf, buf_len);
}
}
@@ -119,7 +136,6 @@ bench_mac(MessageAuthenticationCode* mac,
std::map<std::string, double>
algorithm_benchmark(const std::string& name,
u32bit milliseconds,
- Timer& timer,
RandomNumberGenerator& rng,
Algorithm_Factory& af)
{
@@ -145,7 +161,7 @@ algorithm_benchmark(const std::string& name,
af.prototype_block_cipher(name, provider))
{
std::auto_ptr<BlockCipher> block_cipher(proto->clone());
- results = bench_block_cipher(block_cipher.get(), timer,
+ results = bench_block_cipher(block_cipher.get(),
ns_per_provider,
&buf[0], buf.size());
}
@@ -153,7 +169,7 @@ algorithm_benchmark(const std::string& name,
af.prototype_stream_cipher(name, provider))
{
std::auto_ptr<StreamCipher> stream_cipher(proto->clone());
- results = bench_stream_cipher(stream_cipher.get(), timer,
+ results = bench_stream_cipher(stream_cipher.get(),
ns_per_provider,
&buf[0], buf.size());
}
@@ -161,14 +177,14 @@ algorithm_benchmark(const std::string& name,
af.prototype_hash_function(name, provider))
{
std::auto_ptr<HashFunction> hash(proto->clone());
- results = bench_hash(hash.get(), timer, ns_per_provider,
+ 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());
- results = bench_mac(mac.get(), timer, ns_per_provider,
+ results = bench_mac(mac.get(), ns_per_provider,
&buf[0], buf.size());
}
diff --git a/src/benchmark/benchmark.h b/src/benchmark/benchmark.h
index 272cfdfa2..a9c3fc01e 100644
--- a/src/benchmark/benchmark.h
+++ b/src/benchmark/benchmark.h
@@ -1,6 +1,6 @@
/**
* Runtime benchmarking
-* (C) 2008 Jack Lloyd
+* (C) 2008-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
@@ -9,35 +9,12 @@
#define BOTAN_RUNTIME_BENCHMARK_H__
#include <botan/algo_factory.h>
-#include <botan/timer.h>
#include <botan/rng.h>
#include <map>
#include <string>
-/**
-* Choose some sort of default timer implementation to use, since some
-* (like hardware tick counters and current Win32 timer) are not
-* reliable for benchmarking.
-*/
-#if defined(BOTAN_HAS_TIMER_POSIX)
- #include <botan/tm_posix.h>
-#elif defined(BOTAN_HAS_TIMER_UNIX)
- #include <botan/tm_unix.h>
-#endif
-
namespace Botan {
-#if defined(BOTAN_HAS_TIMER_POSIX)
- typedef POSIX_Timer Default_Benchmark_Timer;
-#elif defined(BOTAN_HAS_TIMER_UNIX)
- typedef Unix_Timer Default_Benchmark_Timer;
-#else
- /* I have not had good success using clock(), the results seem
- * pretty bogus, but as a last resort it works.
- */
- typedef ANSI_Clock_Timer Default_Benchmark_Timer;
-#endif
-
/**
* Algorithm benchmark
* @param name the name of the algorithm to test (cipher, hash, or MAC)
@@ -50,7 +27,6 @@ namespace Botan {
std::map<std::string, double>
algorithm_benchmark(const std::string& name,
u32bit milliseconds,
- Timer& timer,
RandomNumberGenerator& rng,
Algorithm_Factory& af);
diff --git a/src/benchmark/info.txt b/src/benchmark/info.txt
index 0fbcdb2de..03d5aac50 100644
--- a/src/benchmark/info.txt
+++ b/src/benchmark/info.txt
@@ -17,5 +17,4 @@ hash
mac
rng
stream
-timer
</requires>