Skip to content

Commit ead6249

Browse files
committed
feat(resmon): Save to JSON (Chrome Performance Visualizer)
1 parent 568b453 commit ead6249

File tree

9 files changed

+123
-11
lines changed

9 files changed

+123
-11
lines changed

src/core/commands/chat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void SwiftlyChatReload(CPlayerSlot slot)
1111
void SwiftlyChatManagerHelp(CPlayerSlot slot)
1212
{
1313
PrintToClientOrConsole(slot, "Commands", "Swiftly Chat Menu\n");
14-
PrintToClientOrConsole(slot, "Commands", "Usage: swiftly chat <command>\n");
14+
PrintToClientOrConsole(slot, "Commands", "Usage: sw chat <command>\n");
1515
PrintToClientOrConsole(slot, "Commands", " reload - Reload chat message configurations.\n");
1616
}
1717

src/core/commands/config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
void SwiftlyConfigurationManagerHelp(CPlayerSlot slot)
55
{
66
PrintToClientOrConsole(slot, "Commands", "Swiftly Configuration Menu\n");
7-
PrintToClientOrConsole(slot, "Commands", "Usage: swiftly config <command>\n");
7+
PrintToClientOrConsole(slot, "Commands", "Usage: sw config <command>\n");
88
PrintToClientOrConsole(slot, "Commands", " reload - Reloads all the plugin configurations.\n");
99
}
1010

src/core/commands/exts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
void SwiftlyExtensionManagerHelp(CPlayerSlot slot)
66
{
77
PrintToClientOrConsole(slot, "Commands", "Swiftly Configuration Menu\n");
8-
PrintToClientOrConsole(slot, "Commands", "Usage: swiftly exts <command>\n");
8+
PrintToClientOrConsole(slot, "Commands", "Usage: sw exts <command>\n");
99
PrintToClientOrConsole(slot, "Commands", " list - Shows all the extensions loaded.\n");
1010
}
1111

src/core/commands/help.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
void ShowSwiftlyCommandHelp(CPlayerSlot slot)
44
{
55
PrintToClientOrConsole(slot, "Commands", "Swiftly Commands Menu\n");
6-
PrintToClientOrConsole(slot, "Commands", "Usage: swiftly <command> [args]\n");
6+
PrintToClientOrConsole(slot, "Commands", "Usage: sw <command> [args]\n");
77
PrintToClientOrConsole(slot, "Commands", " credits - List Swiftly credits\n");
88
PrintToClientOrConsole(slot, "Commands", " help - Show the help for Swiftly commands\n");
99
PrintToClientOrConsole(slot, "Commands", " list - Show the list of online players\n");

src/core/commands/plugins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
void ShowSwiftlyPluginManagerHelp(CPlayerSlot slot)
66
{
77
PrintToClientOrConsole(slot, "Commands", "Swiftly Plugin Management Menu\n");
8-
PrintToClientOrConsole(slot, "Commands", "Usage: swiftly plugins <command> [plugin_name]\n");
8+
PrintToClientOrConsole(slot, "Commands", "Usage: sw plugins <command> [plugin_name]\n");
99
PrintToClientOrConsole(slot, "Commands", " info - Shows informations about a plugin\n");
1010
PrintToClientOrConsole(slot, "Commands", " list - Shows loaded plugins\n");
1111
PrintToClientOrConsole(slot, "Commands", " load - Loads a plugin\n");

src/core/commands/resmon.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
#include <tools/resourcemonitor/monitor.h>
44
#include <plugins/manager.h>
5+
#include <filesystem/files/files.h>
56

67
#include <sstream>
78

89
void SwiftlyResourceMonitorManagerHelp(CPlayerSlot slot)
910
{
1011
PrintToClientOrConsole(slot, "Commands", "Swiftly Resource Monitor Menu\n");
11-
PrintToClientOrConsole(slot, "Commands", "Usage: swiftly resmon <command>\n");
12-
PrintToClientOrConsole(slot, "Commands", " enable - Enabled the usage monitoring.\n");
13-
PrintToClientOrConsole(slot, "Commands", " disable - Disables the usage monitoring.\n");
14-
PrintToClientOrConsole(slot, "Commands", " view - Shows the usage monitored.\n");
15-
PrintToClientOrConsole(slot, "Commands", " viewplugin <ID> - Shows the usage monitored for a specific plugin.\n");
12+
PrintToClientOrConsole(slot, "Commands", "Usage: sw resmon <command>\n");
13+
PrintToClientOrConsole(slot, "Commands", " enable - Enabled the usage monitoring.\n");
14+
PrintToClientOrConsole(slot, "Commands", " disable - Disables the usage monitoring.\n");
15+
PrintToClientOrConsole(slot, "Commands", " view - Shows the usage monitored.\n");
16+
PrintToClientOrConsole(slot, "Commands", " viewplugin <ID> - Shows the usage monitored for a specific plugin.\n");
17+
PrintToClientOrConsole(slot, "Commands", " save [plugin_name] - Saves the registered usage into a JSON file.\n");
1618
}
1719

1820
void SwiftlyResourceMonitorManagerEnable(CPlayerSlot slot)
@@ -33,6 +35,28 @@ void SwiftlyResourceMonitorManagerDisable(CPlayerSlot slot)
3335
PrintToClientOrConsole(slot, "Resource Monitor", "Resource monitoring has been disabled.\n");
3436
}
3537

38+
void SwiftlyResourceMonitorManagerSavePlugin(CPlayerSlot slot, std::string plugin_id)
39+
{
40+
if (!g_ResourceMonitor.IsEnabled())
41+
return PrintToClientOrConsole(slot, "Resource Monitor", "Resource monitoring is not enabled.\n");
42+
43+
auto json_output = g_ResourceMonitor.GenerateJSONPerformance(plugin_id);
44+
45+
if (!Files::ExistsPath("addons/swiftly/profilers"))
46+
{
47+
if (!Files::CreateDirectory("addons/swiftly/profilers"))
48+
{
49+
return PrintToClientOrConsole(slot, "Resource Monitor", "Couldn't create profilers folder.\n");
50+
}
51+
}
52+
53+
std::string file_path = string_format("addons/swiftly/profilers/profiler.%s.%s.json", plugin_id == "" ? "all" : plugin_id.c_str(), get_uuid().c_str());
54+
if (Files::ExistsPath(file_path)) Files::Delete(file_path);
55+
56+
Files::Write(file_path, json_output, false);
57+
PrintToClientOrConsole(slot, "Resource Monitor", "The profiler file has been saved to %s.\n", file_path.c_str());
58+
}
59+
3660
void SwiftlyResourceMonitorManagerViewPlugin(CPlayerSlot slot, std::string plugin_id)
3761
{
3862
if (!g_ResourceMonitor.IsEnabled())
@@ -238,6 +262,8 @@ void SwiftlyResourceMonitorManager(CPlayerSlot slot, const char* subcmd, const c
238262
SwiftlyResourceMonitorManagerView(slot);
239263
else if (sbcmd == "viewplugin")
240264
SwiftlyResourceMonitorManagerViewPlugin(slot, (subcmd2 == nullptr ? "" : subcmd2));
265+
else if (sbcmd == "save")
266+
SwiftlyResourceMonitorManagerSavePlugin(slot, (subcmd2 == nullptr ? "" : subcmd2));
241267
else
242268
SwiftlyResourceMonitorManagerHelp(slot);
243269
}

src/core/commands/translations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
void SwiftlyTranslationManagerHelp(CPlayerSlot slot)
55
{
66
PrintToClientOrConsole(slot, "Commands", "Swiftly Translation Menu\n");
7-
PrintToClientOrConsole(slot, "Commands", "Usage: swiftly translations <command>\n");
7+
PrintToClientOrConsole(slot, "Commands", "Usage: sw translations <command>\n");
88
PrintToClientOrConsole(slot, "Commands", " reload - Reloads the translations.\n");
99
}
1010

src/tools/resourcemonitor/monitor.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "monitor.h"
22

3+
#include <memory/encoders/json.h>
4+
#include <utils/utils.h>
5+
36
void ResourceMonitor::Enable()
47
{
58
resmonTimesTable.clear();
@@ -53,4 +56,85 @@ void ResourceMonitor::StopTime(std::string plugin_id, std::string key)
5356
if (resmonTempTables[plugin_id].find(key) == resmonTempTables[plugin_id].end()) return;
5457

5558
RecordTime(plugin_id, key, float(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - resmonTempTables[plugin_id][key]).count()) / 1000.0);
59+
}
60+
61+
std::string ResourceMonitor::GenerateJSONPerformance(std::string plugin_id)
62+
{
63+
rapidjson::Document doc(rapidjson::kArrayType);
64+
65+
rapidjson::Document v1 = encoders::json::FromString("{\"args\": {\"name\": \"Swiftly\"},\"cat\": \"__metadata\",\"name\": \"process_name\",\"ph\": \"M\",\"pid\": 1,\"tid\": 1,\"ts\": 0}");
66+
doc.PushBack(v1, doc.GetAllocator());
67+
rapidjson::Document v2 = encoders::json::FromString("{\"args\":{\"name\":\"Swiftly Main\"},\"cat\":\"__metadata\",\"name\":\"thread_name\",\"ph\":\"M\",\"pid\":1,\"tid\":1,\"ts\":0}");
68+
doc.PushBack(v2, doc.GetAllocator());
69+
rapidjson::Document v3 = encoders::json::FromString("{\"args\":{\"name\":\"Swiftly Profiler\"},\"cat\":\"__metadata\",\"name\":\"thread_name\",\"ph\":\"M\",\"pid\":1,\"tid\":2,\"ts\":0}");
70+
doc.PushBack(v3, doc.GetAllocator());
71+
72+
uint64_t t = 0;
73+
auto timings = GetResmonTimeTables();
74+
if (plugin_id == "") {
75+
for (auto it = timings.begin(); it != timings.end(); ++it) {
76+
for (auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
77+
for (float time : it2->second) {
78+
uint64_t uint_time = (uint64_t)(time * 1000.0f);
79+
80+
rapidjson::Value val(rapidjson::kObjectType);
81+
rapidjson::Value val2(rapidjson::kObjectType);
82+
83+
t++;
84+
85+
std::string event_name = string_format("%s [%s]", it2->first.c_str(), it->first.c_str());
86+
87+
val.AddMember("name", rapidjson::Value().SetString(event_name.c_str(), doc.GetAllocator()), doc.GetAllocator());
88+
val.AddMember("ph", rapidjson::Value().SetString("B", doc.GetAllocator()), doc.GetAllocator());
89+
val.AddMember("tid", rapidjson::Value().SetInt(2), doc.GetAllocator());
90+
val.AddMember("pid", rapidjson::Value().SetInt(1), doc.GetAllocator());
91+
val.AddMember("ts", rapidjson::Value().SetUint64(t), doc.GetAllocator());
92+
93+
doc.PushBack(val, doc.GetAllocator());
94+
95+
t += uint_time;
96+
97+
val2.AddMember("name", rapidjson::Value().SetString(event_name.c_str(), doc.GetAllocator()), doc.GetAllocator());
98+
val2.AddMember("ph", rapidjson::Value().SetString("E", doc.GetAllocator()), doc.GetAllocator());
99+
val2.AddMember("tid", rapidjson::Value().SetInt(2), doc.GetAllocator());
100+
val2.AddMember("pid", rapidjson::Value().SetInt(1), doc.GetAllocator());
101+
val2.AddMember("ts", rapidjson::Value().SetUint64(t), doc.GetAllocator());
102+
103+
doc.PushBack(val2, doc.GetAllocator());
104+
}
105+
}
106+
}
107+
}
108+
else {
109+
for (auto it2 = timings[plugin_id].begin(); it2 != timings[plugin_id].end(); ++it2) {
110+
for (float time : it2->second) {
111+
uint64_t uint_time = (uint64_t)(time * 1000.0f);
112+
113+
rapidjson::Value val(rapidjson::kObjectType);
114+
rapidjson::Value val2(rapidjson::kObjectType);
115+
116+
t++;
117+
118+
val.AddMember("name", rapidjson::Value().SetString(it2->first.c_str(), doc.GetAllocator()), doc.GetAllocator());
119+
val.AddMember("ph", rapidjson::Value().SetString("B", doc.GetAllocator()), doc.GetAllocator());
120+
val.AddMember("tid", rapidjson::Value().SetInt(2), doc.GetAllocator());
121+
val.AddMember("pid", rapidjson::Value().SetInt(1), doc.GetAllocator());
122+
val.AddMember("ts", rapidjson::Value().SetUint64(t), doc.GetAllocator());
123+
124+
doc.PushBack(val, doc.GetAllocator());
125+
126+
t += uint_time;
127+
128+
val2.AddMember("name", rapidjson::Value().SetString(it2->first.c_str(), doc.GetAllocator()), doc.GetAllocator());
129+
val2.AddMember("ph", rapidjson::Value().SetString("E", doc.GetAllocator()), doc.GetAllocator());
130+
val2.AddMember("tid", rapidjson::Value().SetInt(2), doc.GetAllocator());
131+
val2.AddMember("pid", rapidjson::Value().SetInt(1), doc.GetAllocator());
132+
val2.AddMember("ts", rapidjson::Value().SetUint64(t), doc.GetAllocator());
133+
134+
doc.PushBack(val2, doc.GetAllocator());
135+
}
136+
}
137+
}
138+
139+
return std::string("{\"traceEvents\":") + encoders::json::ToString(doc) + "}";
56140
}

src/tools/resourcemonitor/monitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class ResourceMonitor
2323
void StopTime(std::string plugin_id, std::string key);
2424

2525
std::map<std::string, std::map<std::string, std::vector<float>>> GetResmonTimeTables();
26+
27+
std::string GenerateJSONPerformance(std::string plugin_id = "");
2628
};
2729

2830
extern ResourceMonitor g_ResourceMonitor;

0 commit comments

Comments
 (0)