Skip to content

Commit

Permalink
Reformat with clang-format.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangacer committed Jun 9, 2017
1 parent f93c700 commit 85dacdc
Show file tree
Hide file tree
Showing 5 changed files with 757 additions and 855 deletions.
189 changes: 88 additions & 101 deletions bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,138 +28,125 @@
* POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#include <cstring>
#include <fstream>
#include <iostream>
#include <functional>
#include <cstring>
#include <iostream>
#include "sqlite3cpp.h"

std::function<void()> gen_test_data(int index, int argc, char **argv)
{
if (index + 1 >= argc)
throw std::invalid_argument("missing data size (mb)");
std::function<void()> gen_test_data(int index, int argc, char **argv) {
if (index + 1 >= argc) throw std::invalid_argument("missing data size (mb)");

size_t data_size = strtoul(argv[index+1], 0, 10) << 20;
size_t data_size = strtoul(argv[index + 1], 0, 10) << 20;

return [data_size](){
sqlite3cpp::database db("testdata.db");
return [data_size]() {
sqlite3cpp::database db("testdata.db");

auto c = db.make_cursor();
auto c = db.make_cursor();

c.executescript(
"pragma journal_mode=wal;"
"drop table if exists T;"
"create table T (msg TEXT, rand INTEGER);"
);
c.executescript(
"pragma journal_mode=wal;"
"drop table if exists T;"
"create table T (msg TEXT, rand INTEGER);");

auto cnt = data_size >> 4;
for(size_t i = 0; i < cnt; ++i) {
c.executescript("insert into T values(strftime('%Y-%m-%d %H:%M:%f', 'now'), random())");
}
};
auto cnt = data_size >> 4;
for (size_t i = 0; i < cnt; ++i) {
c.executescript(
"insert into T values(strftime('%Y-%m-%d %H:%M:%f', 'now'), "
"random())");
}
};
}


template<typename T>
template <typename T>
struct scan {
static void sequential() {
sqlite3cpp::database db("testdata.db");

static void sequential() {
sqlite3cpp::database db("testdata.db");

auto c = db.make_cursor();
size_t cnt = 0;
T ts;

for (auto const & row : c.execute("select msg from T")) {
std::tie(ts) = row.to<T>();
cnt += 1;
}
auto c = db.make_cursor();
size_t cnt = 0;
T ts;

std::cout << "scan " << cnt << " rows" << std::endl;
for (auto const &row : c.execute("select msg from T")) {
std::tie(ts) = row.to<T>();
cnt += 1;
}

static void random() {
sqlite3cpp::database db("testdata.db");
std::cout << "scan " << cnt << " rows" << std::endl;
}

auto c = db.make_cursor();
size_t cnt = 0;
T ts;
static void random() {
sqlite3cpp::database db("testdata.db");

for (auto const & row : c.execute("select msg from T order by rand")) {
std::tie(ts) = row.to<T>();
cnt += 1;
}
auto c = db.make_cursor();
size_t cnt = 0;
T ts;

std::cout << "scan " << cnt << " rows" << std::endl;
for (auto const &row : c.execute("select msg from T order by rand")) {
std::tie(ts) = row.to<T>();
cnt += 1;
}

std::function<void()> operator()(int index, int argc, char **argv) const
{
if (index + 1 >= argc)
throw std::invalid_argument("missing scan pattern (seq|rand)");
std::cout << "scan " << cnt << " rows" << std::endl;
}

char const *pattern = argv[index + 1];
std::function<void()> operator()(int index, int argc, char **argv) const {
if (index + 1 >= argc)
throw std::invalid_argument("missing scan pattern (seq|rand)");

char const *pattern = argv[index + 1];

if (!strcmp("seq", pattern)) {
return scan::sequential;
} else if(!strcmp("rand", pattern)) {
return scan::random;
} else {
throw std::invalid_argument("invalid scan pattern");
}
if (!strcmp("seq", pattern)) {
return scan::sequential;
} else if (!strcmp("rand", pattern)) {
return scan::random;
} else {
throw std::invalid_argument("invalid scan pattern");
}
}
};

int main(int argc, char **argv) {

using std::function;
using opt_act_t = function<function<void()>(int idx, int argc, char **argv)>;

struct option {
char const *opt;
char const *cmt;
opt_act_t act;
} options[] = {
{
"-g",
"-g <mb>\tGenerate testdata.db of specified size.",
gen_test_data
}, {
"-rc",
"-rc <seq|rand>\tScan testdata with specified pattern (sequential or random) in copy semantic.",
scan<std::string>()
},{
"-rr",
"-rr <seq|rand>\tScan testdata with specified pattern (sequential or random) in ref semantic.",
scan<sqlite3cpp::string_ref>()
}, {
"-h",
"-h\tPrint usage.",
{}
}
};

opt_act_t help = [&options](int, int, char **) {

return [&options]() {
using namespace std;
cout << "Usage:" << endl;
for(auto const &op : options) {
cout << "\t" << op.cmt << endl;
}
};
using std::function;
using opt_act_t = function<function<void()>(int idx, int argc, char **argv)>;

struct option {
char const *opt;
char const *cmt;
opt_act_t act;
} options[] = {
{"-g", "-g <mb>\tGenerate testdata.db of specified size.", gen_test_data},
{"-rc",
"-rc <seq|rand>\tScan testdata with specified pattern (sequential or "
"random) in copy semantic.",
scan<std::string>()},
{"-rr",
"-rr <seq|rand>\tScan testdata with specified pattern (sequential or "
"random) in ref semantic.",
scan<sqlite3cpp::string_ref>()},
{"-h", "-h\tPrint usage.", {}}};

opt_act_t help = [&options](int, int, char **) {

return [&options]() {
using namespace std;
cout << "Usage:" << endl;
for (auto const &op : options) {
cout << "\t" << op.cmt << endl;
}
};
};

options[sizeof(options)/sizeof(option) - 1].act = help;
options[sizeof(options) / sizeof(option) - 1].act = help;

for (int i = 1; i < argc; ++i) {
for (auto const &op : options) {
if (strcmp(argv[i], op.opt)) {
continue;
}
op.act(i, argc, argv)();
for (int i = 1; i < argc; ++i) {
for (auto const &op : options) {
if (strcmp(argv[i], op.opt)) {
continue;
}
op.act(i, argc, argv)();
}
}

return 0;
return 0;
}

0 comments on commit 85dacdc

Please sign in to comment.