From 9fc9cf851c52ab89d4fbf5b7ccd8c33748cca6f3 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 14 Jun 2017 09:16:30 +0100 Subject: [PATCH] Add page_time token to mustache. That's the elapsed time from program start to the moment before the final invocation to mustache rendering. So not super accurate but that's what I can do for now. --- src/kamokan/main.cpp | 4 ++++ src/kamokan_impl/response.cpp | 13 +++++++++++++ src/kamokan_impl/response.hpp | 3 +++ src/kamokan_impl/response_factory.cpp | 18 ++++++++++++++++-- src/kamokan_impl/response_factory.hpp | 4 ++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/kamokan/main.cpp b/src/kamokan/main.cpp index 64e1477..cead51f 100644 --- a/src/kamokan/main.cpp +++ b/src/kamokan/main.cpp @@ -36,6 +36,7 @@ #include #include #include +#include //www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4150.pdf @@ -142,6 +143,8 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) { using kamokan::Response; using tawashi::RequestMethodType; + std::chrono::time_point app_start_time = std::chrono::steady_clock::now(); + if (2 == parArgc and boost::string_view(parArgv[1]) == "--show-paths") { print_buildtime_info(); return 0; @@ -166,6 +169,7 @@ int main (int parArgc, char* parArgv[], char* parEnvp[]) { resp_factory.register_maker("paste.cgi", RequestMethodType::POST, &make_response); resp_factory.register_maker("error.cgi", RequestMethodType::GET, &make_response); resp_factory.register_jolly_maker(&make_response, RequestMethodType::GET); + resp_factory.set_app_start_time(app_start_time); std::unique_ptr response = resp_factory.make_response( tawashi::cgi::drop_arguments(cgi_env->request_uri_relative()), diff --git a/src/kamokan_impl/response.cpp b/src/kamokan_impl/response.cpp index 108f94d..3e72496 100644 --- a/src/kamokan_impl/response.cpp +++ b/src/kamokan_impl/response.cpp @@ -135,6 +135,7 @@ namespace kamokan { //m_page_basename(fetch_page_basename(m_cgi_env)), m_cgi_env(parCgiEnv), m_settings(parSettings), + m_time0(std::chrono::steady_clock::now()), m_website_root(make_root_path(*parSettings)), m_base_uri(make_base_uri(m_settings, m_cgi_env)), m_stream_out(parStreamOut) @@ -187,9 +188,16 @@ namespace kamokan { *m_stream_out << http_header; if (http_header.body_required()) { + using std::chrono::steady_clock; + using std::chrono::milliseconds; + using std::chrono::duration_cast; + SPDLOG_TRACE(statuslog, "Raising event on_mustache_prepare"); this->on_mustache_prepare(mustache_context); + const auto time1 = steady_clock::now(); + const int page_time = duration_cast(time1 - m_time0).count(); + mustache_context["page_time"] = page_time; SPDLOG_TRACE(statuslog, "Rendering in mustache"); *m_stream_out << mstch::render( on_mustache_retrieve(), @@ -261,4 +269,9 @@ namespace kamokan { oss << "error.cgi?reason=" << parReason._to_integral(); return make_redirect(redir_code, oss.str()); } + + void Response::set_app_start_time (const std::chrono::time_point& parTime) { + assert(parTime <= m_time0); + m_time0 = parTime; + } } //namespace kamokan diff --git a/src/kamokan_impl/response.hpp b/src/kamokan_impl/response.hpp index c530543..0adf5fc 100644 --- a/src/kamokan_impl/response.hpp +++ b/src/kamokan_impl/response.hpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace tawashi { namespace cgi { @@ -43,6 +44,7 @@ namespace kamokan { virtual ~Response() noexcept; void send(); + void set_app_start_time (const std::chrono::time_point& parTime); protected: Response ( @@ -71,6 +73,7 @@ namespace kamokan { Storage m_storage; Kakoune::SafePtr m_cgi_env; Kakoune::SafePtr m_settings; + std::chrono::time_point m_time0; std::string m_website_root; std::string m_base_uri; std::ostream* m_stream_out; diff --git a/src/kamokan_impl/response_factory.cpp b/src/kamokan_impl/response_factory.cpp index 7ce88b2..3c5e3d1 100644 --- a/src/kamokan_impl/response_factory.cpp +++ b/src/kamokan_impl/response_factory.cpp @@ -34,11 +34,13 @@ namespace kamokan { boost::container::flat_map makers_post; std::array jolly_makers; Kakoune::SafePtr cgi_env; + std::chrono::time_point time0; }; ResponseFactory::ResponseFactory (const Kakoune::SafePtr& parSettings, const Kakoune::SafePtr& parCgiEnv) : m_local_data(std::make_unique()) { + m_local_data->time0 = std::chrono::steady_clock::now(); m_local_data->settings = parSettings; m_local_data->cgi_env = parCgiEnv; std::fill(m_local_data->jolly_makers.begin(), m_local_data->jolly_makers.end(), nullptr); @@ -61,11 +63,11 @@ namespace kamokan { auto maker_it = makers.find(name); if (makers.end() != maker_it) { - return maker_it->second(m_local_data->settings, m_local_data->cgi_env); + return make_response(maker_it->second); } else if (m_local_data->jolly_makers[parReqType]) { spdlog::get("statuslog")->info("no exact match found for \"{}\", assuming it's a pastie's token", name); - return m_local_data->jolly_makers[parReqType](m_local_data->settings, m_local_data->cgi_env); + return make_response(m_local_data->jolly_makers[parReqType]); } else { spdlog::get("statuslog")->critical("no exact match found for \"{}\" with method {} and no jolly maker given, this should not happen", name, parReqType._to_string()); @@ -73,6 +75,13 @@ namespace kamokan { } } + std::unique_ptr ResponseFactory::make_response (ResponseMakerFunc parMaker) const { + auto retval = parMaker(m_local_data->settings, m_local_data->cgi_env); + assert(retval); + retval->set_app_start_time(m_local_data->time0); + return retval; + } + void ResponseFactory::register_maker (std::string&& parName, ResponseMakerFunc parMaker) { m_local_data->makers_get[parName] = parMaker; m_local_data->makers_post[std::move(parName)] = parMaker; @@ -92,4 +101,9 @@ namespace kamokan { void ResponseFactory::register_jolly_maker (ResponseMakerFunc parMaker, tawashi::RequestMethodType parReqType) { m_local_data->jolly_makers[parReqType] = parMaker; } + + void ResponseFactory::set_app_start_time (const std::chrono::time_point& parTime) { + assert(parTime <= m_local_data->time0); + m_local_data->time0 = parTime; + } } //namespace kamokan diff --git a/src/kamokan_impl/response_factory.hpp b/src/kamokan_impl/response_factory.hpp index cb1777b..a8afb4a 100644 --- a/src/kamokan_impl/response_factory.hpp +++ b/src/kamokan_impl/response_factory.hpp @@ -21,6 +21,7 @@ #include "kakoune/safe_ptr.hh" #include "request_method_type.hpp" #include +#include namespace tawashi { namespace cgi { @@ -43,10 +44,13 @@ namespace kamokan { void register_maker (std::string&& parName, ResponseMakerFunc parMaker); void register_maker (std::string&& parName, tawashi::RequestMethodType parReqType, ResponseMakerFunc parMaker); void register_jolly_maker (ResponseMakerFunc parMaker, tawashi::RequestMethodType parReqType); + void set_app_start_time (const std::chrono::time_point& parTime); private: struct LocalData; + std::unique_ptr make_response (ResponseMakerFunc parMaker) const; + std::unique_ptr m_local_data; }; } //namespace kamokan