aboutsummaryrefslogtreecommitdiffstats
path: root/src/benchmark/benchmark.cpp
blob: 7e5eae0489aed87d3a68b93367d2cfacbd61173b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Runtime benchmarking
* (C) 2008 Jack Lloyd
*/

#include <botan/benchmark.h>
#include <botan/buf_comp.h>
#include <botan/block_cipher.h>
#include <botan/stream_cipher.h>
#include <botan/hash.h>
#include <botan/mac.h>
#include <botan/util.h>
#include <memory>

namespace Botan {

namespace {

/**
* Benchmark BufferedComputation (hash or MAC)
*/
std::pair<u32bit, u64bit> bench_buf_es(BufferedComputation* buf_comp,
                                       Timer& timer,
                                       u64bit nanoseconds_max,
                                       const byte buf[], u32bit buf_len)
   {
   const u64bit start = timer.clock();
   u64bit nanoseconds_used = 0;
   u32bit reps = 0;

   while(nanoseconds_used < nanoseconds_max)
      {
      buf_comp->update(buf, buf_len);
      ++reps;
      nanoseconds_used = timer.clock() - start;
      }

   return std::make_pair(reps * buf_len, nanoseconds_used);
   }

/**
* Benchmark block cipher
*/
std::pair<u32bit, 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;
   u32bit reps = 0;

   buf_len = round_down(buf_len - 1, block_cipher->BLOCK_SIZE);

   while(nanoseconds_used < nanoseconds_max)
      {
      for(u32bit j = 0; j <= buf_len; j += block_cipher->BLOCK_SIZE)
         block_cipher->encrypt(&buf[j]);

      ++reps;
      nanoseconds_used = timer.clock() - start;
      }

   return std::make_pair(reps * buf_len, nanoseconds_used);
   }

/**
* Benchmark stream
*/
std::pair<u32bit, 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;
   u32bit reps = 0;

   while(nanoseconds_used < nanoseconds_max)
      {
      stream_cipher->encrypt(buf, buf_len);
      ++reps;
      nanoseconds_used = timer.clock() - start;
      }

   return std::make_pair(reps * buf_len, nanoseconds_used);
   }

/**
* Benchmark hash
*/
std::pair<u32bit, u64bit>
bench_hash(HashFunction* hash, Timer& timer,
           u64bit nanoseconds_max,
           const byte buf[], u32bit buf_len)
   {
   return bench_buf_es(hash, timer, nanoseconds_max, buf, buf_len);
   }

/**
* Benchmark MAC
*/
std::pair<u32bit, 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_es(mac, timer, nanoseconds_max, buf, buf_len);
   }

}

std::map<std::string, double>
algorithm_benchmark(const std::string& name,
                    u32bit milliseconds,
                    Timer& timer,
                    RandomNumberGenerator& rng,
                    Algorithm_Factory& af)
   {
   std::vector<std::string> providers = af.providers_of(name);
   std::map<std::string, double> all_results;

   if(providers.empty()) // no providers, nothing to do
      return all_results;

   const u64bit ns_per_provider =
      ((u64bit)milliseconds * 1000 * 1000) / providers.size();

   std::vector<byte> buf(16 * 1024);
   rng.randomize(&buf[0], buf.size());

   for(u32bit i = 0; i != providers.size(); ++i)
      {
      const std::string provider = providers[i];

      std::pair<u32bit, u64bit> results = std::make_pair(0, 0);

      if(const BlockCipher* proto =
            af.prototype_block_cipher(name, provider))
         {
         std::auto_ptr<BlockCipher> block_cipher(proto->clone());
         results = bench_block_cipher(block_cipher.get(), timer,
                                      ns_per_provider,
                                      &buf[0], buf.size());
         }
      else if(const StreamCipher* proto =
                 af.prototype_stream_cipher(name, provider))
         {
         std::auto_ptr<StreamCipher> stream_cipher(proto->clone());
         results = bench_stream_cipher(stream_cipher.get(), timer,
                                       ns_per_provider,
                                       &buf[0], buf.size());
         }
      else if(const HashFunction* proto =
                 af.prototype_hash_function(name, provider))
         {
         std::auto_ptr<HashFunction> hash(proto->clone());
         results = bench_hash(hash.get(), timer, 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,
                             &buf[0], buf.size());
         }

      if(results.first && results.second)
         {
         /* 953.67 == 1000 * 1000 * 1000 / 1024 / 1024 - the conversion
            factor from bytes per nanosecond to mebibytes per second.
         */
         double speed = (953.67 * results.first) / results.second;
         all_results[provider] = speed;
         }
      }

   return all_results;
   }

}