java - 如何自动化 jprofiler 以保存具有不同参数的多个目标 java 程序的快照
问题描述
我想分析我的 java 程序,我在其中打开一个文件并对其执行读写操作。要读取的文件是一个大型 XML 文件,我想比较内存统计信息/快照以使用批处理/流读取/标准 xml 解析等技术解析文件。
假设我们有三个程序要保存快照:
批处理解析.java
public class BatchParsing {
public static void main(String[] args) throws Exception {
File = new File("path/to/file")
// parse XML using batch logic
}
}
StreamReading.java
public class StreamReadingParsing {
public static void main(String[] args) throws Exception {
File = new File("path/to/file")
// parse XML using stream reader
}
}
NormalParsing.java
public class NormalParsing {
public static void main(String[] args) throws Exception {
File = new File("path/to/file")
// parse XML by loading whole file
}
}
此外,我想分析的不是一个,而是多个不同大小的文件,而不是对不同的文件运行循环,我想通过为每个文件分别运行上述每个程序来检查分析结果。
最后,我想按如下方式保存我的分析快照。
- snapshot_results
- batch_results
- small_file.jps
- average_file.jps
- large_file.jps
- stream_results
- small_file.jps
- average_file.jps
- large_file.jps
- normal_parsing_results
- small_file.jps
- average_file.jps
- large_file.jps
我可以使用 GUI 完成上述操作,但想要自动化所有这些。我目前在 macOS 上使用 jprofiler 而不是 intelliJ 。
解决方案
要在没有 JProfiler GUI 的情况下进行分析,请使用离线分析。调用
会话->集成向导->新建远程集成
在 JProfiler 主菜单中,在“启动模式”步骤中选择“离线配置文件”选项。然后,您将获得在离线模式下加载分析代理所需的 VM 参数。
要记录数据和保存快照,您可以使用触发器或分析 API。在您的情况下,分析 API会更方便。
要记录 CPU 日期并保存整个应用程序运行的快照,请像这样包装您的代码:
Controller.startCPURecording(true);
// Your code here
Controller.stopCPURecording();
Controller.saveSnapshot(new File("<snapshot name>.jps"));