@@ -36,9 +36,8 @@ public class BenchmarkDiagnostics implements AutoCloseable {
3636 private final DiskUsageMonitor diskUsageMonitor ;
3737 private final PerformanceAnalyzer performanceAnalyzer ;
3838 private final List <SystemMonitor .SystemSnapshot > snapshots ;
39- private final List <DiskUsageMonitor .DiskUsageSnapshot > diskSnapshots ;
39+ private final List <DiskUsageMonitor .MultiDirectorySnapshot > diskSnapshots ;
4040 private final List <PerformanceAnalyzer .TimingAnalysis > timingAnalyses ;
41- private Path monitoredDirectory ;
4241 private boolean diskMonitorStarted = false ;
4342
4443 public BenchmarkDiagnostics (DiagnosticLevel level ) {
@@ -49,7 +48,6 @@ public BenchmarkDiagnostics(DiagnosticLevel level) {
4948 this .snapshots = new ArrayList <>();
5049 this .diskSnapshots = new ArrayList <>();
5150 this .timingAnalyses = new ArrayList <>();
52- this .monitoredDirectory = null ;
5351 }
5452
5553 /**
@@ -79,12 +77,27 @@ public static BenchmarkDiagnostics createVerbose() {
7977 *
8078 * @param directory the directory to monitor
8179 * @throws IOException if unable to start monitoring
80+ * @deprecated Use {@link #startMonitoring(String, Path)} instead
8281 */
82+ @ Deprecated
8383 public void setMonitoredDirectory (Path directory ) throws IOException {
84- this .monitoredDirectory = directory ;
85- if (directory != null && !diskMonitorStarted ) {
86- diskUsageMonitor .start (directory );
84+ startMonitoring ("default" , directory );
85+ }
86+
87+ /**
88+ * Starts monitoring a labeled directory for disk usage.
89+ * This should be called before capturing any snapshots for optimal performance.
90+ *
91+ * @param label a label to identify this directory in reports
92+ * @param directory the directory to monitor
93+ * @throws IOException if unable to start monitoring
94+ */
95+ public void startMonitoring (String label , Path directory ) throws IOException {
96+ if (!diskMonitorStarted ) {
97+ diskUsageMonitor .startMonitoring (label , directory );
8798 diskMonitorStarted = true ;
99+ } else {
100+ diskUsageMonitor .addDirectory (label , directory );
88101 }
89102 }
90103
@@ -95,12 +108,12 @@ public void capturePrePhaseSnapshot(String phase) {
95108 SystemMonitor .SystemSnapshot snapshot = systemMonitor .captureSnapshot ();
96109 snapshots .add (snapshot );
97110
98- // Capture disk usage if directory is set
99- if (monitoredDirectory != null ) {
111+ // Capture disk usage if monitoring is started
112+ if (diskMonitorStarted ) {
100113 try {
101- DiskUsageMonitor .DiskUsageSnapshot diskSnapshot = diskUsageMonitor .captureSnapshot (monitoredDirectory );
114+ DiskUsageMonitor .MultiDirectorySnapshot diskSnapshot = diskUsageMonitor .captureSnapshot ();
102115 diskSnapshots .add (diskSnapshot );
103- } catch (IOException e ) {
116+ } catch (Exception e ) {
104117 if (level != DiagnosticLevel .NONE ) {
105118 System .err .printf ("[%s] Failed to capture disk usage: %s%n" , phase , e .getMessage ());
106119 }
@@ -127,15 +140,15 @@ public void capturePostPhaseSnapshot(String phase) {
127140 snapshots .add (postSnapshot );
128141
129142 // Capture and log disk usage changes
130- if (monitoredDirectory != null ) {
143+ if (diskMonitorStarted ) {
131144 try {
132- DiskUsageMonitor .DiskUsageSnapshot postDiskSnapshot = diskUsageMonitor .captureSnapshot (monitoredDirectory );
145+ DiskUsageMonitor .MultiDirectorySnapshot postDiskSnapshot = diskUsageMonitor .captureSnapshot ();
133146 if (!diskSnapshots .isEmpty () && level != DiagnosticLevel .NONE ) {
134- DiskUsageMonitor .DiskUsageSnapshot preDiskSnapshot = diskSnapshots .get (diskSnapshots .size () - 1 );
147+ DiskUsageMonitor .MultiDirectorySnapshot preDiskSnapshot = diskSnapshots .get (diskSnapshots .size () - 1 );
135148 diskUsageMonitor .logDifference (phase , preDiskSnapshot , postDiskSnapshot );
136149 }
137150 diskSnapshots .add (postDiskSnapshot );
138- } catch (IOException e ) {
151+ } catch (Exception e ) {
139152 if (level != DiagnosticLevel .NONE ) {
140153 System .err .printf ("[%s] Failed to capture disk usage: %s%n" , phase , e .getMessage ());
141154 }
@@ -223,7 +236,7 @@ public SystemMonitor.SystemSnapshot getLatestSystemSnapshot() {
223236 /**
224237 * Gets the latest disk usage snapshot, or null if none captured
225238 */
226- public DiskUsageMonitor .DiskUsageSnapshot getLatestDiskSnapshot () {
239+ public DiskUsageMonitor .MultiDirectorySnapshot getLatestDiskSnapshot () {
227240 return diskSnapshots .isEmpty () ? null : diskSnapshots .get (diskSnapshots .size () - 1 );
228241 }
229242
@@ -298,17 +311,33 @@ public void logSummary() {
298311 public void printDiskStatistics (String label ) {
299312 // Disk usage summary
300313 if (!diskSnapshots .isEmpty ()) {
301- DiskUsageMonitor .DiskUsageSnapshot firstDisk = diskSnapshots .get (0 );
302- DiskUsageMonitor .DiskUsageSnapshot lastDisk = diskSnapshots .get (diskSnapshots .size () - 1 );
303- DiskUsageMonitor .DiskUsageSnapshot totalDisk = lastDisk .subtract (firstDisk );
314+ DiskUsageMonitor .MultiDirectorySnapshot firstDisk = diskSnapshots .get (0 );
315+ DiskUsageMonitor .MultiDirectorySnapshot lastDisk = diskSnapshots .get (diskSnapshots .size () - 1 );
316+ DiskUsageMonitor .MultiDirectorySnapshot totalDisk = lastDisk .subtract (firstDisk );
304317
305318 System .out .printf ("\n Disk Usage Summary %s:%n" , label );
306- System .out .printf (" Total Disk Used: %s%n" , DiskUsageMonitor .formatBytes (lastDisk .totalBytes ));
307- System .out .printf (" Total Files: %d%n" , lastDisk .fileCount );
308- System .out .printf (" Net Change: %s, %+d files%n" ,
309- DiskUsageMonitor .formatBytes (totalDisk .totalBytes ), totalDisk .fileCount );
319+
320+ // Print statistics for each monitored directory
321+ for (String dirLabel : lastDisk .snapshots .keySet ()) {
322+ DiskUsageMonitor .DiskUsageSnapshot lastSnap = lastDisk .get (dirLabel );
323+ DiskUsageMonitor .DiskUsageSnapshot totalSnap = totalDisk .get (dirLabel );
324+
325+ System .out .printf (" [%s]:%n" , dirLabel );
326+ System .out .printf (" Total Disk Used: %s%n" , DiskUsageMonitor .formatBytes (lastSnap .totalBytes ));
327+ System .out .printf (" Total Files: %d%n" , lastSnap .fileCount );
328+ if (totalSnap != null ) {
329+ System .out .printf (" Net Change: %s, %+d files%n" ,
330+ DiskUsageMonitor .formatBytes (totalSnap .totalBytes ), totalSnap .fileCount );
331+ }
332+ }
333+
334+ // Print overall totals
335+ System .out .printf (" [Overall Total]:%n" );
336+ System .out .printf (" Total Disk Used: %s%n" , DiskUsageMonitor .formatBytes (lastDisk .getTotalBytes ()));
337+ System .out .printf (" Total Files: %d%n" , lastDisk .getTotalFileCount ());
338+ System .out .printf (" Net Change: %s, %+d files%n" ,
339+ DiskUsageMonitor .formatBytes (totalDisk .getTotalBytes ()), totalDisk .getTotalFileCount ());
310340 }
311-
312341 }
313342
314343 /**
0 commit comments