-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchainhash_bench.cpp
214 lines (203 loc) · 9.68 KB
/
chainhash_bench.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <gtest/gtest.h>
#include <atomic>
#include <fstream>
#include <thread>
#include "bench_utils.h"
#include "chainhash_modes.h"
#include "hash_modes.h"
#include "timer.h"
const constexpr u_int8_t iters_indices = 3;
const constexpr u_int64_t iterations[] = {10000, 100000, 1000000};
// const constexpr u_int8_t RUN_ITERS[] = {3,3,3};
const constexpr u_int8_t RUN_ITERS[] = {5, 5, 5};
// const constexpr u_int8_t RUN_ITERS[] = {10,5,3};
// flag that indicates whether the measurement thread should terminate
std::atomic<bool> _terminateMeasurementThread(false);
std::atomic<u_int64_t> _memory_max(0);
std::atomic<u_int64_t> _memory_min(0);
std::atomic<u_int64_t> _memory_avg(0);
std::atomic<u_int64_t> _memory_base(0);
void MemoryThread() {
// this thread will measure the memory usage
_memory_max = 0;
_memory_min = 0;
_memory_base = getPhysicalMem();
_terminateMeasurementThread = false;
u_int64_t memory = 0;
u_int64_t memory_max = 0;
u_int64_t memory_min = 0;
u_int64_t memory_avg = 0;
u_int64_t memory_sum = 0;
u_int64_t memory_count = 0;
while (!_terminateMeasurementThread) {
memory = getPhysicalMem();
memory_sum += memory;
memory_count++;
if (memory_max < memory || memory_max == 0) {
memory_max = memory;
}
if (memory_min > memory || memory_min == 0) {
memory_min = memory;
}
memory_avg = memory_sum / memory_count;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
_memory_max = memory_max;
_memory_min = memory_min;
_memory_avg = memory_avg;
}
void filing(std::string chainhash, std::string hash, u_int64_t iters, u_int64_t avg, u_int64_t slowest, u_int64_t memory_max, u_int64_t memory_min, u_int64_t memory_avg, u_int64_t memory_base) {
std::ofstream file;
file.open("chainhash_bench.csv", std::ios::app);
file << chainhash << "," << hash << "," << iters << "," << avg << "," << slowest << "," << memory_max << "," << memory_min << "," << memory_avg << "," << memory_base << "\n";
file.close();
}
TEST(ChainHash, normal) {
// setup the chainhashes
std::string data_str = "test";
u_int8_t ichash = 1;
std::string chainhash_info = ChainHashModes::getShortInfo(CHModes(ichash));
std::shared_ptr<ChainHashData> chd1 = std::make_shared<ChainHashData>(Format{CHModes(ichash)});
chd1->generateRandomData();
std::vector<ChainHash> chainhashes = {ChainHash{CHModes(ichash), iterations[0], chd1}, ChainHash{CHModes(ichash), iterations[1], chd1}, ChainHash{CHModes(ichash), iterations[2], chd1}};
// the benchmark starts here
// run the chainhashes
for (u_int8_t ihash = 1; ihash <= MAX_HASHMODE_NUMBER; ihash++) {
std::shared_ptr<Hash> hash = HashModes::getHash(HModes(ihash));
std::string hash_info = HashModes::getInfo(HModes(ihash), true);
for (u_int64_t iters_ind = 0; iters_ind < iters_indices; iters_ind++) {
u_int8_t run_iters = RUN_ITERS[iters_ind];
Timer timer;
std::thread memoryThread(MemoryThread);
timer.start();
for (int i = 0; i < run_iters; i++) {
ChainHashModes::performChainHash(chainhashes[iters_ind], hash, data_str);
if (i != run_iters - 1) timer.recordTime();
}
timer.stop();
_terminateMeasurementThread = true;
memoryThread.join();
filing(chainhash_info, hash_info, chainhashes[iters_ind].getIters(), timer.getAverageTime(), timer.getSlowest(), _memory_max, _memory_min, _memory_avg, _memory_base);
}
}
}
TEST(ChainHash, constant) {
// setup the chainhashes
std::string data_str = "test";
u_int8_t ichash = 2;
std::string chainhash_info = ChainHashModes::getShortInfo(CHModes(ichash));
std::shared_ptr<ChainHashData> chd1 = std::make_shared<ChainHashData>(Format{CHModes(ichash)});
chd1->generateRandomData();
std::vector<ChainHash> chainhashes = {ChainHash{CHModes(ichash), iterations[0], chd1}, ChainHash{CHModes(ichash), iterations[1], chd1}, ChainHash{CHModes(ichash), iterations[2], chd1}};
// the benchmark starts here
// run the chainhashes
for (u_int8_t ihash = 1; ihash <= MAX_HASHMODE_NUMBER; ihash++) {
std::shared_ptr<Hash> hash = HashModes::getHash(HModes(ihash));
std::string hash_info = HashModes::getInfo(HModes(ihash), true);
for (u_int64_t iters_ind = 0; iters_ind < iters_indices; iters_ind++) {
ChainHash ch = chainhashes[iters_ind];
u_int8_t run_iters = RUN_ITERS[iters_ind];
Timer timer;
std::thread memoryThread(MemoryThread);
timer.start();
for (int i = 0; i < run_iters; i++) {
ChainHashModes::performChainHash(ch, hash, data_str);
if (i != run_iters - 1) timer.recordTime();
}
timer.stop();
_terminateMeasurementThread = true;
memoryThread.join();
filing(chainhash_info, hash_info, ch.getIters(), timer.getAverageTime(), timer.getSlowest(), _memory_max, _memory_min, _memory_avg, _memory_base);
}
}
}
TEST(ChainHash, count) {
// setup the chainhashes
std::string data_str = "test";
u_int8_t ichash = 3;
std::string chainhash_info = ChainHashModes::getShortInfo(CHModes(ichash));
std::shared_ptr<ChainHashData> chd1 = std::make_shared<ChainHashData>(Format{CHModes(ichash)});
chd1->generateRandomData();
std::vector<ChainHash> chainhashes = {ChainHash{CHModes(ichash), iterations[0], chd1}, ChainHash{CHModes(ichash), iterations[1], chd1}, ChainHash{CHModes(ichash), iterations[2], chd1}};
// the benchmark starts here
// run the chainhashes
for (u_int8_t ihash = 1; ihash <= MAX_HASHMODE_NUMBER; ihash++) {
std::shared_ptr<Hash> hash = HashModes::getHash(HModes(ihash));
std::string hash_info = HashModes::getInfo(HModes(ihash), true);
for (u_int64_t iters_ind = 0; iters_ind < iters_indices; iters_ind++) {
ChainHash ch = chainhashes[iters_ind];
u_int8_t run_iters = RUN_ITERS[iters_ind];
Timer timer;
std::thread memoryThread(MemoryThread);
timer.start();
for (int i = 0; i < run_iters; i++) {
ChainHashModes::performChainHash(ch, hash, data_str);
if (i != run_iters - 1) timer.recordTime();
}
timer.stop();
_terminateMeasurementThread = true;
memoryThread.join();
filing(chainhash_info, hash_info, ch.getIters(), timer.getAverageTime(), timer.getSlowest(), _memory_max, _memory_min, _memory_avg, _memory_base);
}
}
}
TEST(ChainHash, constant_count) {
// setup the chainhashes
std::string data_str = "test";
u_int8_t ichash = 4;
std::string chainhash_info = ChainHashModes::getShortInfo(CHModes(ichash));
std::shared_ptr<ChainHashData> chd1 = std::make_shared<ChainHashData>(Format{CHModes(ichash)});
chd1->generateRandomData();
std::vector<ChainHash> chainhashes = {ChainHash{CHModes(ichash), iterations[0], chd1}, ChainHash{CHModes(ichash), iterations[1], chd1}, ChainHash{CHModes(ichash), iterations[2], chd1}};
// the benchmark starts here
// run the chainhashes
for (u_int8_t ihash = 1; ihash <= MAX_HASHMODE_NUMBER; ihash++) {
std::shared_ptr<Hash> hash = HashModes::getHash(HModes(ihash));
std::string hash_info = HashModes::getInfo(HModes(ihash), true);
for (u_int64_t iters_ind = 0; iters_ind < iters_indices; iters_ind++) {
ChainHash ch = chainhashes[iters_ind];
u_int8_t run_iters = RUN_ITERS[iters_ind];
Timer timer;
std::thread memoryThread(MemoryThread);
timer.start();
for (int i = 0; i < run_iters; i++) {
ChainHashModes::performChainHash(ch, hash, data_str);
if (i != run_iters - 1) timer.recordTime();
}
timer.stop();
_terminateMeasurementThread = true;
memoryThread.join();
filing(chainhash_info, hash_info, ch.getIters(), timer.getAverageTime(), timer.getSlowest(), _memory_max, _memory_min, _memory_avg, _memory_base);
}
}
}
TEST(ChainHash, quadratic) {
// setup the chainhashes
std::string data_str = "test";
u_int8_t ichash = 5;
std::string chainhash_info = ChainHashModes::getShortInfo(CHModes(ichash));
std::shared_ptr<ChainHashData> chd1 = std::make_shared<ChainHashData>(Format{CHModes(ichash)});
chd1->generateRandomData();
std::vector<ChainHash> chainhashes = {ChainHash{CHModes(ichash), iterations[0], chd1}, ChainHash{CHModes(ichash), iterations[1], chd1}, ChainHash{CHModes(ichash), iterations[2], chd1}};
// the benchmark starts here
// run the chainhashes
for (u_int8_t ihash = 1; ihash <= MAX_HASHMODE_NUMBER; ihash++) {
std::shared_ptr<Hash> hash = HashModes::getHash(HModes(ihash));
std::string hash_info = HashModes::getInfo(HModes(ihash), true);
for (u_int64_t iters_ind = 0; iters_ind < iters_indices; iters_ind++) {
ChainHash ch = chainhashes[iters_ind];
u_int8_t run_iters = RUN_ITERS[iters_ind];
Timer timer;
std::thread memoryThread(MemoryThread);
timer.start();
for (int i = 0; i < run_iters; i++) {
ChainHashModes::performChainHash(ch, hash, data_str);
if (i != run_iters - 1) timer.recordTime();
}
timer.stop();
_terminateMeasurementThread = true;
memoryThread.join();
filing(chainhash_info, hash_info, ch.getIters(), timer.getAverageTime(), timer.getSlowest(), _memory_max, _memory_min, _memory_avg, _memory_base);
}
}
}