1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
#include "capi_tester.hpp"
using namespace duckdb;
using namespace std;
string BuildSettingsString(const duckdb::vector<string> &settings) {
string result = "'{";
for (idx_t i = 0; i < settings.size(); i++) {
result += "\"" + settings[i] + "\": \"true\"";
if (i < settings.size() - 1) {
result += ", ";
}
}
result += "}'";
return result;
}
void RetrieveMetrics(duckdb_profiling_info info, duckdb::map<string, double> &cumulative_counter,
duckdb::map<string, double> &cumulative_result, const idx_t depth) {
auto map = duckdb_profiling_info_get_metrics(info);
REQUIRE(map);
auto count = duckdb_get_map_size(map);
REQUIRE(count != 0);
// Test index out of bounds for MAP value.
if (depth == 0) {
auto invalid_key = duckdb_get_map_key(map, 10000000);
REQUIRE(!invalid_key);
auto invalid_value = duckdb_get_map_value(map, 10000000);
REQUIRE(!invalid_value);
}
for (idx_t i = 0; i < count; i++) {
auto key = duckdb_get_map_key(map, i);
REQUIRE(key);
auto value = duckdb_get_map_value(map, i);
REQUIRE(value);
auto key_c_str = duckdb_get_varchar(key);
auto value_c_str = duckdb_get_varchar(value);
auto key_str = duckdb::string(key_c_str);
auto value_str = duckdb::string(value_c_str);
if (depth == 0) {
REQUIRE(key_str != EnumUtil::ToString(MetricType::OPERATOR_CARDINALITY));
REQUIRE(key_str != EnumUtil::ToString(MetricType::OPERATOR_ROWS_SCANNED));
REQUIRE(key_str != EnumUtil::ToString(MetricType::OPERATOR_TIMING));
REQUIRE(key_str != EnumUtil::ToString(MetricType::OPERATOR_NAME));
REQUIRE(key_str != EnumUtil::ToString(MetricType::OPERATOR_TYPE));
} else {
REQUIRE(key_str != EnumUtil::ToString(MetricType::QUERY_NAME));
REQUIRE(key_str != EnumUtil::ToString(MetricType::BLOCKED_THREAD_TIME));
REQUIRE(key_str != EnumUtil::ToString(MetricType::LATENCY));
REQUIRE(key_str != EnumUtil::ToString(MetricType::ROWS_RETURNED));
}
if (key_str == EnumUtil::ToString(MetricType::QUERY_NAME) ||
key_str == EnumUtil::ToString(MetricType::OPERATOR_NAME) ||
key_str == EnumUtil::ToString(MetricType::OPERATOR_TYPE) ||
key_str == EnumUtil::ToString(MetricType::EXTRA_INFO)) {
REQUIRE(!value_str.empty());
} else {
double result = 0;
try {
result = std::stod(value_str);
} catch (std::invalid_argument &e) {
REQUIRE(false);
}
if (cumulative_counter.find(key_str) != cumulative_counter.end()) {
cumulative_counter[key_str] += result;
}
if (cumulative_result.find(key_str) != cumulative_result.end() && cumulative_result[key_str] == 0) {
cumulative_result[key_str] = result;
}
}
duckdb_destroy_value(&key);
duckdb_destroy_value(&value);
duckdb_free(key_c_str);
duckdb_free(value_c_str);
}
duckdb_destroy_value(&map);
}
void TraverseTree(duckdb_profiling_info profiling_info, duckdb::map<string, double> &cumulative_counter,
duckdb::map<string, double> &cumulative_result, const idx_t depth) {
RetrieveMetrics(profiling_info, cumulative_counter, cumulative_result, depth);
// Recurse into the child node.
auto child_count = duckdb_profiling_info_get_child_count(profiling_info);
if (depth == 0) {
REQUIRE(child_count != 0);
}
for (idx_t i = 0; i < child_count; i++) {
auto child = duckdb_profiling_info_get_child(profiling_info, i);
TraverseTree(child, cumulative_counter, cumulative_result, depth + 1);
}
}
idx_t ConvertToInt(double value) {
return idx_t(value * 1000);
}
TEST_CASE("Test profiling with a single metric and get_value", "[capi]") {
CAPITester tester;
duckdb::unique_ptr<CAPIResult> result;
REQUIRE(tester.OpenDatabase(nullptr));
REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
// test only CPU_TIME profiling
duckdb::vector<string> settings = {"CPU_TIME"};
REQUIRE_NO_FAIL(tester.Query("PRAGMA custom_profiling_settings=" + BuildSettingsString(settings)));
REQUIRE_NO_FAIL(tester.Query("SELECT 42"));
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info != nullptr);
// Retrieve a metric that is not enabled.
REQUIRE(duckdb_profiling_info_get_value(info, "EXTRA_INFO") == nullptr);
duckdb::map<string, double> cumulative_counter;
duckdb::map<string, double> cumulative_result;
TraverseTree(info, cumulative_counter, cumulative_result, 0);
tester.Cleanup();
}
TEST_CASE("Test profiling with cumulative metrics", "[capi]") {
CAPITester tester;
duckdb::unique_ptr<CAPIResult> result;
REQUIRE(tester.OpenDatabase(nullptr));
REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
// test all profiling metrics
duckdb::vector<string> settings = {"BLOCKED_THREAD_TIME", "CPU_TIME", "CUMULATIVE_CARDINALITY", "EXTRA_INFO",
"OPERATOR_CARDINALITY", "OPERATOR_TIMING"};
REQUIRE_NO_FAIL(tester.Query("PRAGMA custom_profiling_settings=" + BuildSettingsString(settings)));
REQUIRE_NO_FAIL(tester.Query("SELECT 42"));
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info != nullptr);
duckdb::map<string, double> cumulative_counter = {{"OPERATOR_TIMING", 0}, {"OPERATOR_CARDINALITY", 0}};
duckdb::map<string, double> cumulative_result {
{"CPU_TIME", 0},
{"CUMULATIVE_CARDINALITY", 0},
};
TraverseTree(info, cumulative_counter, cumulative_result, 0);
REQUIRE(ConvertToInt(cumulative_result["CPU_TIME"]) == ConvertToInt(cumulative_counter["OPERATOR_TIMING"]));
REQUIRE(ConvertToInt(cumulative_result["CUMULATIVE_CARDINALITY"]) ==
ConvertToInt(cumulative_counter["OPERATOR_CARDINALITY"]));
tester.Cleanup();
}
TEST_CASE("Test profiling without profiling enabled", "[capi]") {
CAPITester tester;
duckdb::unique_ptr<CAPIResult> result;
REQUIRE(tester.OpenDatabase(nullptr));
// Retrieve info without profiling enabled.
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info == nullptr);
tester.Cleanup();
}
TEST_CASE("Test profiling with detailed profiling mode enabled", "[capi]") {
CAPITester tester;
duckdb::unique_ptr<CAPIResult> result;
REQUIRE(tester.OpenDatabase(nullptr));
REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
REQUIRE_NO_FAIL(tester.Query("PRAGMA profiling_mode = 'detailed'"));
REQUIRE_NO_FAIL(tester.Query("SELECT 42"));
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info != nullptr);
duckdb::map<string, double> cumulative_counter;
duckdb::map<string, double> cumulative_result;
TraverseTree(info, cumulative_counter, cumulative_result, 0);
tester.Cleanup();
}
TEST_CASE("Test invalid use of profiling API", "[capi]") {
CAPITester tester;
duckdb::unique_ptr<CAPIResult> result;
REQUIRE(tester.OpenDatabase(nullptr));
REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
REQUIRE_NO_FAIL(tester.Query("PRAGMA profiling_mode = 'detailed'"));
REQUIRE_NO_FAIL(tester.Query("SELECT 42"));
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info != nullptr);
// Incorrect usage tests.
auto map = duckdb_profiling_info_get_metrics(nullptr);
REQUIRE(map == nullptr);
map = duckdb_profiling_info_get_metrics(info);
auto dummy_value = duckdb_create_bool(true);
auto count = duckdb_get_map_size(nullptr);
REQUIRE(count == 0);
count = duckdb_get_map_size(dummy_value);
REQUIRE(count == 0);
count = duckdb_get_map_size(map);
for (idx_t i = 0; i < count; i++) {
auto key = duckdb_get_map_key(nullptr, i);
REQUIRE(key == nullptr);
key = duckdb_get_map_key(map, DConstants::INVALID_INDEX);
REQUIRE(key == nullptr);
key = duckdb_get_map_key(dummy_value, i);
REQUIRE(key == nullptr);
auto value = duckdb_get_map_value(nullptr, i);
REQUIRE(value == nullptr);
value = duckdb_get_map_value(map, DConstants::INVALID_INDEX);
REQUIRE(value == nullptr);
value = duckdb_get_map_value(dummy_value, i);
REQUIRE(value == nullptr);
break;
}
duckdb_destroy_value(&dummy_value);
duckdb_destroy_value(&map);
tester.Cleanup();
}
TEST_CASE("Test profiling after throwing an error", "[capi]") {
CAPITester tester;
auto main_db = TestCreatePath("profiling_error.db");
REQUIRE(tester.OpenDatabase(main_db.c_str()));
auto path = TestCreatePath("profiling_error.db");
REQUIRE_NO_FAIL(tester.Query("ATTACH IF NOT EXISTS '" + path + "' (TYPE DUCKDB)"));
REQUIRE_NO_FAIL(tester.Query("CREATE TABLE profiling_error.tbl AS SELECT range AS id FROM range(10)"));
REQUIRE_NO_FAIL(tester.Query("SET enable_profiling = 'no_output'"));
REQUIRE_NO_FAIL(tester.Query("SET profiling_mode = 'standard'"));
CAPIPrepared prepared_q1;
CAPIPending pending_q1;
REQUIRE(prepared_q1.Prepare(tester, "SELECT * FROM profiling_error.tbl"));
REQUIRE(pending_q1.Pending(prepared_q1));
auto result = pending_q1.Execute();
REQUIRE(result);
REQUIRE(!result->HasError());
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info != nullptr);
CAPIPrepared prepared_q2;
REQUIRE(!prepared_q2.Prepare(tester, "SELECT * FROM profiling_error.does_not_exist"));
info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info == nullptr);
tester.Cleanup();
}
TEST_CASE("Test profiling with Extra Info enabled", "[capi]") {
CAPITester tester;
REQUIRE(tester.OpenDatabase(nullptr));
REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
duckdb::vector<string> settings = {"EXTRA_INFO"};
REQUIRE_NO_FAIL(tester.Query("PRAGMA custom_profiling_settings=" + BuildSettingsString(settings)));
REQUIRE_NO_FAIL(tester.Query("SELECT 1"));
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info);
// Retrieve the child node.
auto child_info = duckdb_profiling_info_get_child(info, 0);
REQUIRE(duckdb_profiling_info_get_child_count(child_info) != 0);
auto map = duckdb_profiling_info_get_metrics(child_info);
REQUIRE(map);
auto count = duckdb_get_map_size(map);
REQUIRE(count != 0);
bool found_extra_info = false;
for (idx_t i = 0; i < count; i++) {
auto key = duckdb_get_map_key(map, i);
REQUIRE(key);
auto key_c_str = duckdb_get_varchar(key);
auto key_str = duckdb::string(key_c_str);
auto value = duckdb_get_map_value(map, i);
REQUIRE(value);
auto value_c_str = duckdb_get_varchar(value);
auto value_str = duckdb::string(value_c_str);
if (key_str == EnumUtil::ToString(MetricType::EXTRA_INFO)) {
REQUIRE(value_str.find("__order_by__"));
REQUIRE(value_str.find("ASC"));
found_extra_info = true;
}
if (key) {
duckdb_destroy_value(&key);
duckdb_free(key_c_str);
}
if (value) {
duckdb_destroy_value(&value);
duckdb_free(value_c_str);
}
}
REQUIRE(found_extra_info);
duckdb_destroy_value(&map);
tester.Cleanup();
}
TEST_CASE("Test profiling with the appender", "[capi]") {
CAPITester tester;
duckdb::unique_ptr<CAPIResult> result;
REQUIRE(tester.OpenDatabase(nullptr));
tester.Query("CREATE TABLE tbl (i INT PRIMARY KEY, value VARCHAR)");
REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
REQUIRE_NO_FAIL(tester.Query("SET profiling_coverage='ALL'"));
duckdb_appender appender;
string query = "INSERT INTO tbl FROM my_appended_data";
duckdb_logical_type types[2];
types[0] = duckdb_create_logical_type(DUCKDB_TYPE_INTEGER);
types[1] = duckdb_create_logical_type(DUCKDB_TYPE_VARCHAR);
auto status = duckdb_appender_create_query(tester.connection, query.c_str(), 2, types, "my_appended_data", nullptr,
&appender);
duckdb_destroy_logical_type(&types[0]);
duckdb_destroy_logical_type(&types[1]);
REQUIRE(status == DuckDBSuccess);
REQUIRE(duckdb_appender_error(appender) == nullptr);
REQUIRE(duckdb_appender_begin_row(appender) == DuckDBSuccess);
REQUIRE(duckdb_append_int32(appender, 1) == DuckDBSuccess);
REQUIRE(duckdb_append_varchar(appender, "hello world") == DuckDBSuccess);
REQUIRE(duckdb_appender_end_row(appender) == DuckDBSuccess);
REQUIRE(duckdb_appender_flush(appender) == DuckDBSuccess);
REQUIRE(duckdb_appender_close(appender) == DuckDBSuccess);
REQUIRE(duckdb_appender_destroy(&appender) == DuckDBSuccess);
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info);
// Check that the query name matches the appender query.
auto query_name = duckdb_profiling_info_get_value(info, "QUERY_NAME");
REQUIRE(query_name);
auto query_name_c_str = duckdb_get_varchar(query_name);
auto query_name_str = duckdb::string(query_name_c_str);
REQUIRE(query_name_str == query);
duckdb_destroy_value(&query_name);
duckdb_free(query_name_c_str);
duckdb::map<string, double> cumulative_counter;
duckdb::map<string, double> cumulative_result;
TraverseTree(info, cumulative_counter, cumulative_result, 0);
tester.Cleanup();
}
TEST_CASE("Test profiling with the non-query appender", "[capi]") {
CAPITester tester;
duckdb::unique_ptr<CAPIResult> result;
REQUIRE(tester.OpenDatabase(nullptr));
tester.Query("CREATE TABLE test (i INTEGER)");
REQUIRE_NO_FAIL(tester.Query("PRAGMA enable_profiling = 'no_output'"));
REQUIRE_NO_FAIL(tester.Query("SET profiling_coverage='ALL'"));
duckdb_appender appender;
REQUIRE(duckdb_appender_create(tester.connection, nullptr, "test", &appender) == DuckDBSuccess);
REQUIRE(duckdb_appender_error(appender) == nullptr);
// Appending a row.
REQUIRE(duckdb_appender_begin_row(appender) == DuckDBSuccess);
REQUIRE(duckdb_append_int32(appender, 42) == DuckDBSuccess);
// Finish and flush.
REQUIRE(duckdb_appender_end_row(appender) == DuckDBSuccess);
REQUIRE(duckdb_appender_flush(appender) == DuckDBSuccess);
REQUIRE(duckdb_appender_close(appender) == DuckDBSuccess);
REQUIRE(duckdb_appender_destroy(&appender) == DuckDBSuccess);
auto info = duckdb_get_profiling_info(tester.connection);
REQUIRE(info);
// Check that the query name matches the appender query.
auto query_name = duckdb_profiling_info_get_value(info, "QUERY_NAME");
REQUIRE(query_name);
auto query_name_c_str = duckdb_get_varchar(query_name);
auto query_name_str = duckdb::string(query_name_c_str);
auto query = "INSERT INTO main.test FROM __duckdb_internal_appended_data";
REQUIRE(query_name_str == query);
duckdb_destroy_value(&query_name);
duckdb_free(query_name_c_str);
duckdb::map<string, double> cumulative_counter;
duckdb::map<string, double> cumulative_result;
TraverseTree(info, cumulative_counter, cumulative_result, 0);
tester.Cleanup();
}
|