ClientServerProject/project/metrics.h
2021-02-12 03:19:18 -05:00

51 lines
1.3 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.;
for(auto&& client : p.client_metrics)
total_trip_time += client.trip_time;
j = json{
{ "operation", p.operation },
{ "total_trip_time", total_trip_time },
{ "average_trip_time",total_trip_time / p.client_metrics.size() },
{ "client_metrics", p.client_metrics}
};
}
inline std::vector<job> job_metrics{};
}