Simple Runtime Analyzer
Loading...
Searching...
No Matches
runtime_reporter.hpp
1
13#pragma once
14
15#include <cstddef>
16#include <filesystem>
17#include <fstream>
18#include <iostream>
19#include <ranges>
20#include <stdexcept>
21#include <string>
22
23#include "shineknightdev/runtime_analyzer.hpp"
24
25namespace sra
26{
27
28namespace detail
29{
30
31template <ChronoDuration Unit>
32void write_text_report_impl(std::ostream& out, const RuntimeProfile<Unit>& profile)
33{
34 size_t i = 0;
35 for (const auto& [duration, size] : std::views::zip(profile.raw_durations, profile.sample_sizes))
36 {
37 out << "Sample " << ++i << ": "
38 << "| Time: " << duration.count() << " " << profile.unit_symbol << " | Sample size: " << size << "\n";
39 }
40}
41
42template <ChronoDuration Unit>
43void write_csv_report_impl(std::ostream& out, const RuntimeProfile<Unit>& profile)
44{
45 out << "sample_id,time_unit,time_value,sample_size\n";
46 size_t i = 0;
47 for (const auto& [duration, size] : std::views::zip(profile.raw_durations, profile.sample_sizes))
48 {
49 out << ++i << "," << profile.unit_symbol << "," << duration.count() << "," << size << "\n";
50 }
51}
52
53template <ChronoDuration Unit>
54void write_json_report_impl(std::ostream& out, const RuntimeProfile<Unit>& profile)
55{
56 out << "[\n";
57 bool first = true;
58 size_t i = 0;
59
60 for (const auto& [duration, size] : std::views::zip(profile.raw_durations, profile.sample_sizes))
61 {
62 if (!first) { out << ",\n"; }
63
64 out << " {\n"
65 << " \"sample_id\": " << ++i << ",\n"
66 << " \"time_unit\": \"" << profile.unit_symbol << "\",\n"
67 << " \"time_value\": " << duration.count() << ",\n"
68 << " \"sample_size\": " << size << "\n"
69 << " }";
70
71 first = false;
72 }
73 out << "\n]\n";
74}
75
76} // namespace detail
77
78template <ChronoDuration Unit>
79void print_report(const RuntimeProfile<Unit>& runtime_profile)
80{
81 detail::write_text_report_impl(std::cout, runtime_profile);
82}
83
84template <ChronoDuration Unit>
85void save_report(const RuntimeProfile<Unit>& runtime_profile, const std::filesystem::path& filename)
86{
87 std::ofstream file(filename);
88 if (!file.is_open()) { throw std::runtime_error("Error: Could not open file " + filename.string()); }
89
90 const auto ext = filename.extension();
91
92 if (ext == ".txt") { detail::write_text_report_impl(file, runtime_profile); }
93 else if (ext == ".csv") { detail::write_csv_report_impl(file, runtime_profile); }
94 else if (ext == ".json") { detail::write_json_report_impl(file, runtime_profile); }
95 else { throw std::runtime_error("Error: Unsupported file extension " + ext.string()); }
96}
97
98template <ChronoDuration Unit>
99void save_report(const RuntimeProfile<Unit>& runtime_profile, const std::string& base_filename = "runtime_report")
100{
101 const auto filename = std::filesystem::path(base_filename).replace_extension(".csv");
102 save_report(runtime_profile, filename);
103}
104
105template <ChronoDuration Unit>
106void save_reports(const RuntimeProfile<Unit>& runtime_profile, const std::string& base_filename = "runtime_report")
107{
108 std::filesystem::path base_path(base_filename);
109
110 save_report(runtime_profile, base_path.replace_extension(".csv"));
111 save_report(runtime_profile, base_path.replace_extension(".json"));
112 save_report(runtime_profile, base_path.replace_extension(".txt"));
113}
114
115template <ChronoDuration Unit, typename Stream>
116requires std::derived_from<Stream, std::ostream>
117void generate_report(Stream& stream, const RuntimeProfile<Unit>& runtime_profile, const std::string& format = "text")
118{
119 if (format == "text" || format == "txt") { detail::write_text_report_impl(stream, runtime_profile); }
120 else if (format == "csv") { detail::write_csv_report_impl(stream, runtime_profile); }
121 else if (format == "json") { detail::write_json_report_impl(stream, runtime_profile); }
122 else { throw std::invalid_argument("Unsupported format: " + format); }
123}
124
125} // namespace sra
void write_text_report_impl(std::ostream &out, const RuntimeProfile< Unit > &profile)
Definition runtime_reporter.hpp:32
void write_json_report_impl(std::ostream &out, const RuntimeProfile< Unit > &profile)
Definition runtime_reporter.hpp:54
void write_csv_report_impl(std::ostream &out, const RuntimeProfile< Unit > &profile)
Definition runtime_reporter.hpp:43
Definition runtime_analyzer.hpp:28
void generate_report(Stream &stream, const RuntimeProfile< Unit > &runtime_profile, const std::string &format="text")
Definition runtime_reporter.hpp:117
void save_report(const RuntimeProfile< Unit > &runtime_profile, const std::filesystem::path &filename)
Definition runtime_reporter.hpp:85
void save_reports(const RuntimeProfile< Unit > &runtime_profile, const std::string &base_filename="runtime_report")
Definition runtime_reporter.hpp:106
void print_report(const RuntimeProfile< Unit > &runtime_profile)
Definition runtime_reporter.hpp:79
Definition runtime_analyzer.hpp:60
std::string unit_symbol
Definition runtime_analyzer.hpp:63
std::vector< Unit > raw_durations
Definition runtime_analyzer.hpp:61
std::vector< size_t > sample_sizes
Definition runtime_analyzer.hpp:62