-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.cpp
141 lines (110 loc) · 3.41 KB
/
benchmark.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
#include <cstdint>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cassert>
#include <unistd.h>
#include "SecBlock.h"
#include "SecBlockAlloc.h"
#include <chrono>
class TimeMeasure{
private:
std::chrono::steady_clock::time_point tp;
public:
TimeMeasure(){
reset();
}
void reset(){
tp = std::chrono::steady_clock::now();
}
uint32_t report(){
auto intv =
std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::steady_clock::now() - tp);
reset();
return (uint32_t) intv.count();
}
};
uint32_t rand_access(uint32_t times){
uint32_t sum = 0;
return sum;
}
void exitUsage(const char *progName){
std::cout << "Usage: " << progName << " AllocTimes AccessTimes";
exit(-1);
}
int strtoint(const char *str){
for (const char *p = str; *p; p++)
if (*p <'0' || *p > '9') return -1;
return atoi(str);
}
void TestAlloc(const char* desp, uint32_t times, TC::SecBlockAlloc* alloc){
std::vector<uint32_t> data;
for (uint32_t i = 0; i < (BLOCK_SIZE >> 2); i++)
data.push_back(rand());
TC::SecBlock* newBlocks[times];
uint32_t nanoSec;
TimeMeasure tm;
tm.reset();
for (uint32_t i = 0; i < times; i++){
newBlocks[i] = alloc->AllocBlock(data);
}
nanoSec = tm.report();
std::cout << desp << ": \t" << nanoSec << std::endl;
tm.reset();
for (uint32_t i = 0; i < times; i++){
alloc->FreeBlock(newBlocks[i]);
}
nanoSec = tm.report();
std::cout << desp << ": \t" << nanoSec << std::endl;
}
void TestAccess(const char* desp, uint32_t times, TC::SecBlockAlloc *alloc){
uint32_t numBlock = (NUM_BLOCK + rand()%NUM_BLOCK) / 2;
TC::SecBlock* blocks[numBlock];
std::vector<uint32_t> data[numBlock];
for (uint32_t i = 0; i < numBlock; i++){
data[i].clear();
for (uint32_t j = 0; j < (BLOCK_SIZE >> 2); j++){
data[i].push_back(rand());
}
blocks[i] = alloc->AllocBlock(data[i]);
}
TimeMeasure tm;
tm.reset();
for (uint32_t i = 0; i < times; i++){
uint32_t id = rand() % numBlock;
uint32_t bitid = rand() % (BLOCK_SIZE * 8);
assert(blocks[id]->access(bitid) ==
((data[id][bitid >> 5] >> (bitid & ((1 << 5) - 1))) & 1));
}
uint32_t nanoSec = tm.report();
std::cout << desp << ":\t" << nanoSec << std::endl;
for (uint32_t i = 0; i < numBlock; i++)
alloc->FreeBlock(blocks[i]);
}
int main(int argc, char *argv[]){
int AllocTimes = 20000;
int AccessTimes = 1<<20;
if (argc > 1){
if (argc > 3)
exitUsage(argv[0]);
if (argc >= 2){
if (-1 == (AllocTimes = strtoint(argv[1])))
exitUsage(argv[0]);
}
if (argc == 3){
if (-1 == (AccessTimes = strtoint(argv[2])))
exitUsage(argv[0]);
}
}
TimeMeasure tm;
usleep(100);
uint32_t nanoSec = tm.report();
std::cout << "10us = " << nanoSec << "ns" << std::endl;
TC::SecBlockAlloc *TCAlloc = TC::AllocBlockAllocator("TC");
TC::SecBlockAlloc *HMAlloc = TC::AllocBlockAllocator("HM");
TestAlloc("TCAlloc", AllocTimes, TCAlloc);
TestAlloc("HMAlloc", AllocTimes, HMAlloc);
TestAccess("TCAccess", AccessTimes, TCAlloc);
TestAccess("HMAccess", AccessTimes, HMAlloc);
}