-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence.cc
198 lines (170 loc) · 5.41 KB
/
persistence.cc
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
#include <iostream>
#include <cstdint>
#include <string>
#include <cassert>
#include "test.h"
class PersistenceTest : public Test {
private:
const uint64_t TEST_MAX = 1024 *32;
void prepare(uint64_t max)
{
uint64_t i;
// Clean up
store.reset();
// Test multiple key-value pairs
for (i = 0; i < max; ++i) {
store.put(i, std::string(i+1, 's'));
EXPECT(std::string(i+1, 's'), store.get(i));
}
phase();
// Test after all insertions
for (i = 0; i < max; ++i)
EXPECT(std::string(i+1, 's'), store.get(i));
phase();
// Test deletions
for (i = 0; i < max; i+=2)
EXPECT(true, store.del(i));
phase();
for (i = 0; i < max; ++i) {
EXPECT((i & 1) ? std::string(i+1, 's') : not_found,
store.get(i));
}
phase();
// Prepare data for Test Mode
for (i = 0; i < max; ++i) {
switch (i & 3) {
case 0:
EXPECT(not_found, store.get(i));
store.put(i, std::string(i+1, 't'));
break;
case 1:
EXPECT(std::string(i+1, 's'), store.get(i));
store.put(i, std::string(i+1, 't'));
break;
case 2:
EXPECT(not_found, store.get(i));
break;
case 3:
EXPECT(std::string(i+1, 's'), store.get(i));
break;
default:
assert(0);
}
}
report();
/**
* Write a10MB data to drain previous data out of memory.
*/
for (i = 0; i <= 10240; ++i)
store.put(max + i, std::string(1024, 'x'));
for (i = 0; i < max; ++i) {
if(i==1649)
int k=0;
EXPECT((i & 1) ? std::string(i+1, 's') : not_found,
store.get(i));
}
phase();
std::cout << "Data is ready, please press ctrl-c/ctrl-d to"
" terminate this program!" << std::endl;
std::cout.flush();
while (true) {
volatile int dummy;
for (i = 0; i <= 1024; ++i) {
// The loop slows down the program
for (i = 0; i <= 1000; ++i)
dummy = i;
store.del(max + i);
for (i = 0; i <= 1000; ++i)
dummy = i;
store.put(max + i, std::string(1024, '.'));
for (i = 0; i <= 1000; ++i)
dummy = i;
store.put(max + i, std::string(512, 'x'));
}
}
}
void test(uint64_t max)
{
uint64_t i;
// Test data
for (i = 0; i < max; ++i) {
EXPECT((i & 1) ? std::string(i+1, 's') : not_found,
store.get(i));
switch (i & 1) {
case 0:
EXPECT(not_found, store.get(i));
break;
case 1:
EXPECT(std::string(i+1, 's'), store.get(i));
break;
case 2:
EXPECT(not_found, store.get(i));
break;
case 3:
EXPECT(std::string(i+1, 's'), store.get(i));
break;
default:
assert(0);
}
}
phase();
report();
}
public:
PersistenceTest(const std::string &dir, bool v=true) : Test(dir, v)
{
}
void start_test(void *args = NULL) override
{
bool testmode = (args && *static_cast<bool *>(args));
std::cout << "KVStore Persistence Test" << std::endl;
if (testmode) {
std::cout << "<<Test Mode>>" << std::endl;
test(TEST_MAX);
} else {
std::cout << "<<Preparation Mode>>" << std::endl;
prepare(TEST_MAX);
}
}
};
void usage(const char *prog, const char *verb, const char *mode)
{
std::cout << "Usage: " << prog << " [-t] [-v]" << std::endl;
std::cout << " -t: test mode for persistence test,"
" if -t is not given, the program only prepares data for test."
" [currently " << mode << "]" << std::endl;
std::cout << " -v: print extra info for failed tests [currently ";
std::cout << verb << "]" << std::endl;
std::cout << std::endl;
std::cout << " NOTE: A normal usage is as follows:" << std::endl;
std::cout << " 1. invoke `" << prog << "`;" << std::endl;
std::cout << " 2. terminate (kill) the program when data is ready;";
std::cout << std::endl;
std::cout << " 3. invoke `" << prog << "-t ` to test." << std::endl;
std::cout << std::endl;
std::cout.flush();
}
int main(int argc, char *argv[])
{
bool verbose = false;
bool testmode = false;
if (argc == 2) {
verbose = std::string(argv[1]) == "-v";
testmode = std::string(argv[1]) == "-t";
} else if (argc == 3) {
verbose = std::string(argv[1]) == "-v" ||
std::string(argv[2]) == "-v";
testmode = std::string(argv[1]) == "-t" ||
std::string(argv[2]) == "-t";
} else if (argc > 3) {
std::cerr << "Too many arguments." << std::endl;
usage(argv[0], "OFF", "Preparation Mode");
exit(-1);
}
usage(argv[0], verbose ? "ON" : "OFF",
testmode ? "Test Mode" : "Preparation Mode");
PersistenceTest test("./data", verbose);
testmode = true;
test.start_test(static_cast<void *>(&testmode));
return 0;
}