-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_bench.cpp
More file actions
113 lines (89 loc) · 2.07 KB
/
Copy pathcrypto_bench.cpp
File metadata and controls
113 lines (89 loc) · 2.07 KB
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
#include <ctime>
#include <cstdio>
#include "crypto_bench.h"
#include "gcrypt.h"
#include "botan.h"
#include "cryptopp.h"
#include "af_alg.h"
#define EPS 0.0001
#define N_ROUNDS 50000
static size_t malloc_total = 0;
extern "C" {
void *__real_malloc(size_t size);
void *__wrap_malloc(size_t size)
{
malloc_total += size;
return __real_malloc(size);
}
}
void *operator new(std::size_t size)
{
return malloc(size);
}
void* operator new[](std::size_t size) {
return malloc(size);
}
void operator delete(void* ptr) {
free(ptr);
}
void operator delete[](void* ptr) {
free(ptr);
}
void run_benchmark(CryptoBenchTest *test, int rounds)
{
clock_t start, stop;
float time, speed, mem;
const char *suffix[] = {"B", "KB", "MB", "GB"};
int sidx = 0;
int midx = 0;
malloc_total = 0;
start = clock();
test->encrypt(rounds);
stop = clock();
time = (float)(stop - start) / CLOCKS_PER_SEC;
time = time < EPS ? 1 : time;
speed = test->data_size() * rounds / time;
while (speed >= 1024) {
sidx++;
speed /= 1024;
}
mem = malloc_total;
while (mem >= 1024) {
midx++;
mem /= 1024;
}
printf("%20s\tEncrypt\t%8.2f %s/s %8.2f %s\n", test->name, speed, suffix[sidx], mem, suffix[midx]);
malloc_total = 0;
start = clock();
test->decrypt(rounds);
stop = clock();
time = (float)(stop - start) / CLOCKS_PER_SEC;
time = time < EPS ? 1 : time;
speed = test->data_size() * rounds / time;
sidx = midx = 0;
while (speed >= 1024) {
sidx++;
speed /= 1024;
}
mem = malloc_total;
while (mem >= 1024) {
midx++;
mem /= 1024;
}
printf("%20s\tDecrypt\t%8.2f %s/s %8.2f %s\n", test->name, speed, suffix[sidx], mem, suffix[midx]);
}
int main(int argc, char *argv[])
{
GCryptTest *gctest = new GCryptTest();
BotanTest *bt = new BotanTest();
CryptoppTest *cppt = new CryptoppTest();
AF_ALGTest *aftest = new AF_ALGTest();
printf("%20s\tOperation\tSpeed\tMemory\n", "Implementation");
for (int i = 0; i < 60; i++)
printf("-");
printf("\n");
run_benchmark(bt, N_ROUNDS);
run_benchmark(gctest, N_ROUNDS);
run_benchmark(cppt, N_ROUNDS);
run_benchmark(aftest, N_ROUNDS);
}