1#include "benchmark_runner.hpp"
2#include "duckdb_benchmark_macro.hpp"
3#include "duckdb/main/appender.hpp"
4
5#include <random>
6
7using namespace duckdb;
8using namespace std;
9
10#define RANGE_QUERY_ROW_COUNT 100000000
11#define RANGE_QUERY_ENTRY_LOW 15000100
12#define RANGE_QUERY_ENTRY_HIGH 15000200
13#define SUM_RESULT 1515015150
14
15DUCKDB_BENCHMARK(RangeQueryWithoutIndex, "[micro]")
16virtual void Load(DuckDBBenchmarkState *state) {
17 state->conn.Query("CREATE TABLE integers(i INTEGER, j INTEGER);");
18 Appender appender(state->conn, "integers"); // insert the elements into the database
19 for (size_t i = 0; i < RANGE_QUERY_ROW_COUNT; i++) {
20 appender.BeginRow();
21 appender.Append<int32_t>(i);
22 appender.Append<int32_t>(i);
23 appender.EndRow();
24 }
25 appender.Close();
26}
27virtual std::string GetQuery() {
28 return "SELECT sum(j) FROM integers WHERE i >= " + to_string(RANGE_QUERY_ENTRY_LOW) +
29 " and i <= " + to_string(RANGE_QUERY_ENTRY_HIGH);
30}
31
32virtual std::string VerifyResult(QueryResult *result) {
33 if (!result->success) {
34 return result->error;
35 }
36 auto &materialized = (MaterializedQueryResult &)*result;
37 if (materialized.collection.count != 1) {
38 return "Incorrect amount of rows in result";
39 }
40 if (result->names.size() != 1) {
41 return "Incorrect amount of columns";
42 }
43 if (materialized.GetValue<int64_t>(0, 0) != SUM_RESULT) {
44 return "Incorrect result returned, expected " + to_string(SUM_RESULT);
45 }
46 return std::string();
47}
48
49virtual std::string BenchmarkInfo() {
50 return StringUtil::Format("Runs the following query: \"" + GetQuery() + "\" without an index");
51}
52FINISH_BENCHMARK(RangeQueryWithoutIndex)
53
54DUCKDB_BENCHMARK(RangeQueryWithIndexART, "[micro]")
55virtual void Load(DuckDBBenchmarkState *state) {
56 state->conn.Query("CREATE TABLE integers(i INTEGER, j INTEGER);");
57 Appender appender(state->conn, "integers"); // insert the elements into the database
58 for (size_t i = 0; i < RANGE_QUERY_ROW_COUNT; i++) {
59 appender.BeginRow();
60 appender.Append<int32_t>(i);
61 appender.Append<int32_t>(i);
62 appender.EndRow();
63 }
64 state->conn.Query("CREATE INDEX i_index ON integers using art(i)");
65}
66
67virtual std::string GetQuery() {
68 return "SELECT sum(j) FROM integers WHERE i >= " + to_string(RANGE_QUERY_ENTRY_LOW) +
69 " and i <= " + to_string(RANGE_QUERY_ENTRY_HIGH);
70}
71
72virtual std::string VerifyResult(QueryResult *result) {
73 if (!result->success) {
74 return result->error;
75 }
76 auto &materialized = (MaterializedQueryResult &)*result;
77 if (materialized.collection.count != 1) {
78 return "Incorrect amount of rows in result";
79 }
80 if (result->names.size() != 1) {
81 return "Incorrect amount of columns";
82 }
83 if (materialized.GetValue<int64_t>(0, 0) != SUM_RESULT) {
84 return "Incorrect result returned, expected " + to_string(SUM_RESULT);
85 }
86 return std::string();
87}
88
89virtual std::string BenchmarkInfo() {
90 return StringUtil::Format("Runs the following query: \"" + GetQuery() + "\" with an index");
91}
92FINISH_BENCHMARK(RangeQueryWithIndexART)
93