|
| 1 | +#include <benchmark/benchmark.h> |
| 2 | +#include <memory> |
| 3 | +#include <vector> |
| 4 | +#include <filesystem> |
| 5 | +#include "storage/storage_manager.hpp" |
| 6 | +#include "storage/buffer_pool_manager.hpp" |
| 7 | +#include "storage/heap_table.hpp" |
| 8 | +#include "executor/operator.hpp" |
| 9 | +#include "executor/query_executor.hpp" |
| 10 | +#include "parser/expression.hpp" |
| 11 | +#include "catalog/catalog.hpp" |
| 12 | +#include "common/config.hpp" |
| 13 | + |
| 14 | +using namespace cloudsql; |
| 15 | +using namespace cloudsql::storage; |
| 16 | +using namespace cloudsql::executor; |
| 17 | +using namespace cloudsql::parser; |
| 18 | + |
| 19 | +// Helper to create a table with N rows |
| 20 | +static void SetupBenchTable(HeapTable& table, int num_rows) { |
| 21 | + for (int i = 0; i < num_rows; ++i) { |
| 22 | + std::vector<common::Value> values = { |
| 23 | + common::Value::make_int64(i), |
| 24 | + common::Value::make_text("Data_" + std::to_string(i)) |
| 25 | + }; |
| 26 | + table.insert(Tuple(values), 0); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +static void BM_ExecutionSeqScan(benchmark::State& state) { |
| 31 | + std::string test_dir = "./bench_exec_scan_" + std::to_string(state.range(0)); |
| 32 | + std::filesystem::create_directories(test_dir); |
| 33 | + StorageManager disk_manager(test_dir); |
| 34 | + BufferPoolManager bpm(2000, disk_manager); |
| 35 | + |
| 36 | + Schema schema; |
| 37 | + schema.add_column("id", common::ValueType::TYPE_INT64); |
| 38 | + schema.add_column("data", common::ValueType::TYPE_TEXT); |
| 39 | + |
| 40 | + for (auto _ : state) { |
| 41 | + state.PauseTiming(); |
| 42 | + auto table = std::make_unique<HeapTable>("scan_table", bpm, schema); |
| 43 | + table->create(); |
| 44 | + SetupBenchTable(*table, state.range(0)); |
| 45 | + state.ResumeTiming(); |
| 46 | + |
| 47 | + auto scan_op = std::make_unique<SeqScanOperator>(std::move(table)); |
| 48 | + scan_op->init(); |
| 49 | + Tuple tuple; |
| 50 | + while (scan_op->next(tuple)) { |
| 51 | + benchmark::DoNotOptimize(tuple); |
| 52 | + } |
| 53 | + |
| 54 | + state.PauseTiming(); |
| 55 | + std::filesystem::remove_all(test_dir); |
| 56 | + std::filesystem::create_directories(test_dir); |
| 57 | + state.ResumeTiming(); |
| 58 | + } |
| 59 | + |
| 60 | + state.SetItemsProcessed(state.iterations() * state.range(0)); |
| 61 | + std::filesystem::remove_all(test_dir); |
| 62 | +} |
| 63 | +BENCHMARK(BM_ExecutionSeqScan)->Arg(1000)->Arg(10000); |
| 64 | + |
| 65 | +static void BM_ExecutionHashJoin(benchmark::State& state) { |
| 66 | + std::string test_dir = "./bench_exec_join_" + std::to_string(state.range(0)); |
| 67 | + std::filesystem::create_directories(test_dir); |
| 68 | + StorageManager disk_manager(test_dir); |
| 69 | + BufferPoolManager bpm(4000, disk_manager); |
| 70 | + |
| 71 | + Schema schema; |
| 72 | + schema.add_column("id", common::ValueType::TYPE_INT64); |
| 73 | + schema.add_column("data", common::ValueType::TYPE_TEXT); |
| 74 | + |
| 75 | + for (auto _ : state) { |
| 76 | + state.PauseTiming(); |
| 77 | + auto left_table = std::make_unique<HeapTable>("left_table", bpm, schema); |
| 78 | + left_table->create(); |
| 79 | + SetupBenchTable(*left_table, state.range(0)); |
| 80 | + |
| 81 | + auto right_table = std::make_unique<HeapTable>("right_table", bpm, schema); |
| 82 | + right_table->create(); |
| 83 | + SetupBenchTable(*right_table, state.range(0)); |
| 84 | + state.ResumeTiming(); |
| 85 | + |
| 86 | + auto left_scan = std::make_unique<SeqScanOperator>(std::move(left_table)); |
| 87 | + auto right_scan = std::make_unique<SeqScanOperator>(std::move(right_table)); |
| 88 | + |
| 89 | + // Join on "id" |
| 90 | + auto left_key = std::make_unique<ColumnExpr>("id"); |
| 91 | + auto right_key = std::make_unique<ColumnExpr>("id"); |
| 92 | + |
| 93 | + auto join_op = std::make_unique<HashJoinOperator>( |
| 94 | + std::move(left_scan), std::move(right_scan), std::move(left_key), std::move(right_key)); |
| 95 | + |
| 96 | + join_op->init(); |
| 97 | + Tuple tuple; |
| 98 | + while (join_op->next(tuple)) { |
| 99 | + benchmark::DoNotOptimize(tuple); |
| 100 | + } |
| 101 | + |
| 102 | + state.PauseTiming(); |
| 103 | + std::filesystem::remove_all(test_dir); |
| 104 | + std::filesystem::create_directories(test_dir); |
| 105 | + state.ResumeTiming(); |
| 106 | + } |
| 107 | + |
| 108 | + state.SetItemsProcessed(state.iterations() * state.range(0)); |
| 109 | + std::filesystem::remove_all(test_dir); |
| 110 | +} |
| 111 | +BENCHMARK(BM_ExecutionHashJoin)->Arg(100)->Arg(1000); |
0 commit comments