ClientServerProject/project/metrics.h
2021-04-18 16:02:33 -04:00

60 lines
1.5 KiB
C++

#pragma once
//#include "libraries/json.hpp"
using json = nlohmann::json;
namespace metrics
{
class time_tracker
{
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
public:
template<typename T, typename U>
std::chrono::duration<T, U> elapsed() const
{
return std::chrono::duration<T, U>(std::chrono::high_resolution_clock::now() - start);
}
};
struct client_message
{
int client_number;
double trip_time;
};
inline void to_json(json& j, const client_message& p)
{
j = json{
{ "client_number", p.client_number },
{ "trip_time", p.trip_time }
};
}
struct job
{
int operation;
std::vector<client_message> client_metrics{};
};
inline void to_json(json& j, const job& p)
{
auto total_trip_time = 0.;
auto summation_time = 0.f;
for(auto&& client : p.client_metrics)
{
summation_time += client.trip_time;
#ifdef THREADED
total_trip_time = client.trip_time > total_trip_time ? client.trip_time : total_trip_time;
#else
total_trip_time += client.trip_time;
#endif
}
j = json{
{ "operation", p.operation },
{ "total_trip_time", total_trip_time },
{ "average_trip_time",summation_time / p.client_metrics.size() },
{ "client_metrics", p.client_metrics}
};
}
inline std::vector<job> job_metrics{};
}