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 CAST_COUNT 10000000 |
11 | |
12 | DUCKDB_BENCHMARK(CastIntToString, "[cast]" ) |
13 | void Load(DuckDBBenchmarkState *state) override { |
14 | state->conn.Query("CREATE TABLE integers(i INTEGER);" ); |
15 | Appender appender(state->conn, "integers" ); |
16 | // insert the elements into the database |
17 | for (int i = 0; i < CAST_COUNT; i++) { |
18 | appender.AppendRow(i); |
19 | } |
20 | } |
21 | string GetQuery() override { |
22 | return "SELECT CAST(i AS VARCHAR) FROM integers" ; |
23 | } |
24 | string VerifyResult(QueryResult *result) override { |
25 | if (!result->success) { |
26 | return result->error; |
27 | } |
28 | return string(); |
29 | } |
30 | string BenchmarkInfo() override { |
31 | return "Cast integer to string" ; |
32 | } |
33 | FINISH_BENCHMARK(CastIntToString) |
34 | |
35 | DUCKDB_BENCHMARK(FormatIntToString, "[cast]" ) |
36 | void Load(DuckDBBenchmarkState *state) override { |
37 | state->conn.Query("CREATE TABLE integers(i INTEGER);" ); |
38 | Appender appender(state->conn, "integers" ); |
39 | // insert the elements into the database |
40 | for (int i = 0; i < CAST_COUNT; i++) { |
41 | appender.AppendRow(i); |
42 | } |
43 | } |
44 | string GetQuery() override { |
45 | return "SELECT format('{}', i) FROM integers" ; |
46 | } |
47 | string VerifyResult(QueryResult *result) override { |
48 | if (!result->success) { |
49 | return result->error; |
50 | } |
51 | return string(); |
52 | } |
53 | string BenchmarkInfo() override { |
54 | return "Convert integer to string using format function" ; |
55 | } |
56 | FINISH_BENCHMARK(FormatIntToString) |
57 | |
58 | DUCKDB_BENCHMARK(CastDoubleToString, "[cast]" ) |
59 | void Load(DuckDBBenchmarkState *state) override { |
60 | state->conn.Query("CREATE TABLE doubles(i DOUBLE);" ); |
61 | Appender appender(state->conn, "doubles" ); |
62 | // insert the elements into the database |
63 | for (double i = 0; i < CAST_COUNT; i += 0.8) { |
64 | appender.AppendRow(i); |
65 | } |
66 | } |
67 | string GetQuery() override { |
68 | return "SELECT CAST(i AS VARCHAR) FROM doubles" ; |
69 | } |
70 | string VerifyResult(QueryResult *result) override { |
71 | if (!result->success) { |
72 | return result->error; |
73 | } |
74 | return string(); |
75 | } |
76 | string BenchmarkInfo() override { |
77 | return "Cast double to string" ; |
78 | } |
79 | FINISH_BENCHMARK(CastDoubleToString) |
80 | |
81 | DUCKDB_BENCHMARK(CastStringToInt, "[cast]" ) |
82 | void Load(DuckDBBenchmarkState *state) override { |
83 | state->conn.Query("CREATE TABLE varchars(i VARCHAR);" ); |
84 | Appender appender(state->conn, "varchars" ); |
85 | // insert the elements into the database |
86 | for (int i = 0; i < CAST_COUNT; i++) { |
87 | appender.AppendRow(Value(to_string(i))); |
88 | } |
89 | } |
90 | string GetQuery() override { |
91 | return "SELECT CAST(i AS INTEGER) FROM varchars" ; |
92 | } |
93 | string VerifyResult(QueryResult *result) override { |
94 | if (!result->success) { |
95 | return result->error; |
96 | } |
97 | return string(); |
98 | } |
99 | string BenchmarkInfo() override { |
100 | return "Cast int to string" ; |
101 | } |
102 | FINISH_BENCHMARK(CastStringToInt) |
103 | |
104 | DUCKDB_BENCHMARK(CastStringToDouble, "[cast]" ) |
105 | void Load(DuckDBBenchmarkState *state) override { |
106 | state->conn.Query("CREATE TABLE varchars(i VARCHAR);" ); |
107 | Appender appender(state->conn, "varchars" ); |
108 | // insert the elements into the database |
109 | for (int i = 0; i < CAST_COUNT; i++) { |
110 | appender.AppendRow(Value(to_string(i))); |
111 | } |
112 | } |
113 | string GetQuery() override { |
114 | return "SELECT CAST(i AS DOUBLE) FROM varchars" ; |
115 | } |
116 | string VerifyResult(QueryResult *result) override { |
117 | if (!result->success) { |
118 | return result->error; |
119 | } |
120 | return string(); |
121 | } |
122 | string BenchmarkInfo() override { |
123 | return "Cast double to string" ; |
124 | } |
125 | FINISH_BENCHMARK(CastStringToDouble) |
126 | |
127 | DUCKDB_BENCHMARK(CastDateToString, "[cast]" ) |
128 | void Load(DuckDBBenchmarkState *state) override { |
129 | std::uniform_int_distribution<> year_dist(1990, 2010), day_dist(1, 28), month_dist(1, 12); |
130 | std::mt19937 gen; |
131 | gen.seed(42); |
132 | |
133 | state->conn.Query("CREATE TABLE dates(d DATE);" ); |
134 | Appender appender(state->conn, "dates" ); |
135 | // insert the elements into the database |
136 | for (int i = 0; i < CAST_COUNT; i++) { |
137 | appender.AppendRow(Value::DATE(year_dist(gen), month_dist(gen), day_dist(gen))); |
138 | } |
139 | } |
140 | string GetQuery() override { |
141 | return "SELECT CAST(d AS VARCHAR) FROM dates" ; |
142 | } |
143 | string VerifyResult(QueryResult *result) override { |
144 | if (!result->success) { |
145 | return result->error; |
146 | } |
147 | return string(); |
148 | } |
149 | string BenchmarkInfo() override { |
150 | return "Cast date to string" ; |
151 | } |
152 | FINISH_BENCHMARK(CastDateToString) |
153 | |
154 | DUCKDB_BENCHMARK(CastTimeToString, "[cast]" ) |
155 | void Load(DuckDBBenchmarkState *state) override { |
156 | std::uniform_int_distribution<> hour_dist(0, 23), min_dist(0, 59); |
157 | std::mt19937 gen; |
158 | gen.seed(42); |
159 | |
160 | state->conn.Query("CREATE TABLE times(d TIME);" ); |
161 | Appender appender(state->conn, "times" ); |
162 | // insert the elements into the database |
163 | for (int i = 0; i < CAST_COUNT; i++) { |
164 | appender.AppendRow(Value::TIME(hour_dist(gen), min_dist(gen), min_dist(gen), 0)); |
165 | } |
166 | } |
167 | string GetQuery() override { |
168 | return "SELECT CAST(d AS VARCHAR) FROM times" ; |
169 | } |
170 | string VerifyResult(QueryResult *result) override { |
171 | if (!result->success) { |
172 | return result->error; |
173 | } |
174 | return string(); |
175 | } |
176 | string BenchmarkInfo() override { |
177 | return "Cast time to string" ; |
178 | } |
179 | FINISH_BENCHMARK(CastTimeToString) |
180 | |
181 | DUCKDB_BENCHMARK(CastTimestampToString, "[cast]" ) |
182 | void Load(DuckDBBenchmarkState *state) override { |
183 | std::uniform_int_distribution<> year_dist(1990, 2010), day_dist(1, 28), month_dist(1, 12); |
184 | std::uniform_int_distribution<> hour_dist(0, 23), min_dist(0, 59); |
185 | std::mt19937 gen; |
186 | gen.seed(42); |
187 | |
188 | state->conn.Query("CREATE TABLE timestamps(d TIMESTAMP);" ); |
189 | Appender appender(state->conn, "timestamps" ); |
190 | // insert the elements into the database |
191 | for (int i = 0; i < CAST_COUNT; i++) { |
192 | appender.AppendRow(Value::TIMESTAMP(year_dist(gen), month_dist(gen), day_dist(gen), hour_dist(gen), |
193 | min_dist(gen), min_dist(gen), 0)); |
194 | } |
195 | } |
196 | string GetQuery() override { |
197 | return "SELECT CAST(d AS VARCHAR) FROM timestamps" ; |
198 | } |
199 | string VerifyResult(QueryResult *result) override { |
200 | if (!result->success) { |
201 | return result->error; |
202 | } |
203 | return string(); |
204 | } |
205 | string BenchmarkInfo() override { |
206 | return "Cast timestamp to string" ; |
207 | } |
208 | FINISH_BENCHMARK(CastTimestampToString) |
209 | |