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