Skip to content

Commit 7411071

Browse files
jmatakgitbuda
andauthored
Add verbose query execution info (#36)
Co-authored-by: Marko Budiselic <marko.budiselic@memgraph.com>
1 parent 8045fd8 commit 7411071

4 files changed

Lines changed: 42 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
cmake_minimum_required(VERSION 3.4)
18-
project(mgconsole VERSION 1.2)
18+
project(mgconsole VERSION 1.3)
1919
include(CTest)
2020

2121
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)

src/main.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
#include <signal.h>
18+
1719
#include <algorithm>
1820
#include <cstdio>
1921
#include <iostream>
2022
#include <optional>
2123
#include <thread>
2224

23-
#include <signal.h>
24-
2525
#ifdef _WIN32
2626

2727
#include <cstdlib>
@@ -58,6 +58,8 @@ DEFINE_bool(term_colors, false, "Use terminal colors syntax highlighting.");
5858
DEFINE_string(output_format, "tabular",
5959
"Query output format. Can be csv/tabular. If output format is "
6060
"other than tabular `fit-to-screen` flag is ignored.");
61+
DEFINE_bool(verbose_execution_info, false,
62+
"Output the additional information about query such is query cost, parsing, planning and execution times.");
6163
DEFINE_validator(output_format, [](const char *, const std::string &value) {
6264
if (value == constants::kCsvFormat || value == constants::kTabularFormat) {
6365
return true;
@@ -252,13 +254,11 @@ int main(int argc, char **argv) {
252254
if (query->empty()) {
253255
continue;
254256
}
255-
256257
try {
257258
auto ret = query::ExecuteQuery(session.get(), *query);
258259
if (ret.records.size() > 0) {
259260
Output(ret.header, ret.records, output_opts, csv_opts);
260261
}
261-
262262
if (console::is_a_tty(STDIN_FILENO)) {
263263
std::string summary;
264264
if (ret.records.size() == 0) {
@@ -268,7 +268,7 @@ int main(int argc, char **argv) {
268268
} else {
269269
summary = std::to_string(ret.records.size()) + " rows in set";
270270
}
271-
std::printf("%s (%.3lf sec)\n", summary.c_str(), ret.wall_time.count());
271+
std::printf("%s (round trip in %.3lf sec)\n", summary.c_str(), ret.wall_time.count());
272272
auto history_ret = save_history();
273273
if (history_ret != 0) {
274274
cleanup_resources();
@@ -280,6 +280,9 @@ int main(int argc, char **argv) {
280280
if (ret.stats) {
281281
console::EchoStats(ret.stats.value());
282282
}
283+
if (FLAGS_verbose_execution_info && ret.execution_info) {
284+
console::EchoExecutionInfo(ret.execution_info.value());
285+
}
283286
}
284287
} catch (const utils::ClientQueryException &e) {
285288
if (!console::is_a_tty(STDIN_FILENO)) {

src/utils/utils.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <string.h>
2+
23
#include <algorithm>
34
#include <chrono>
45
#include <cmath>
@@ -504,6 +505,8 @@ std::map<std::string, std::string> ParseNotifications(const mg_value *mg_notific
504505
return notifications;
505506
}
506507

508+
double ParseFloat(const mg_value *mg_val_float) { return mg_value_float(mg_val_float); }
509+
507510
} // namespace
508511

509512
namespace console {
@@ -567,6 +570,21 @@ void EchoInfo(const std::string &message) {
567570
}
568571
}
569572

573+
void EchoExecutionInfo(const std::map<std::string, double> &execution_info) {
574+
std::cout << "Additional execution time info:" << std::endl;
575+
for (const auto &[key, value] : execution_info) {
576+
if (key == "cost_estimate") {
577+
std::cout << " Query COST estimate: " << value << std::endl;
578+
} else if (key == "parsing_time") {
579+
std::cout << " Query PARSING time: " << value << " sec" << std::endl;
580+
} else if (key == "planning_time") {
581+
std::cout << " Query PLANNING time: " << value << " sec" << std::endl;
582+
} else if (key == "plan_execution_time") {
583+
std::cout << " Query PLAN EXECUTION time: " << value << " sec" << std::endl;
584+
}
585+
}
586+
}
587+
570588
void EchoStats(const std::map<std::string, std::int64_t> &stats) {
571589
for (const auto &[key, value] : stats) {
572590
if (value == 0) {
@@ -814,6 +832,18 @@ QueryData ExecuteQuery(mg_session *session, const std::string &query) {
814832

815833
const mg_map *summary = mg_result_summary(result);
816834
if (summary && mg_map_size(summary) > 0) {
835+
{
836+
std::map<std::string, double> execution_info;
837+
for (auto key : {"cost_estimate", "parsing_time", "planning_time", "plan_execution_time"}) {
838+
if (const mg_value *info = mg_map_at(summary, key); info) {
839+
execution_info.emplace(key, ParseFloat(info));
840+
}
841+
}
842+
if (!execution_info.empty()) {
843+
ret.execution_info = execution_info;
844+
}
845+
}
846+
817847
if (const mg_value *mg_stats = mg_map_at(summary, "stats"); mg_stats) {
818848
ret.stats.emplace(ParseStats(mg_stats));
819849
}

src/utils/utils.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ void EchoStats(const std::map<std::string, std::int64_t> &stats);
178178

179179
void EchoNotification(const std::map<std::string, std::string> &notification);
180180

181+
void EchoExecutionInfo(const std::map<std::string, double> &execution_info);
182+
181183
/// Helper function that sets default input for 'readline'
182184
int SetDefaultText();
183185

@@ -210,6 +212,7 @@ struct QueryData {
210212
std::chrono::duration<double> wall_time;
211213
std::optional<std::map<std::string, std::string>> notification;
212214
std::optional<std::map<std::string, std::int64_t>> stats;
215+
std::optional<std::map<std::string, double>> execution_info;
213216
};
214217

215218
std::optional<std::string> GetQuery(Replxx *replxx_instance);

0 commit comments

Comments
 (0)