Skip to content

Instantly share code, notes, and snippets.

@daveyc
Last active June 5, 2023 00:49
Show Gist options
  • Save daveyc/14b45d6d70d8dd9af1848e539d78881f to your computer and use it in GitHub Desktop.
Save daveyc/14b45d6d70d8dd9af1848e539d78881f to your computer and use it in GitHub Desktop.
// ibm-clang++64 -std=c++20 -O -o benchio benchio.cpp
#include "argparse.hpp"
#include "file.hpp"
#include <string>
static void read_file(const std::string & filename) {
std::string mode = "rb";
if (filename.starts_with("//")) {
mode += ",type=record,noseek";
}
char buf[4096];
File f(filename, mode);
while (f.read(buf, sizeof buf));
}
static void write_file(const std::string & filename, size_t numrecs, bool sync) {
std::string mode = "wb";
if (filename.starts_with("//")) {
mode += ",type=record,recfm=fb,lrecl=4096,blksize=0";
}
char buf[4096] = {0};
File f(filename, mode);
while (numrecs--) {
f.write(buf, sizeof buf);
if (sync) f.sync();
}
}
int main(int argc, char *argv[]) {
argparse::ArgumentParser program("benchio");
program.add_argument("--mode")
.required()
.help("the mode, 'read' or 'write'");
program.add_argument("-n", "--numrecs")
.help("the number of records to write")
.default_value(1000000)
.scan<'i', int>();
program.add_argument("-f", "--sync")
.help("sync record after write operation")
.default_value(false)
.implicit_value(false);
program.add_argument("filename")
.help("the name of the file or data set");
try {
program.parse_args(argc, argv);
} catch (const std::runtime_error &err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
return 1;
}
auto filename = program.get<std::string>("filename");
auto mode = program.get<std::string>("mode");
try {
if (mode == "read") {
read_file(filename);
} else if (mode == "write") {
auto n = program.get<int>("numrecs");
write_file(filename, n, program.get<bool>("sync"));
} else {
throw std::runtime_error("Invalid mode: " + mode);
}
} catch (std::exception & e) {
std::cerr << e.what() << "\n";
return 1;
}
}
/*
########## writes ############
> time ./benchio --mode write -n 200000 "//fb4096"
real 0m9.654s
user 0m0.138s
sys 0m0.055s
> time ./benchio --mode write -n 200000 fb4096.bin
real 0m2.850s
user 0m1.502s
sys 0m0.509s
> time ./benchio --mode write -n 200000 --sync fb4096.bin
real 0m2.976s
user 0m1.561s
sys 0m0.529s
########## reads ############
> time ./benchio --mode read "//fb4096"
real 0m3.304s
user 0m0.063s
sys 0m0.031s
> time ./benchio --mode read fb4096.bin
real 0m0.664s
user 0m0.457s
sys 0m0.161s
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment