Command Profiler 是一个模组,允许你使用 /cmdprof 命令对命令(commands)和函数(functions)进行性能分析。
使用分析器
如果你拥有管理员权限(operator privileges),可以使用 /cmdprof enable 命令来启动分析器。要停止收集数据,请执行 /cmdprof disable;要重置当前已分析的数据,请执行 /cmdprof reset。
查看分析结果
有三种不同的格式用于获取数据:
- 在控制台中显示文本(以
print 开头的子命令)
- 保存为 JSON 格式(以
save 开头的子命令)
- 保存为字节流(与
save 相同,但需要提供 bytes 作为参数)
这意味着你需要访问服务器控制台才能查看分析结果。
有 print 和 save 子命令可用于查看整个分析运行期间的总体结果。(print 按总耗时排序)
还有 print_trace 和 save_trace 子命令可用于查看最近一次捕获的 tick 内的分析结果。(按执行顺序排列)
注意:要使用这些命令查看追踪(trace)数据,你必须先启用 cmdprof:local/trace。而对于配置文件(profile)数据,你需要启用 cmdprof:local/profile,该选项默认处于启用状态。
配置分析器
你可以使用 /cmdprof config 子命令来配置分析器,目前有以下多个选项:
cmdprof:granularity - 设置为 command 时将对所有命令进行分析,设置为 function 时不会单独分析命令
cmdprof:command/macro_templates - 设置为 false 时,带有宏的命令会将其参数内联到命令中
cmdprof:function/line_numbers - 设置为 true 时,命令将包含其行号,而不是设为 0
cmdprof:local/profile,当设置为 true 时,/cmdprof print 和 /cmdprof save 可用
cmdprof:local/trace,当设置为 true 时,/cmdprof print_trace 和 /cmdprof save_trace 可用
cmdprof:memory_usage,当设置为 true 时,所有事件都会跟踪内存使用情况(这会显著降低性能,除非你正在尝试制作修复内存问题的模组,否则不建议使用)
cmdprof:macro/instantiations,当设置为 true 时,分析结果会将实例化宏函数所花费的时间作为一个单独的步骤列出,而不是将其合并到执行实例化的命令中
cmdprof:online/scoreboard - 当设置为 true 时,服务器将在启用时将记分板数据发送到在线数据页面。
在线查看数据
你可以使用 /cmdprof online view 打开在线分析器界面,并使用 /cmdprof online stop 停止它。注意,你需要先通过 /cmdprof enable 启用分析器,否则将无法提供数据。如果你想在线查看记分板数据,请执行 /cmdprof config cmdprof:online/scoreboard true。
当分析器运行时,数据会自动每秒刷新一次。记分板数据大约每个 tick 更新一次。
基准测试函数
使用 /cmdprof benchmark run <name>,你可以对一个函数或函数标签(function tag)进行基准测试。这可能需要一段时间,但在完成后,它会在聊天窗口中显示结果。你可以通过 /cmdprof benchmark cancel 取消当前正在运行的基准测试。成功的 /reload 操作会自动取消所有待处理的基准测试。被基准测试的函数是在服务器命令上下文中运行的,而不是由运行命令的玩家上下文运行。
此外,你还可以使用 /cmdprof benchmark command <command> 对单个命令进行基准测试。
注意:专门进行基准测试时,你不需要启用分析器,你可能应该执行 /cmdprof disable 以获得更准确的函数速度表现。但是,如果你希望转储(dump)结果等,可能仍希望保持分析器处于启用状态。
Command Profiler is a mod which lets you profile commands and functions using /cmdprof.
Using the profiler
You can start the profiler if you have operator privileges using the /cmdprof enable command. For the profiler to stop collecting data do /cmdprof disable, and to reset the current profiled data do /cmdprof reset.
Viewing profiler results
There are three different formats for taking the data
- display text in console (subcommands that start with
print)
- save as JSON (subcommands that start with
save)
- save as bytes (same as
save except you need to provide bytes as an argument)
This does mean that you need access to the server console to view the profiler results.
There are print and save subcommands for viewing the profiler results over the entire duration of the profiler running. (print is sorted by total time taken)
And there are print_trace and save_trace subcommands for viewing the profiler results over the last captured tick. (ordered by execution)
Note: To use these commands to view trace data, you have to enable cmdprof:local/trace first. Meanwhile for profile data, you have to enable cmdprof:local/profile which is enabled by default.
Configuring the profiler
You can configure the profiler using the /cmdprof config subcommand, there are currently several options:
cmdprof:granularity - when set to command all commands will be profiled, and when set to function commands will not be profiled individually
cmdprof:command/macro_templates - when set to false, commands which have macros will have their arguments inlined into the command
cmdprof:function/line_numbers - when set to true, commands will include their line number, instead of being set to 0
cmdprof:local/profile, when set to true, /cmdprof print and /cmdprof save are usable
cmdprof:local/trace, when set to true, /cmdprof print_trace and /cmdprof save_trace are usable
cmdprof:memory_usage, when set to true, all events will track the memory usage (this reduces the performance by quite a bit, not worth using it unless you're trying to make a mod which fixes memory usage issues)
cmdprof:macro/instantiations, when set to true, profiler results will include how long it takes to instantiate a macro function as a separate step, instead of merging it with the command that executes the instantiation
cmdprof:online/scoreboard - when set to true, the server will send scoreboard data to the online data page when enabled.
Viewing data online
You can open the profiler online using /cmdprof online view, and stop it using /cmdprof online stop. Note that you need to enable the profiler using /cmdprof enable first, otherwise there will be no data provided. If you want to see scoreboard data online, do /cmdprof config cmdprof:online/scoreboard true.
The profiler automatically refreshes data every second when it is running. The scoreboard data gets updated approximately every tick.
Benchmarking functions
Using /cmdprof benchmark run <name>, you can benchmark a function or a function tag, it might take a while, but after it finishes, it will display the results in chat. You can cancel a currently running benchmark via /cmdprof benchmark cancel. A successful /reload automatically cancels all pending benchmarks. The benchmarked function(s) run in the server command context instead of whoever is running it.
Additionally, you benchmark a single command using /cmdprof benchmark command <command>.
Note: You don't need to have the profiler enabled for benchmarking specifically, you should probably do /cmdprof disable to have a more accurate representation of how fast a function could be. However you might still want to keep the profiler enabled, if you want to dump the results for example.
评论 0
登录后参与评分
还没有评论,来抢沙发吧