Skip to content

Commit

Permalink
campaignd: Add --timings option to report request servicing timings
Browse files Browse the repository at this point in the history
Yes, this requires making campaignd::server::request public so I don't
lose my mind writing friend declarations.
  • Loading branch information
irydacea committed Nov 14, 2020
1 parent 420df89 commit 3022899
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/server/campaignd/options.cpp
Expand Up @@ -41,6 +41,7 @@ command_line::command_line(const std::vector<std::string>& args)
, show_log_domains(false)
, log_domain_levels()
, log_precise_timestamps(false)
, report_timings(false)
, argv0_(args.at(0))
, args_(args.begin() + 1, args.end())
, help_text_()
Expand All @@ -67,6 +68,7 @@ command_line::command_line(const std::vector<std::string>& args)
("log-debug", po::value<std::string>(), "sets the severity level of the specified log domain(s) to 'debug'.")
("log-none", po::value<std::string>(), "disables logging for the specified log domain(s).")
("log-precise", "shows the timestamps in log output with more precision.")
("timings", "outputs timings for serviced requests to stderr.")
;

po::options_description opts;
Expand Down Expand Up @@ -122,6 +124,9 @@ command_line::command_line(const std::vector<std::string>& args)
if(vm.count("log-precise")) {
log_precise_timestamps = true;
}
if(vm.count("timings")) {
report_timings = true;
}
}

} // end namespace campaignd
2 changes: 2 additions & 0 deletions src/server/campaignd/options.hpp
Expand Up @@ -62,6 +62,8 @@ class command_line
std::map<std::string, int> log_domain_levels;
/** Whether to use higher precision for log timestamps. */
bool log_precise_timestamps;
/** Whether to report timing information for server requests. */
bool report_timings;

private:
std::string argv0_;
Expand Down
27 changes: 26 additions & 1 deletion src/server/campaignd/server.cpp
Expand Up @@ -40,6 +40,7 @@
#include "server/campaignd/options.hpp"
#include "game_version.hpp"
#include "hash.hpp"
#include "utils/optimer.hpp"

#include <csignal>
#include <ctime>
Expand Down Expand Up @@ -73,6 +74,24 @@ namespace campaignd {

namespace {

bool timing_reports_enabled = false;

void timing_report_function(const util::ms_optimer& tim, const campaignd::server::request& req, const std::string& label = {})
{
if(timing_reports_enabled) {
if(label.empty()) {
LOG_CS << req << "Time elapsed: " << tim << " ms\n";
} else {
LOG_CS << req << "Time elapsed [" << label << "]: " << tim << " ms\n";
}
}
}

inline util::ms_optimer service_timer(const campaignd::server::request& req, const std::string& label = {})
{
return util::ms_optimer{std::bind(timing_report_function, std::placeholders::_1, req, label)};
}

//
// Auxiliary shortcut functions
//
Expand Down Expand Up @@ -449,7 +468,9 @@ void server::handle_request(socket_ptr socket, std::shared_ptr<simple_wml::docum

if(j != handlers_.end()) {
// Call the handler.
j->second(this, request(c.key, c.cfg, socket));
request req{c.key, c.cfg, socket};
auto st = service_timer(req);
j->second(this, req);
} else {
send_error("Unrecognized [" + c.key + "] request.",socket);
}
Expand Down Expand Up @@ -1728,6 +1749,10 @@ int run_campaignd(int argc, char** argv)
lg::precise_timestamps(true);
}

if(cmdline.report_timings) {
campaignd::timing_reports_enabled = true;
}

std::cerr << "Wesnoth campaignd v" << game_config::revision << " starting...\n";

if(server_path.empty() || !filesystem::is_directory(server_path)) {
Expand Down
3 changes: 2 additions & 1 deletion src/server/campaignd/server.hpp
Expand Up @@ -43,7 +43,6 @@ class server : public server_base

server& operator=(const config& server) = delete;

private:
/**
* Client request information object.
*
Expand Down Expand Up @@ -84,6 +83,8 @@ class server : public server_base

friend std::ostream& operator<<(std::ostream& o, const request& r);

private:

typedef std::function<void (server*, const request& req)> request_handler;
typedef std::map<std::string, request_handler> request_handlers_table;

Expand Down

0 comments on commit 3022899

Please sign in to comment.