I wanted to profile my java program where I was opening a file and performing read and write operations over it . The file to be read is a large XML file, and i wanted to compare memory stats/snapshots for parsing the file using techniques like batching / stream-reading / standard xml parsing etc.
So let's say we had three programs that I wanted to save the snapshots for :
BatchParsing.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
}
}
Moreover , I wanted to analyse not one , but multiple files of different sizes, and instead of running a loop over different files, I wanted to check profiling results by running each of the above mentioned programs separately for each file.
In then end , I wanted to save my profiling snapshots as follows.
- 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
I could do the above using the GUI, but wanted to automate all of this . I am currently using jprofiler over intelliJ on macOS .
To profile without the JProfiler GUI, you use offline profiling. Invoke
Session->Integration Wizards->New Remote Integration
in the JProfiler main menu and choose the "Profile offline" option on the "Startup mode" step. You will then get the VM parameter that is required to load the profiling agent in offline mode.
To record data and save snapshots, you can use triggers or the profiling API. In your case, the profiling API will be more convenient.
To record CPU date and save a snapshot of the entire application run, wrap your code like this:
Controller.startCPURecording(true);
// Your code here
Controller.stopCPURecording();
Controller.saveSnapshot(new File("<snapshot name>.jps"));