From 3095cbd956c61d9e9fb5d8e6e02169329b4925cf Mon Sep 17 00:00:00 2001 From: Marko Budiselic Date: Tue, 21 Feb 2023 22:40:07 +0100 Subject: [PATCH] Add some initial measurements --- src/query/cypher_query_interpreter.hpp | 4 ++-- src/query/v2/cypher_query_interpreter.hpp | 2 +- src/query/v2/request_router.hpp | 9 +++++++++ src/telemetry/telemetry.hpp | 4 ++-- src/utils/timer.hpp | 22 ++++++++++++++++++++-- tests/manual/query_performance.cpp | 1 + tests/stress/long_running.cpp | 4 ++-- 7 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/query/cypher_query_interpreter.hpp b/src/query/cypher_query_interpreter.hpp index 14b4a3792..530eefa5f 100644 --- a/src/query/cypher_query_interpreter.hpp +++ b/src/query/cypher_query_interpreter.hpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -63,7 +63,7 @@ class CachedPlan { private: std::unique_ptr plan_; - utils::Timer cache_timer_; + utils::Timer<> cache_timer_; }; struct CachedQuery { diff --git a/src/query/v2/cypher_query_interpreter.hpp b/src/query/v2/cypher_query_interpreter.hpp index 18505820f..9a96d5adf 100644 --- a/src/query/v2/cypher_query_interpreter.hpp +++ b/src/query/v2/cypher_query_interpreter.hpp @@ -63,7 +63,7 @@ class CachedPlan { private: std::unique_ptr plan_; - utils::Timer cache_timer_; + utils::Timer<> cache_timer_; }; struct CachedQuery { diff --git a/src/query/v2/request_router.hpp b/src/query/v2/request_router.hpp index a8326d900..5e186beed 100644 --- a/src/query/v2/request_router.hpp +++ b/src/query/v2/request_router.hpp @@ -47,6 +47,7 @@ #include "storage/v3/id_types.hpp" #include "storage/v3/value_conversions.hpp" #include "utils/result.hpp" +#include "utils/timer.hpp" namespace memgraph::query::v2 { @@ -151,6 +152,7 @@ class RequestRouter : public RequestRouterInterface { } void StartTransaction() override { + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); coordinator::HlcRequest req{.last_shard_map_version = shards_map_.GetHlc()}; CoordinatorWriteRequests write_req = req; spdlog::trace("sending hlc request to start transaction"); @@ -172,6 +174,7 @@ class RequestRouter : public RequestRouterInterface { } void Commit() override { + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); coordinator::HlcRequest req{.last_shard_map_version = shards_map_.GetHlc()}; CoordinatorWriteRequests write_req = req; spdlog::trace("sending hlc request before committing transaction"); @@ -232,6 +235,7 @@ class RequestRouter : public RequestRouterInterface { } bool IsPrimaryProperty(storage::v3::LabelId primary_label, storage::v3::PropertyId property) const override { + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); const auto schema_it = shards_map_.schemas.find(primary_label); MG_ASSERT(schema_it != shards_map_.schemas.end(), "Invalid primary label id: {}", primary_label.AsUint()); @@ -248,6 +252,7 @@ class RequestRouter : public RequestRouterInterface { // TODO(kostasrim) Simplify return result std::vector ScanVertices(std::optional label) override { + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); // create requests auto requests_to_be_sent = RequestsForScanVertices(label); @@ -283,6 +288,7 @@ class RequestRouter : public RequestRouterInterface { std::vector CreateVertices(std::vector new_vertices) override { MG_ASSERT(!new_vertices.empty()); + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); // create requests std::vector> requests_to_be_sent = @@ -310,6 +316,7 @@ class RequestRouter : public RequestRouterInterface { std::vector CreateExpand(std::vector new_edges) override { MG_ASSERT(!new_edges.empty()); + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); // create requests std::vector> requests_to_be_sent = @@ -332,6 +339,7 @@ class RequestRouter : public RequestRouterInterface { } std::vector ExpandOne(msgs::ExpandOneRequest request) override { + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); // TODO(kostasrim)Update to limit the batch size here // Expansions of the destination must be handled by the caller. For example // match (u:L1 { prop : 1 })-[:Friend]-(v:L1) @@ -373,6 +381,7 @@ class RequestRouter : public RequestRouterInterface { } std::vector GetProperties(msgs::GetPropertiesRequest requests) override { + MG_RAII_TIMER(timer, __PRETTY_FUNCTION__); requests.transaction_id = transaction_id_; // create requests std::vector> requests_to_be_sent = diff --git a/src/telemetry/telemetry.hpp b/src/telemetry/telemetry.hpp index 5ff53bba7..70cfc0a93 100644 --- a/src/telemetry/telemetry.hpp +++ b/src/telemetry/telemetry.hpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -58,7 +58,7 @@ class Telemetry final { const std::string machine_id_; uint64_t num_{0}; utils::Scheduler scheduler_; - utils::Timer timer_; + utils::Timer<> timer_; const uint64_t send_every_n_; diff --git a/src/utils/timer.hpp b/src/utils/timer.hpp index 96a9fa1fd..50c108e0e 100644 --- a/src/utils/timer.hpp +++ b/src/utils/timer.hpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -16,17 +16,35 @@ namespace memgraph::utils { // This class is threadsafe. +template > class Timer { public: - Timer() : start_time_(std::chrono::steady_clock::now()) {} + Timer(std::function().count()) elapsed)> destroy_callback = nullptr) + : start_time_(std::chrono::steady_clock::now()), destroy_callback_(destroy_callback) {} template > TDuration Elapsed() const { return std::chrono::duration_cast(std::chrono::steady_clock::now() - start_time_); } + ~Timer() { + if (destroy_callback_) { + destroy_callback_(Elapsed().count()); + } + } + private: std::chrono::steady_clock::time_point start_time_; + std::function().count()))> destroy_callback_ = nullptr; }; +#define __MG_RAII_TIMER(name, message) \ + memgraph::utils::Timer<> name([](auto elapsed) { spdlog::critical("{} {}s", message, elapsed); }) +// TODO(gitbuda): Swap MG_TIMER defines +#ifdef MG_TIMER +#define MG_RAII_TIMER(name, message) +#else +#define MG_RAII_TIMER(name, message) __MG_RAII_TIMER(name, message) +#endif + } // namespace memgraph::utils diff --git a/tests/manual/query_performance.cpp b/tests/manual/query_performance.cpp index b766d5fbb..05b968a20 100644 --- a/tests/manual/query_performance.cpp +++ b/tests/manual/query_performance.cpp @@ -257,6 +257,7 @@ void RunV2() { void RunV3() { spdlog::critical("Running V3"); + MG_RAII_TIMER(timer, "WHOLE v3"); const auto run_start = std::chrono::high_resolution_clock::now(); std::ifstream sm_file{FLAGS_split_file, std::ios::in}; MG_ASSERT(sm_file.good(), "Cannot open split file to read: {}", FLAGS_split_file); diff --git a/tests/stress/long_running.cpp b/tests/stress/long_running.cpp index 11f5c5feb..055f4a434 100644 --- a/tests/stress/long_running.cpp +++ b/tests/stress/long_running.cpp @@ -1,4 +1,4 @@ -// Copyright 2022 Memgraph Ltd. +// Copyright 2023 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -92,7 +92,7 @@ class GraphSession { std::mt19937 generator_; - memgraph::utils::Timer timer_; + memgraph::utils::Timer<> timer_; private: double GetRandom() { return std::generate_canonical(generator_); }