forked from huanchenz/index-microbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkload.cpp
222 lines (185 loc) · 6.34 KB
/
workload.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
215
216
217
218
219
220
221
222
#include <cstdlib>
#include "microbench.h"
typedef uint64_t keytype;
typedef std::less<uint64_t> keycomp;
static const uint64_t key_type=0;
void
load_operations(const std::string &txn_file, std::vector<int> &ops, std::vector<keytype> &keys, std::vector<int> &ranges);
//==============================================================
// GET INSTANCE
//==============================================================
template<typename KeyType, class KeyComparator>
Index<KeyType, KeyComparator> *getInstance(const int type, const uint64_t kt) {
if (type == 0)
return new BtreeIndex<KeyType, KeyComparator>(kt);
else if (type == 1)
return new ArtIndex<KeyType, KeyComparator>(kt);
else
return new BtreeIndex<KeyType, KeyComparator>(kt);
}
//==============================================================
// LOAD
//==============================================================
inline void load(std::string workloadName, int index_type, std::vector<keytype> &init_keys, std::vector<keytype> &keys, std::vector<uint64_t> &values, std::vector<int> &ranges, std::vector<int> &ops) {
std::string init_file = "workloads/" + workloadName + "_load.dat";
std::string txn_file = "workloads/" + workloadName + "_txn.dat";
check_input_files(init_file, txn_file);
load_initial_keys(init_file, init_keys, values, [](keytype const &key) {
return key;
});
load_operations<keytype>(txn_file, ops, keys, ranges);
}
//==============================================================
// EXEC
//==============================================================
inline void exec(int index_type, std::vector<keytype> &init_keys, std::vector<keytype> &keys, std::vector<uint64_t> &values, std::vector<int> &ranges, std::vector<int> &ops) {
Index<keytype, keycomp> *idx = getInstance<keytype, keycomp>(index_type, key_type);
//WRITE ONLY TEST-----------------
int count = 0;
double start_time = get_now();
while (count < (int)init_keys.size()) {
if (!idx->insert(init_keys[count], values[count])) {
std::cout << "LOAD FAIL!\n";
return;
}
count++;
}
double end_time = get_now();
double tput = count / (end_time - start_time) / 1000000; //Mops/sec
std::cout << "insert " << tput << "\n";
std::cout << "memory " << (idx->getMemory() / 1000000) << "\n\n";
//idx->merge();
std::cout << "static memory " << (idx->getMemory() / 1000000) << "\n\n";
//return;
//READ/UPDATE/SCAN TEST----------------
start_time = get_now();
int txn_num = 0;
uint64_t sum = 0;
uint64_t s = 0;
#ifdef PAPI_IPC
//Variables for PAPI
float real_time, proc_time, ipc;
long long ins;
int retval;
if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) {
printf("PAPI error: retval: %d\n", retval);
exit(1);
}
#endif
#ifdef PAPI_CACHE
static const int EVENT_COUNT = 3;
int events[EVENT_COUNT] = {PAPI_L1_TCM, PAPI_L2_TCM, PAPI_L3_TCM};
long long counters[EVENT_COUNT];
int retval;
if ((retval = PAPI_start_counters(events, EVENT_COUNT)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to start counters: %s\n", PAPI_strerror(retval));
exit(1);
}
#endif
size_t inserts = 0ul;
size_t reads = 0ul;
size_t updates = 0ul;
size_t scans = 0ul;
while ((txn_num < LIMIT) && (txn_num < (int)ops.size())) {
if (ops[txn_num] == 0) { //INSERT
idx->insert(keys[txn_num] + 1, values[txn_num]);
++inserts;
/*
if (!idx->insert(keys[txn_num] + 1, values[txn_num])) { //need to modify +1
std::cout << "INSERT FAIL!\n";
return -1;
}
*/
}
else if (ops[txn_num] == 1) { //READ
sum += idx->find(keys[txn_num]);
++reads;
/*
s = idx->find(keys[txn_num]);
if (s == 0)
std::cout << "read fail\n";
sum += s;
*/
}
else if (ops[txn_num] == 2) { //UPDATE
idx->upsert(keys[txn_num], values[txn_num]);
++updates;
}
else if (ops[txn_num] == 3) { //SCAN
idx->scan(keys[txn_num], ranges[txn_num]);
++scans;
}
else {
std::cout << "UNRECOGNIZED CMD!\n";
return;
}
txn_num++;
}
#ifdef PAPI_IPC
if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) {
printf("PAPI error: retval: %d\n", retval);
exit(1);
}
std::cout << "Time = " << real_time << "\n";
std::cout << "Tput = " << LIMIT/real_time << "\n";
std::cout << "Inst = " << ins << "\n";
std::cout << "IPC = " << ipc << "\n";
#endif
#ifdef PAPI_CACHE
if ((retval = PAPI_read_counters(counters, EVENT_COUNT)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to read counters: %s\n", PAPI_strerror(retval));
exit(1);
}
std::cout << "L1 miss = " << counters[0] << "\n";
std::cout << "L2 miss = " << counters[1] << "\n";
std::cout << "L3 miss = " << counters[2] << "\n";
#endif
std::cout << std::endl;
std::cout << "Inserts = " << inserts << "\n";
std::cout << "Updates = " << updates << "\n";
std::cout << "Reads = " << reads << "\n";
std::cout << "Scans = " << scans << "\n";
std::cout << std::endl;
end_time = get_now();
tput = txn_num / (end_time - start_time) / 1000000; //Mops/sec
std::cout << "sum = " << sum << "\n";
std::vector<std::string> operationsOccured;
if(reads > 0) {
operationsOccured.push_back("read");
}
if(scans > 0) {
operationsOccured.push_back("scan");
}
if(updates > 0) {
operationsOccured.push_back("update");
}
if(inserts > 0) {
operationsOccured.push_back("insert");
}
std::string operationSummary = "";
for(size_t i=0; i < operationsOccured.size(); ++i) {
if(i != 0) {
operationSummary += "/";
}
operationSummary += operationsOccured[i];
}
std::cout << operationSummary << " " << tput << " Mops/sec\n";
}
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cout << "Usage:\n";
std::cout << "1. workload-name: basename of workload file (workloads/email_load_<workload-name>.dat) and transaction file (workloads/email_txn_<workload-name>.dat).\n";
std::cout << "2. index type: btree, art\n";
return 1;
}
std::string workloadName { argv[1] };
int index_type = get_and_check_index_type(argv[2]);
std::vector<keytype> init_keys;
std::vector<keytype> keys;
std::vector<uint64_t> values;
std::vector<int> ranges;
std::vector<int> ops; //INSERT = 0, READ = 1, UPDATE = 2
load(workloadName, index_type, init_keys, keys, values, ranges, ops);
exec(index_type, init_keys, keys, values, ranges, ops);
return 0;
}