1 | #include "benchmark_runner.hpp" |
---|---|
2 | #include "duckdb_benchmark_macro.hpp" |
3 | #include "duckdb/main/appender.hpp" |
4 | |
5 | #include <random> |
6 | |
7 | using namespace duckdb; |
8 | using namespace std; |
9 | |
10 | #define GROUP_ROW_COUNT 1000000 |
11 | #define GROUP_COUNT 5 |
12 | |
13 | DUCKDB_BENCHMARK(BulkUpdate, "[bulkupdate]") |
14 | int64_t sum = 0; |
15 | int64_t count = 0; |
16 | void Load(DuckDBBenchmarkState *state) override { |
17 | state->conn.Query("CREATE TABLE integers(i INTEGER);"); |
18 | Appender appender(state->conn, "integers"); |
19 | // insert the elements into the database |
20 | for (size_t i = 0; i < GROUP_ROW_COUNT; i++) { |
21 | appender.BeginRow(); |
22 | appender.Append<int32_t>(i % GROUP_COUNT); |
23 | appender.EndRow(); |
24 | |
25 | sum += i % GROUP_COUNT; |
26 | count++; |
27 | } |
28 | } |
29 | |
30 | void RunBenchmark(DuckDBBenchmarkState *state) override { |
31 | state->conn.Query("BEGIN TRANSACTION"); |
32 | state->conn.Query("UPDATE integers SET i = i + 1"); |
33 | state->result = state->conn.Query("SELECT SUM(i) FROM integers"); |
34 | state->conn.Query("ROLLBACK"); |
35 | } |
36 | |
37 | string VerifyResult(QueryResult *result) override { |
38 | auto &materialized = (MaterializedQueryResult &)*result; |
39 | Value val = materialized.GetValue(0, 0); |
40 | if (val != Value::BIGINT(sum + count)) { |
41 | return string("Value "+ val.ToString() + " does not match expected value "+ to_string(sum + count)); |
42 | } |
43 | return string(); |
44 | } |
45 | string BenchmarkInfo() override { |
46 | return "Run a bulk update followed by an aggregate"; |
47 | } |
48 | FINISH_BENCHMARK(BulkUpdate) |
49 | |
50 | DUCKDB_BENCHMARK(BulkDelete, "[bulkupdate]") |
51 | int64_t sum = 0; |
52 | int64_t count = 0; |
53 | void Load(DuckDBBenchmarkState *state) override { |
54 | state->conn.Query("CREATE TABLE integers(i INTEGER);"); |
55 | Appender appender(state->conn, "integers"); |
56 | // insert the elements into the database |
57 | for (size_t i = 0; i < GROUP_ROW_COUNT; i++) { |
58 | appender.BeginRow(); |
59 | appender.Append<int32_t>(i % GROUP_COUNT); |
60 | appender.EndRow(); |
61 | |
62 | sum += i % GROUP_COUNT; |
63 | if ((i % GROUP_COUNT) == 1) { |
64 | count++; |
65 | } |
66 | } |
67 | } |
68 | |
69 | void RunBenchmark(DuckDBBenchmarkState *state) override { |
70 | state->conn.Query("BEGIN TRANSACTION"); |
71 | state->conn.Query("DELETE FROM integers WHERE i=1"); |
72 | state->result = state->conn.Query("SELECT SUM(i) FROM integers"); |
73 | state->conn.Query("ROLLBACK"); |
74 | } |
75 | |
76 | string VerifyResult(QueryResult *result) override { |
77 | auto &materialized = (MaterializedQueryResult &)*result; |
78 | Value val = materialized.GetValue(0, 0); |
79 | if (val != Value::BIGINT(sum - count)) { |
80 | return string("Value "+ val.ToString() + " does not match expected value "+ to_string(sum - count)); |
81 | } |
82 | return string(); |
83 | } |
84 | string BenchmarkInfo() override { |
85 | return "Run a bulk delete followed by an aggregate"; |
86 | } |
87 | FINISH_BENCHMARK(BulkDelete) |
88 |