From d9a3e3526ec4c1b985a11a8452858b23acb97928 Mon Sep 17 00:00:00 2001 From: Wang Zhiyong Date: Wed, 27 Nov 2024 17:34:41 +0800 Subject: [PATCH] Add time print for lgraph_cli (#787) --- src/bolt/to_string.h | 5 +++-- test/test_lgraph_cli.cpp | 2 +- toolkits/lgraph_cli.cpp | 25 +++++++++++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/bolt/to_string.h b/src/bolt/to_string.h index adab89e1ae..6ca490f360 100644 --- a/src/bolt/to_string.h +++ b/src/bolt/to_string.h @@ -132,8 +132,9 @@ nlohmann::json ToJsonObj(const bolt::Path& path) { ret.append(forward ? "-" : "<-"); ret.append(ToJsonObj(rel).get()); ret.append(forward ? "->" : "-"); - ret.append(ToJsonObj(path.nodes[nodes_index.at(rel.endId)]).get()); - nodeId = rel.endId; + ret.append(ToJsonObj(path.nodes[nodes_index.at( + forward ? rel.endId : rel.startId)]).get()); + nodeId = forward ? rel.endId : rel.startId; } return ret; } diff --git a/test/test_lgraph_cli.cpp b/test/test_lgraph_cli.cpp index 60ed9a1f6d..e1f9b1c919 100644 --- a/test/test_lgraph_cli.cpp +++ b/test/test_lgraph_cli.cpp @@ -274,7 +274,7 @@ p WriteFile(file, statements); std::string lgraph_cli = "./lgraph_cli --ip 127.0.0.1 --port 7687 --graph default " - "--user admin --password 73@TuGraph"; + "--user admin --password 73@TuGraph --print_time false"; lgraph::SubProcess cli(FMA_FMT("{} < {}", lgraph_cli, file)); cli.Wait(); UT_EXPECT_EQ(cli.Stdout(), expected); diff --git a/toolkits/lgraph_cli.cpp b/toolkits/lgraph_cli.cpp index e4a80f263c..070dd53d92 100644 --- a/toolkits/lgraph_cli.cpp +++ b/toolkits/lgraph_cli.cpp @@ -60,7 +60,9 @@ std::any ReadMessage(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator) { return ret.first; } -bool FetchRecords(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator, OutputFormat of) { +bool FetchRecords(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator, + OutputFormat of, bool print_time) { + auto start = std::chrono::high_resolution_clock::now(); std::string error; std::optional> header; tabulate::Table table; @@ -122,12 +124,24 @@ bool FetchRecords(asio::ip::tcp::socket& socket, bolt::Hydrator& hydrator, Outpu } if (error.empty()) { + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = end - start; if (of == OutputFormat::TABLE) { if (table.size() > 0) { LOG_INFO() << table << "\n"; - LOG_INFO() << FMA_FMT("{} rows", table.size() - 1) << "\n"; + if (print_time) { + LOG_INFO() << FMA_FMT("{} rows ({} ms)", table.size() - 1, + static_cast(elapsed.count())) << "\n"; + } else { + LOG_INFO() << FMA_FMT("{} rows", table.size() - 1) << "\n"; + } } else { - LOG_INFO() << FMA_FMT("{} rows", table.size()) << "\n"; + if (print_time) { + LOG_INFO() << FMA_FMT("{} rows ({} ms)", table.size(), + static_cast(elapsed.count())) << "\n"; + } else { + LOG_INFO() << FMA_FMT("{} rows", table.size()) << "\n"; + } } } return true; @@ -184,6 +198,7 @@ int main(int argc, char** argv) { std::string graph = "default"; std::string username = "admin"; std::string password = "73@TuGraph"; + bool print_time = true; config.Add(format, "format", true). Comment("output format (table, csv, json)"). SetPossibleValues({"table", "csv", "json"}); @@ -192,6 +207,8 @@ int main(int argc, char** argv) { config.Add(graph, "graph", true).Comment("Graph to use"); config.Add(username, "user", true).Comment("User to login"); config.Add(password, "password", true).Comment("Password to use when connecting to server"); + config.Add(print_time, "print_time", true).Comment( + "Whether to print the execution time of the query"); try { config.ExitAfterHelp(true); config.ParseAndFinalize(argc, argv); @@ -282,7 +299,7 @@ int main(int argc, char** argv) { ps.AppendPullN(-1); asio::write(socket, asio::const_buffer(ps.ConstBuffer().data(), ps.ConstBuffer().size())); - bool ret = FetchRecords(socket, hydrator, of); + bool ret = FetchRecords(socket, hydrator, of, print_time); if (!ret) { // reset connection ps.Reset();