forked from Sketch-Data-Stream/On-Off-Sketch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.h
245 lines (188 loc) · 5.98 KB
/
benchmark.h
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#ifndef BENCHMARK_H
#define BENCHMARK_H
#include <sys/stat.h>
#include <chrono>
#include "SS.h"
#include "PIE.h"
#include "OO_FPI.h"
#include "OO_SIMD.h"
#include "CM_HT.h"
#include "CM_BF.h"
#include "OO_PE.h"
template<typename DATA_TYPE,typename COUNT_TYPE>
class BenchMark{
public:
typedef std::vector<Abstract<DATA_TYPE, COUNT_TYPE>*> AbsVector;
typedef std::unordered_map<DATA_TYPE, COUNT_TYPE> HashMap;
BenchMark(const char* _PATH, const COUNT_TYPE _T):
PATH(_PATH){
struct stat statbuf;
stat(PATH, &statbuf);
LENGTH = ceil(statbuf.st_size / (double)(_T * sizeof(DATA_TYPE)));
FILE* file = fopen(PATH, "rb");
COUNT_TYPE number = 0;
DATA_TYPE item;
HashMap record;
TOTAL = 0;
T = 0;
while(fread(&item, sizeof(DATA_TYPE), 1, file) > 0){
if(number % LENGTH == 0)
T += 1;
number += 1;
if(record[item] != T){
record[item] = T;
mp[item] += 1;
TOTAL += 1;
}
}
fclose(file);
}
void SketchError(uint32_t section){
AbsVector PEs = {
new OO_PE<DATA_TYPE, COUNT_TYPE>(3, 20000000 / 3.0 / (BITSIZE + sizeof(COUNT_TYPE))),
};
BenchInsert(PEs);
for(auto PE : PEs){
CheckDistribution(PE, section);
PECheckError(PE);
delete PE;
}
}
void TopKError(double alpha){
AbsVector FPIs = {
new OO_FPI<DATA_TYPE, COUNT_TYPE, 8>(200000),
};
BenchInsert(FPIs);
for(auto FPI : FPIs){
FPICheckError(FPI, alpha * TOTAL);
delete FPI;
}
}
void Thp(){
for(uint32_t i = 0;i < 5;++i){
std::cout << i << std::endl;
AbsVector FPIs = {
new OO_PE<DATA_TYPE, COUNT_TYPE>(3, 20000000 / 3.0 / (BITSIZE + sizeof(COUNT_TYPE))),
new OO_FPI<DATA_TYPE, COUNT_TYPE, 8>(200000),
};
for(auto FPI : FPIs){
InsertThp(FPI);
delete FPI;
}
}
}
private:
double TOTAL;
COUNT_TYPE T;
COUNT_TYPE LENGTH;
HashMap mp;
const char* PATH;
typedef std::chrono::high_resolution_clock::time_point TP;
inline TP now(){
return std::chrono::high_resolution_clock::now();
}
inline double durationms(TP finish, TP start){
return std::chrono::duration_cast<std::chrono::duration<double,std::ratio<1,1000000>>>(finish - start).count();
}
void BenchInsert(AbsVector sketches){
FILE* file = fopen(PATH, "rb");
DATA_TYPE item;
COUNT_TYPE number = 0, windowId = 0;
while(fread(&item, sizeof(DATA_TYPE), 1, file) > 0){
if(number % LENGTH == 0){
windowId += 1;
for(auto sketch : sketches)
sketch->NewWindow(windowId);
}
number += 1;
for(auto sketch : sketches)
sketch->Insert(item, windowId);
}
fclose(file);
}
void InsertThp(Abstract<DATA_TYPE, COUNT_TYPE>* sketch){
TP start, finish;
FILE* file = fopen(PATH, "rb");
DATA_TYPE item;
COUNT_TYPE number = 0, windowId = 0;
HashMap record;
start = now();
while(fread(&item, sizeof(DATA_TYPE), 1, file) > 0){
if(number % LENGTH == 0){
windowId += 1;
sketch->NewWindow(windowId);
}
number += 1;
sketch->Insert(item, windowId);
}
finish = now();
fclose(file);
std::cout << "Thp: " << number / durationms(finish, start) << std::endl;
}
void FPICheckError(Abstract<DATA_TYPE, COUNT_TYPE>* sketch, COUNT_TYPE HIT){
double real = 0, estimate = 0, both = 0;
double aae = 0, cr = 0, pr = 0, f1 = 0;
for(auto it = mp.begin();it != mp.end();++it){
COUNT_TYPE value = sketch->Query(it->first);
if(value > HIT){
estimate += 1;
if(it->second > HIT) {
both += 1;
aae += abs(it->second - value);
}
}
if(it->second > HIT)
real += 1;
}
if(both <= 0){
std::cout << "Not Find Real Persistent" << std::endl;
}
else{
aae /= both;
}
cr = both / real;
if(estimate <= 0){
std::cout << "Not Find Persistent" << std::endl;
}
else{
pr = both / estimate;
}
if(cr == 0 && pr == 0)
f1 = 0;
else
f1 = (2*cr*pr)/(cr+pr);
std::cout << HIT << std::endl
<< "AAE: " << aae << std::endl
<< "F1: " << f1 << std::endl;
}
void PECheckError(Abstract<DATA_TYPE, COUNT_TYPE>* sketch){
double aae = 0;
for(auto it = mp.begin();it != mp.end();++it){
COUNT_TYPE value = sketch->Query(it->first);
aae += abs(it->second - value);
}
std::cout << "AAE: " << aae / mp.size() << std::endl;
}
void CheckDistribution(Abstract<DATA_TYPE, COUNT_TYPE>* sketch, const uint32_t section){
uint32_t* aae = new uint32_t[section];
uint32_t* number = new uint32_t[section];
memset(aae, 0, sizeof(uint32_t) * section);
memset(number, 0, sizeof(uint32_t) * section);
for(auto it = mp.begin();it != mp.end();++it){
COUNT_TYPE value = sketch->Query(it->first);
uint32_t pos = (it->second * section - 1) / T;
aae[pos] += abs(it->second - value);
number[pos] += 1;
}
for(uint32_t i = 0;i < section;++i){
if(number[i] != 0)
std::cout << aae[i] / (double)number[i] << ",";
else
std::cout << "NULL,";
}
std::cout << std::endl;
delete [] aae;
delete [] number;
}
};
#endif //BENCHMARK_H