This repository has been archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
test_data.cpp
71 lines (62 loc) · 1.66 KB
/
test_data.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
/**
* > Author: UncP
* > Mail: [email protected]
* > Github: https://www.github.com/UncP/Mushroom
* > Created Time: 2017-03-25 14:30:45
**/
#include <random>
#include <ctime>
#include <string>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <cassert>
namespace Mushroom {
class MushroomDBTestData
{
public:
MushroomDBTestData(time_t seed):seed_(seed) { }
void Generate(int total, int file_num, int key_len) {
const std::string choice =
std::string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-*/");
std::default_random_engine generator(seed_);
std::uniform_int_distribution<int> distribution(0, choice.size()-1);
if ((total % file_num)) {
printf("fail to generate test data :(\n");
return ;
}
total /= file_num;
std::ostringstream os;
os << total;
if (access("data", F_OK))
assert(mkdir("data", S_IRUSR | S_IWUSR | S_IXUSR | S_IROTH) >= 0);
std::string base("data/"+os.str());
for (int i = 0; i != file_num; ++i) {
std::ostringstream o;
o << i;
std::ofstream out(base+"_"+o.str());
char key[key_len+1];
key[key_len] = '\n';
for (int j = 0; j != total; ++j) {
for (int k = 0; k != key_len; ++k)
key[k] = choice[distribution(generator)];
out.write(key, key_len+1);
}
out.close();
}
}
private:
time_t seed_;
};
} // namespace Mushroom
int main()
{
using namespace Mushroom;
MushroomDBTestData data(time(0));
// total_key file_number key_size
data.Generate(100000000, 4, 16);
// data.Generate(10000000, 1, 16);
return 0;
}