-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduckdb_comparison_bench.cpp
More file actions
259 lines (225 loc) · 9.62 KB
/
Copy pathduckdb_comparison_bench.cpp
File metadata and controls
259 lines (225 loc) · 9.62 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* @file duckdb_comparison_bench.cpp
* @brief Performance comparison between cloudSQL and DuckDB
*/
#include <benchmark/benchmark.h>
#include <duckdb.hpp>
#include <filesystem>
#include <memory>
#include <string>
#include <vector>
#include "catalog/catalog.hpp"
#include "common/config.hpp"
#include "executor/query_executor.hpp"
#include "parser/parser.hpp"
#include "storage/buffer_pool_manager.hpp"
#include "storage/heap_table.hpp"
#include "storage/storage_manager.hpp"
#include "transaction/lock_manager.hpp"
#include "transaction/transaction_manager.hpp"
using namespace cloudsql;
using namespace cloudsql::storage;
using namespace cloudsql::executor;
using namespace cloudsql::parser;
namespace {
// Helper to parse SQL string into a Statement
std::unique_ptr<Statement> ParseSQL(const std::string& sql) {
auto lexer = std::make_unique<Lexer>(sql);
Parser parser(std::move(lexer));
return parser.parse_statement();
}
// --- cloudSQL Setup ---
struct CloudSQLContext {
std::string test_dir;
std::unique_ptr<StorageManager> storage;
std::unique_ptr<BufferPoolManager> bpm;
std::unique_ptr<Catalog> catalog;
std::unique_ptr<transaction::LockManager> lock_manager;
std::unique_ptr<transaction::TransactionManager> txn_manager;
std::unique_ptr<QueryExecutor> executor;
CloudSQLContext(const std::string& dir) : test_dir(dir) {
std::filesystem::remove_all(test_dir);
std::filesystem::create_directories(test_dir);
storage = std::make_unique<StorageManager>(test_dir);
bpm = std::make_unique<BufferPoolManager>(4096, *storage);
catalog = std::make_unique<Catalog>();
lock_manager = std::make_unique<transaction::LockManager>();
txn_manager = std::make_unique<transaction::TransactionManager>(*lock_manager, *catalog, *bpm);
executor = std::make_unique<QueryExecutor>(*catalog, *bpm, *lock_manager, *txn_manager);
executor->set_local_only(true);
executor->set_storage_manager(storage.get()); // Enable use_vectorized for large scans
// Create lineitem table (TPC-H schema, simplified)
CreateTableStatement create_stmt;
create_stmt.set_table_name("lineitem");
create_stmt.add_column("l_orderkey", "BIGINT");
create_stmt.add_column("l_partkey", "BIGINT");
create_stmt.add_column("l_quantity", "INT");
create_stmt.add_column("l_extendedprice", "DOUBLE");
create_stmt.add_column("l_discount", "DOUBLE");
create_stmt.add_column("l_tax", "DOUBLE");
executor->execute(create_stmt);
}
~CloudSQLContext() {
executor.reset();
txn_manager.reset();
lock_manager.reset();
catalog.reset();
bpm.reset();
storage.reset();
std::filesystem::remove_all(test_dir);
}
};
// --- DuckDB Setup ---
struct DuckDBContext {
duckdb::DuckDB db;
duckdb::Connection conn;
DuckDBContext() : db(":memory:"), conn(db) {
conn.Query(
"CREATE TABLE lineitem (l_orderkey BIGINT, l_partkey BIGINT, l_quantity INT, "
"l_extendedprice DOUBLE, l_discount DOUBLE, l_tax DOUBLE)");
}
~DuckDBContext() {}
};
} // anonymous namespace
// --- Benchmark 1: cloudSQL Lineitem Aggregation (Q1-like) ---
static void BM_CloudSQL_Q1(benchmark::State& state) {
const int num_rows = state.range(0);
CloudSQLContext ctx("./bench_cloudsql_q1_" + std::to_string(state.thread_index()));
// Populate
ctx.executor->execute("BEGIN");
for (int i = 0; i < num_rows; ++i) {
ctx.executor->execute(*ParseSQL(
"INSERT INTO lineitem VALUES (" + std::to_string(i % 1000) + ", " +
std::to_string(i % 100) + ", " + std::to_string(1 + (i % 10)) + ", " +
"1000.0, 0.05, 0.02);"));
}
ctx.executor->execute("COMMIT");
for (auto _ : state) {
auto result = ctx.executor->execute(
*ParseSQL("SELECT l_quantity, SUM(l_extendedprice), AVG(l_discount) FROM lineitem GROUP BY "
"l_quantity"));
benchmark::DoNotOptimize(result);
}
state.SetItemsProcessed(state.iterations() * num_rows);
}
BENCHMARK(BM_CloudSQL_Q1)->Arg(10000)->Arg(100000);
// --- Benchmark 2: DuckDB Lineitem Aggregation (Q1-like) ---
static void BM_DuckDB_Q1(benchmark::State& state) {
const int num_rows = state.range(0);
DuckDBContext ctx;
// Populate
for (int i = 0; i < num_rows; ++i) {
ctx.conn.Query("INSERT INTO lineitem VALUES (" + std::to_string(i % 1000) + ", " +
std::to_string(i % 100) + ", " + std::to_string(1 + (i % 10)) + ", " +
"1000.0, 0.05, 0.02)");
}
for (auto _ : state) {
auto result = ctx.conn.Query(
"SELECT l_quantity, SUM(l_extendedprice), AVG(l_discount) FROM lineitem GROUP BY "
"l_quantity");
benchmark::DoNotOptimize(result);
}
state.SetItemsProcessed(state.iterations() * num_rows);
}
BENCHMARK(BM_DuckDB_Q1)->Arg(10000)->Arg(100000);
// --- Benchmark 3: cloudSQL Scan with Filter (Q6-like) ---
static void BM_CloudSQL_Q6(benchmark::State& state) {
const int num_rows = state.range(0);
CloudSQLContext ctx("./bench_cloudsql_q6_" + std::to_string(state.thread_index()));
// Populate
ctx.executor->execute("BEGIN");
for (int i = 0; i < num_rows; ++i) {
ctx.executor->execute(*ParseSQL(
"INSERT INTO lineitem VALUES (" + std::to_string(i) + ", " +
std::to_string(i % 100) + ", " + std::to_string(1 + (i % 10)) + ", " +
"1000.0, 0.05, 0.02);"));
}
ctx.executor->execute("COMMIT");
for (auto _ : state) {
auto result = ctx.executor->execute(*ParseSQL(
"SELECT SUM(l_extendedprice) FROM lineitem WHERE l_discount BETWEEN 0.04 AND 0.06 AND "
"l_quantity < 25"));
benchmark::DoNotOptimize(result);
}
state.SetItemsProcessed(state.iterations() * num_rows);
}
BENCHMARK(BM_CloudSQL_Q6)->Arg(10000)->Arg(100000);
// --- Benchmark 4: DuckDB Scan with Filter (Q6-like) ---
static void BM_DuckDB_Q6(benchmark::State& state) {
const int num_rows = state.range(0);
DuckDBContext ctx;
// Populate
for (int i = 0; i < num_rows; ++i) {
ctx.conn.Query("INSERT INTO lineitem VALUES (" + std::to_string(i) + ", " +
std::to_string(i % 100) + ", " + std::to_string(1 + (i % 10)) + ", " +
"1000.0, 0.05, 0.02)");
}
for (auto _ : state) {
auto result = ctx.conn.Query(
"SELECT SUM(l_extendedprice) FROM lineitem WHERE l_discount BETWEEN 0.04 AND 0.06 AND "
"l_quantity < 25");
benchmark::DoNotOptimize(result);
}
state.SetItemsProcessed(state.iterations() * num_rows);
}
BENCHMARK(BM_DuckDB_Q6)->Arg(10000)->Arg(100000);
// --- Benchmark 5: cloudSQL Simple Join (simplified Q3-like) ---
static void BM_CloudSQL_Join(benchmark::State& state) {
const int num_rows = state.range(0);
CloudSQLContext ctx("./bench_cloudsql_join_" + std::to_string(state.thread_index()));
// Create orders table
ctx.executor->execute(*ParseSQL("CREATE TABLE orders (o_orderkey BIGINT, o_custkey BIGINT, "
"o_orderdate TEXT)"));
// Populate orders
ctx.executor->execute("BEGIN");
for (int i = 0; i < num_rows / 10; ++i) {
ctx.executor->execute(*ParseSQL("INSERT INTO orders VALUES (" + std::to_string(i) +
", " + std::to_string(i % 100) + ", '2024-01-01')"));
}
// Populate lineitem
for (int i = 0; i < num_rows; ++i) {
ctx.executor->execute(*ParseSQL("INSERT INTO lineitem VALUES (" +
std::to_string(i % (num_rows / 10)) + ", " +
std::to_string(i % 100) + ", " +
std::to_string(1 + (i % 10)) + ", " +
"1000.0, 0.05, 0.02)"));
}
ctx.executor->execute("COMMIT");
for (auto _ : state) {
auto result = ctx.executor->execute(*ParseSQL(
"SELECT o.o_orderkey, SUM(l.l_extendedprice) FROM orders o JOIN lineitem l ON "
"o.o_orderkey = l.l_orderkey GROUP BY o.o_orderkey"));
benchmark::DoNotOptimize(result);
}
state.SetItemsProcessed(state.iterations() * num_rows);
}
BENCHMARK(BM_CloudSQL_Join)->Arg(10000)->Arg(50000);
// --- Benchmark 6: DuckDB Simple Join (simplified Q3-like) ---
static void BM_DuckDB_Join(benchmark::State& state) {
const int num_rows = state.range(0);
DuckDBContext ctx;
// Create orders table
ctx.conn.Query(
"CREATE TABLE orders (o_orderkey BIGINT, o_custkey BIGINT, o_orderdate TEXT)");
// Populate orders
for (int i = 0; i < num_rows / 10; ++i) {
ctx.conn.Query("INSERT INTO orders VALUES (" + std::to_string(i) + ", " +
std::to_string(i % 100) + ", '2024-01-01')");
}
// Populate lineitem
for (int i = 0; i < num_rows; ++i) {
ctx.conn.Query("INSERT INTO lineitem VALUES (" + std::to_string(i % (num_rows / 10)) + ", " +
std::to_string(i % 100) + ", " + std::to_string(1 + (i % 10)) + ", " +
"1000.0, 0.05, 0.02)");
}
for (auto _ : state) {
auto result = ctx.conn.Query(
"SELECT o.o_orderkey, SUM(l.l_extendedprice) FROM orders o JOIN lineitem l ON "
"o.o_orderkey = l.l_orderkey GROUP BY o.o_orderkey");
benchmark::DoNotOptimize(result);
}
state.SetItemsProcessed(state.iterations() * num_rows);
}
BENCHMARK(BM_DuckDB_Join)->Arg(10000)->Arg(50000);
// BENCHMARK_MAIN() is provided by benchmark::benchmark_main (linked via benchmark_main)
BENCHMARK_MAIN();