Skip to content

Commit

Permalink
add custom profiling via Debug(Start|Stop)Profile
Browse files Browse the repository at this point in the history
  • Loading branch information
lionkor committed Jan 24, 2024
1 parent 5cd7584 commit 0e2855d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/Profiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct Stats {
double stddev;
double min;
double max;
size_t n;
};

/// Calculates and stores the moving average over K samples of execution time data
Expand Down
1 change: 1 addition & 0 deletions include/TLuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class TLuaEngine : public std::enable_shared_from_this<TLuaEngine>, IThreaded {
sol::table Lua_FS_ListDirectories(const std::string& Path);

prof::UnitProfileCollection mProfile {};
std::unordered_map<std::string, prof::TimePoint> mProfileStarts;

std::string mName;
TLuaStateId mStateId;
Expand Down
1 change: 1 addition & 0 deletions src/Profiling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ prof::Stats prof::UnitExecutionTime::stats() const {
if (measurements->size() == 0) {
return result;
}
result.n = measurements->size();
result.max = std::numeric_limits<double>::min();
result.min = std::numeric_limits<double>::max();
Duration sum {};
Expand Down
11 changes: 11 additions & 0 deletions src/TLuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,20 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
Result[name]["stddev"] = stat.stddev;
Result[name]["min"] = stat.min;
Result[name]["max"] = stat.max;
Result[name]["n"] = stat.n;
}
return Result;
});
UtilTable.set_function("DebugStartProfile", [this](const std::string& name) {
mProfileStarts[name] = prof::now();
});
UtilTable.set_function("DebugStopProfile", [this](const std::string& name) {
if (!mProfileStarts.contains(name)) {
beammp_lua_errorf("DebugStopProfile('{}') failed, because a profile for '{}' wasn't started", name, name);
return;
}
mProfile.add_sample(name, prof::duration(mProfileStarts.at(name), prof::now()));
});

auto HttpTable = StateView.create_named_table("Http");
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
Expand Down

0 comments on commit 0e2855d

Please sign in to comment.