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 GROUP_ROW_COUNT 10000000
11#define GROUP_COUNT 5
12
13DUCKDB_BENCHMARK(SimpleAggregate, "[aggregate]")
14virtual void Load(DuckDBBenchmarkState *state) {
15 state->conn.Query("CREATE TABLE integers(i INTEGER);");
16 Appender appender(state->conn, "integers"); // insert the elements into the database
17 for (size_t i = 0; i < GROUP_ROW_COUNT; i++) {
18 appender.BeginRow();
19 appender.Append<int32_t>(i % GROUP_COUNT);
20 appender.EndRow();
21 }
22}
23
24virtual string GetQuery() {
25 return "SELECT SUM(i) FROM integers";
26}
27
28virtual string VerifyResult(QueryResult *result) {
29 if (!result->success) {
30 return result->error;
31 }
32 auto &materialized = (MaterializedQueryResult &)*result;
33 if (materialized.collection.count != 1) {
34 return "Incorrect amount of rows in result";
35 }
36 return string();
37}
38
39virtual string BenchmarkInfo() {
40 return StringUtil::Format("Runs the following query: \"SELECT SUM(i) "
41 "FROM integers\""
42 " on %d rows",
43 GROUP_ROW_COUNT);
44}
45FINISH_BENCHMARK(SimpleAggregate)
46
47DUCKDB_BENCHMARK(SimpleGroupByAggregate, "[aggregate]")
48virtual void Load(DuckDBBenchmarkState *state) {
49 // fixed seed random numbers
50 std::uniform_int_distribution<> distribution(1, 10000);
51 std::mt19937 gen;
52 gen.seed(42);
53
54 state->conn.Query("CREATE TABLE integers(i INTEGER, j INTEGER);");
55 Appender appender(state->conn, "integers"); // insert the elements into the database
56 for (size_t i = 0; i < GROUP_ROW_COUNT; i++) {
57 appender.BeginRow();
58 appender.Append<int32_t>(i % GROUP_COUNT);
59 appender.Append<int32_t>(distribution(gen));
60 appender.EndRow();
61 }
62}
63
64virtual string GetQuery() {
65 return "SELECT i, SUM(j) FROM integers GROUP BY i";
66}
67
68virtual string VerifyResult(QueryResult *result) {
69 if (!result->success) {
70 return result->error;
71 }
72 auto &materialized = (MaterializedQueryResult &)*result;
73 if (materialized.collection.count != GROUP_COUNT) {
74 return "Incorrect amount of rows in result";
75 }
76 return string();
77}
78
79virtual string BenchmarkInfo() {
80 return StringUtil::Format("Runs the following query: \"SELECT i, SUM(j) "
81 "FROM integers GROUP BY i\""
82 " on %d rows with %d unique groups",
83 GROUP_ROW_COUNT, GROUP_COUNT);
84}
85FINISH_BENCHMARK(SimpleGroupByAggregate)
86