Skip to content

Commit

Permalink
scope timing print modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed May 22, 2024
1 parent 6c25d9c commit 599f4d4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.mkn
bin
__pycache__
dist
phlop.egg-info/
58 changes: 52 additions & 6 deletions inc/phlop/timing/scope_timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ struct ScopeTimerMan
traces.clear();
reports.clear();
}
_headers.clear();
active = false;
}

auto& file_name(std::string const& fn)
Expand All @@ -63,10 +65,28 @@ struct ScopeTimerMan
return *this;
}

auto& force_strings(bool const b = true)
{
_force_strings = b;
return *this;
}

template<typename... Args>
auto& headers(Args const... args)
{
_headers.clear();
(_headers.emplace_back(args), ...);
return *this;
}



static void reset() { INSTANCE().shutdown(); }

bool active = false;
bool active = false;
bool _force_strings = false;
std::string timer_file;
std::vector<std::string> _headers;
std::vector<RunTimerReport*> reports;
std::vector<RunTimerReportSnapshot*> traces;
RunTimerReport* report_stack_ptr = nullptr;
Expand Down Expand Up @@ -215,11 +235,37 @@ struct BinaryTimerFile
void write(std::string const& filename) const
{
std::ofstream f{filename, std::ios::binary};
for (auto const& [i, k] : id_to_key)
_byte_write(f, i, " ", k);
f << std::endl; // break between function id map and function times
for (auto const& root : roots)
_write(f, root);

if (ScopeTimerMan::INSTANCE()._force_strings)
{
if (ScopeTimerMan::INSTANCE()._headers.size())
{
f << ScopeTimerMan::INSTANCE()._headers[0];
for (std::size_t i = 1; i < ScopeTimerMan::INSTANCE()._headers.size(); ++i)
f << "," << ScopeTimerMan::INSTANCE()._headers[i];
f << std::endl;
}
for (auto const& root : roots)
_write_strings(f, root);
}
else
{
for (auto const& [i, k] : id_to_key)
_byte_write(f, i, " ", k);
f << std::endl; // break between function id map and function times
for (auto const& root : roots)
_write(f, root);
}
}

void _write_strings(std::ofstream& file, BinaryTimerFileNode const& node,
std::uint16_t tabs = 0) const
{
for (std::size_t ti = 0; ti < tabs; ++ti)
file << " ";
file << id_to_key.at(node.fn_id) << node.time << std::endl;
for (auto const& n : node.kinder)
_write_strings(file, n, tabs + 1);
}

void _write(std::ofstream& file, BinaryTimerFileNode const& node, std::uint16_t tabs = 0) const
Expand Down
2 changes: 1 addition & 1 deletion phlop/run/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def filter_out_regex_fails(cli_args, test_batches):
except re.error:
print("regex invalid, resorting to 'str in str' approach")
is_valid = False
op = lambda x: regex in x
op = lambda x: cli_args.regex in x
filtered = {tb.cores: [] for tb in test_batches}
for tb in test_batches:
for test in tb.tests:
Expand Down
20 changes: 12 additions & 8 deletions tests/timing/test_scope_timer.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@

#include "phlop/timing/scope_timer.hpp"

#include "magic_enum/magic_enum_all.hpp"

using namespace std::chrono_literals;

void fny()
{
PHLOP_SCOPE_TIMER("fny");
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);

std::this_thread::sleep_for(10ms);
}

void fn0()
{
PHLOP_SCOPE_TIMER("fn0");
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);

std::this_thread::sleep_for(10ms);

fny();

Expand All @@ -22,17 +26,17 @@ void fn0()
void fn1()
{
PHLOP_SCOPE_TIMER("fn1");
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);

std::this_thread::sleep_for(10ms);
PHLOP_SCOPE_TIMER("fn1b");
fn0();
}

void fn2()
{
PHLOP_SCOPE_TIMER("fn2");
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);

std::this_thread::sleep_for(10ms);
fn0();
}

Expand Down

0 comments on commit 599f4d4

Please sign in to comment.