-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (143 loc) · 6.15 KB
/
main.cpp
File metadata and controls
164 lines (143 loc) · 6.15 KB
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
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
#include <chrono>
using namespace std;
static constexpr uint16_t hashLength = 20;
static constexpr size_t blockSize = 102400;
struct Block {
uint8_t hash[hashLength];
bool data[1];
};
void testDataBlocksWrite(boost::iostreams::mapped_file &file) {
if (!file.is_open())
return;
auto numberOfBlocks = 100;
auto start = chrono::high_resolution_clock::now();
size_t *currentBlock = reinterpret_cast<size_t *>(file.data());
for (auto i = 1; i <= numberOfBlocks; i++) {
*currentBlock = i;
auto block = reinterpret_cast<Block *>(file.data() + sizeof(size_t) + *currentBlock * (sizeof(Block) + blockSize));
for (auto j = 0; j < hashLength; j++)
block->hash[j] = i;
for (auto j = 0; j < blockSize; j++)
block->data[j] = i % 2;
}
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << numberOfBlocks << " data blocks written in " << elapsed.count() << " ms" << endl;
}
void testDataBlocksRead(boost::iostreams::mapped_file &file) {
if (!file.is_open())
return;
auto numberOfBlocks = 100;
auto start = chrono::high_resolution_clock::now();
size_t *currentBlock = reinterpret_cast<size_t *>(file.data());
if (*currentBlock != numberOfBlocks) {
cout << *currentBlock << " != " << numberOfBlocks << endl;
return;
}
for (auto i = 0; i < numberOfBlocks; i++) {
auto block = reinterpret_cast<Block *>(file.data() + sizeof(size_t) + i * (sizeof(Block) + blockSize));
for (auto j = 0; j < hashLength; j++)
if (block->hash[j] != i) {
cout << "Wrong block->hash[" << j << "]: " << block->hash[j] << " != " << i << endl;
break;
}
for (auto j = 0; j < blockSize; j++)
if (block->data[j] != (i % 2)) {
cout << "Wrong block->data[" << j << "]: " << block->data[j] << " != " << i % 2 << endl;
break;
}
}
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << numberOfBlocks << " data blocks read and checked in " << elapsed.count() << " ms" << endl;
}
void testAlignment(boost::iostreams::mapped_file &file) {
auto alignment = file.alignment();
cout << "The operating system's virtual memory allocation granularity: " << alignment << endl;
chrono::time_point<chrono::high_resolution_clock> start;
chrono::time_point<chrono::high_resolution_clock> end;
auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end - start);
// write test, we use r\w data pointer here
auto *data = reinterpret_cast<int8_t *>(file.data());
for (auto i = alignment * 5 - 5; i < alignment * 5 + 5; i++) {
start = chrono::high_resolution_clock::now();
data[i] = 0;
end = chrono::high_resolution_clock::now();
elapsed = chrono::duration_cast<chrono::nanoseconds>(end - start);
cout << "Byte #" << i << " rewritten in " << elapsed.count() << " ns" << endl;
}
// read test, we use r/o data pointer here
auto *constData = reinterpret_cast<const int8_t *>(file.const_data());
int8_t value = 0;
for (auto i = alignment * 10 - 5; i < alignment * 10 + 5; i++) {
start = chrono::high_resolution_clock::now();
value = constData[i];
end = chrono::high_resolution_clock::now();
elapsed = chrono::duration_cast<chrono::nanoseconds>(end - start);
cout << "Byte #" << i << " read in " << elapsed.count() << " ns" << endl;
}
}
// Read last bytes
void testWriteData(boost::iostreams::mapped_file &file) {
auto *data = reinterpret_cast<int8_t *>(file.data());
auto start = chrono::high_resolution_clock::now();
for (auto i = 0; i < file.size(); i++)
data[i] = 0;
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << file.size() << " bytes changed in " << elapsed.count() << "ms" << endl;
}
// Erase all data with zeroes
void testReadData(boost::iostreams::mapped_file &file) {
int numberOfElements = 100;
auto *data = reinterpret_cast<int8_t *>(file.data());
for (auto i = 0; i < numberOfElements; i++)
cout << int{data[/*file.size() - */i]} << " ";
cout << endl;
}
int main(int argc, char **argv) {
boost::iostreams::mapped_file file;
boost::iostreams::mapped_file_params params;
params.path = "filename.raw";
params.flags = boost::iostreams::mapped_file::readwrite;
// Test file open time
auto start = chrono::high_resolution_clock::now();
try {
file.open(params);
}
catch (const ios_base::failure &e) {
cerr << "Error opening file: " << e.what() << endl;
params.new_file_size = 1024 * 1024 * 10;
cout << "Creating new file of size " << params.new_file_size << endl;
file.open(params);
}
if (file.is_open()) {
cout << "File of size " << file.size() << " opened in ";
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << elapsed.count() << "ms" << endl;
// testReadData(file);
// testWriteData(file);
// testAlignment(file);
testDataBlocksWrite(file);
testDataBlocksRead(file);
// Don't forget to unmap
start = chrono::high_resolution_clock::now();
file.close();
end = chrono::high_resolution_clock::now();
elapsed = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "File closed in " << elapsed.count() << "ms" << endl;
// Test file reopen time
start = chrono::high_resolution_clock::now();
file.open(params);
end = chrono::high_resolution_clock::now();
file.close();
elapsed = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "File reopened in " << elapsed.count() << "ms" << endl;
}
else {
cout << "could not map file " << params.path << endl;
}
}