-
Notifications
You must be signed in to change notification settings - Fork 7
/
bench_open.cpp
66 lines (54 loc) · 1.7 KB
/
bench_open.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
#include <sys/stat.h>
#include <cxxopts.hpp>
#include <filesystem>
#include "common.h"
#include "posix.h"
void prep(const std::filesystem::path& file_path, uint64_t file_size) {
unlink(file_path.c_str());
int fd = open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
throw std::runtime_error("open failed");
}
prefill_file(fd, file_size, 4096);
struct stat st {};
madfs::posix::fstat(fd, &st);
printf("file size: %.3f MB\n", st.st_size / 1024. / 1024.);
close(fd);
}
void bench_open(const std::filesystem::path& file_path) {
int fd = open(file_path.c_str(), O_RDONLY);
assert(fd >= 0);
close(fd);
}
struct Args {
bool prep = false;
bool open = false;
uint64_t file_size = 4096;
std::filesystem::path file_path = "test.txt";
static Args parse(int argc, char** argv) {
cxxopts::Options options("bench_open", "Open benchmark");
Args args;
options.add_options(
{},
{
{"f,file", "Path to the file to store data",
cxxopts::value<std::filesystem::path>(args.file_path)},
{"p,prepare", "Prepare the file", cxxopts::value<bool>(args.prep)},
{"o,open", "Open the file", cxxopts::value<bool>(args.open)},
{"s,size", "File size in bytes",
cxxopts::value<uint64_t>(args.file_size)},
{"help", "Print help"},
});
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help() << std::endl;
exit(0);
}
return args;
}
};
int main(int argc, char** argv) {
const auto args = Args::parse(argc, argv);
if (args.prep) prep(args.file_path, args.file_size);
if (args.open) bench_open(args.file_path);
}