-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbench-duckdb.cpp
110 lines (90 loc) · 2.56 KB
/
bench-duckdb.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
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
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "common.h"
#include "duckdb/duckdb.hpp"
namespace {
namespace d = duckdb;
std::string_view DB_PREFIX = "db:";
std::pair<int, std::string_view> ParseArg(std::string_view s) {
int reps = -1;
bool found = false;
for (int i = 0; i < s.size(); ++i) {
if ('0' <= s[i] && s[i] <= '9') {
found = true;
} else if (s[i] == ':' && found) {
reps = std::stoul(std::string(s.substr(0, i)));
s.remove_prefix(i + 1);
break;
} else {
break;
}
}
return {reps, s};
}
} // namespace
int main(int argc, char* argv[]) {
std::unique_ptr<d::DuckDB> db_ptr;
int argi = 1;
if (argi < argc && std::string_view(argv[argi]).starts_with(DB_PREFIX)) {
std::string_view db_file(argv[1]);
db_file.remove_prefix(DB_PREFIX.size());
db_ptr = std::make_unique<d::DuckDB>(std::string(db_file));
} else {
db_ptr = std::make_unique<d::DuckDB>();
}
d::DuckDB& db = *db_ptr;
d::Connection con(db);
int query_idx = 1;
for (; argi < argc; ++argi) {
auto [reps, file] = ParseArg(argv[argi]);
std::ifstream f((std::string(file)));
std::stringstream file_ss;
file_ss << f.rdbuf();
std::string sql_str = std::move(file_ss).str();
std::stringstream str_ss;
str_ss << "q" << query_idx;
std::string str_i = std::move(str_ss).str();
if (reps > 0) {
std::unique_ptr<d::PreparedStatement> prepare;
time(
[&]() {
prepare = con.Prepare(sql_str);
return 0;
},
(str_i + " prep").c_str(), 1);
if (prepare->HasError()) {
std::cout << "Preparing " << file << " failed: " << prepare->GetError()
<< '\n';
return 1;
}
std::unique_ptr<d::QueryResult> res;
std::vector<d::Value> values;
res = prepare->Execute(values, /*allow_stream_result=*/false);
res = prepare->Execute(values, /*allow_stream_result=*/false);
time(
[&]() {
res = prepare->Execute(values,
/*allow_stream_result=*/false);
return 0;
},
str_i.c_str(), reps);
std::cout << res->ToString() << '\n';
} else {
std::unique_ptr<d::MaterializedQueryResult> res;
time(
[&]() {
res = con.Query(sql_str);
return 0;
},
str_i.c_str(), 1);
std::cout << res->ToString() << '\n';
}
++query_idx;
}
}