3939import java .util .concurrent .TimeoutException ;
4040import java .util .function .Supplier ;
4141
42+ /**
43+ * A comprehensive monitoring utility for tracking task execution metrics including
44+ * elapsed time, CPU usage, and memory consumption.
45+ *
46+ * <p>This class provides both instance-based and static methods for monitoring tasks.
47+ * It tracks the following metrics:
48+ * <ul>
49+ * <li><b>Elapsed Time:</b> Total execution time in seconds</li>
50+ * <li><b>CPU Usage:</b> Process CPU load at start, peak, and end of execution</li>
51+ * <li><b>Memory Usage:</b> Heap and non-heap memory consumption at start, peak, and end</li>
52+ * </ul>
53+ *
54+ * <p>The monitor uses a scheduled executor to periodically sample CPU and memory usage
55+ * (every {@value #INTERVAL} second) to capture peak values during task execution.
56+ *
57+ * <p><b>Example usage (instance-based):</b>
58+ * <pre>{@code
59+ * Monitor monitor = new Monitor("MyTask");
60+ * monitor.start();
61+ * // ... perform task ...
62+ * monitor.stop();
63+ * System.out.println(monitor); // Prints all metrics
64+ * }</pre>
65+ *
66+ * <p><b>Example usage (static convenience method):</b>
67+ * <pre>{@code
68+ * Monitor.runAndCount(() -> {
69+ * // ... perform task ...
70+ * }, "MyTask");
71+ * }</pre>
72+ *
73+ * <p>This class replaces the deprecated {@link Timer} class and provides enhanced
74+ * monitoring capabilities beyond simple time tracking.
75+ *
76+ * @see Timer
77+ */
4278public class Monitor {
4379
4480 private static final Logger logger = LogManager .getLogger (Monitor .class );
4581
4682 /**
47- * Monitoring interval in seconds
83+ * Monitoring interval in seconds for periodic CPU and memory sampling.
4884 */
4985 private static final int INTERVAL = 1 ;
5086
@@ -68,6 +104,11 @@ public class Monitor {
68104
69105 private boolean inCounting = false ;
70106
107+ /**
108+ * Creates a new Monitor with the given name.
109+ *
110+ * @param name the name of this monitor, used in output messages
111+ */
71112 public Monitor (String name ) {
72113 this .name = name ;
73114 this .osBean = (OperatingSystemMXBean ) ManagementFactory
@@ -80,6 +121,13 @@ public Monitor(String name) {
80121 });
81122 }
82123
124+ /**
125+ * Starts the monitoring process.
126+ *
127+ * <p>Records the start time and initial CPU/memory metrics, and begins
128+ * periodic sampling of CPU and memory usage to track peak values.
129+ * If the monitor is already running, this call has no effect.
130+ */
83131 public void start () {
84132 if (!inCounting ) {
85133 inCounting = true ;
@@ -92,6 +140,13 @@ public void start() {
92140 }
93141 }
94142
143+ /**
144+ * Stops the monitoring process.
145+ *
146+ * <p>Records the final elapsed time and end CPU/memory metrics, then
147+ * shuts down the periodic sampling scheduler. If the monitor is not
148+ * currently running, this call has no effect.
149+ */
95150 public void stop () {
96151 if (inCounting ) {
97152 inCounting = false ;
@@ -111,15 +166,34 @@ public void stop() {
111166 }
112167 }
113168
169+ /**
170+ * Returns the total elapsed time in seconds.
171+ *
172+ * @return the elapsed time in seconds as a floating-point number
173+ */
114174 public float inSecond () {
115175 return elapsedTime / 1000F ;
116176 }
117177
178+ /**
179+ * Clears all monitoring data and resets the counting state.
180+ *
181+ * <p>This resets the elapsed time to zero and marks the monitor as not counting.
182+ */
118183 public void clear () {
119184 elapsedTime = 0 ;
120185 inCounting = false ;
121186 }
122187
188+ /**
189+ * Returns a string representation of the monitoring results.
190+ *
191+ * <p>The output includes the monitor name, elapsed time, CPU usage statistics
192+ * (start, peak, and end percentages), and memory usage statistics (start, peak,
193+ * and end in megabytes).
194+ *
195+ * @return a formatted string containing all monitoring metrics
196+ */
123197 @ Override
124198 public String toString () {
125199 StringBuilder sb = new StringBuilder ();
@@ -157,10 +231,16 @@ private long getMemoryUsedMB() {
157231 }
158232
159233 /**
160- * Runs a task, log the elapsed time, and return the result.
234+ * Runs a task, monitors its execution, logs the results, and returns the task's result.
235+ *
236+ * <p>This is a convenience method that creates a Monitor, starts it, executes the task,
237+ * stops monitoring, and logs the results at the specified logging level.
161238 *
162- * @param task task to be executed
163- * @param taskName name of the task
239+ * @param <T> the type of result returned by the task
240+ * @param task the task to be executed
241+ * @param taskName the name of the task for logging purposes
242+ * @param level the logging level for the monitoring results
243+ * @return the result of the task execution
164244 */
165245 public static <T > T runAndCount (Supplier <T > task , String taskName , Level level ) {
166246 logger .info ("[{}] starts ..." , taskName );
@@ -173,15 +253,28 @@ public static <T> T runAndCount(Supplier<T> task, String taskName, Level level)
173253 }
174254
175255 /**
176- * Runs a task and log the elapsed time .
256+ * Runs a task, monitors its execution, and logs the results at INFO level .
177257 *
178- * @param task task to be executed
179- * @param taskName taskName of the task
258+ * <p>This is a convenience method that creates a Monitor, starts it, executes the task,
259+ * stops monitoring, and logs the results at INFO level.
260+ *
261+ * @param task the task to be executed
262+ * @param taskName the name of the task for logging purposes
180263 */
181264 public static void runAndCount (Runnable task , String taskName ) {
182265 runAndCount (task , taskName , Level .INFO );
183266 }
184267
268+ /**
269+ * Runs a task, monitors its execution, and logs the results at the specified level.
270+ *
271+ * <p>This is a convenience method that creates a Monitor, starts it, executes the task,
272+ * stops monitoring, and logs the results at the specified logging level.
273+ *
274+ * @param task the task to be executed
275+ * @param taskName the name of the task for logging purposes
276+ * @param level the logging level for the monitoring results
277+ */
185278 public static void runAndCount (Runnable task , String taskName , Level level ) {
186279 runAndCount (() -> {
187280 task .run ();
@@ -190,7 +283,14 @@ public static void runAndCount(Runnable task, String taskName, Level level) {
190283 }
191284
192285 /**
193- * Runs a task with given time budget.
286+ * Runs a task with a given time budget.
287+ *
288+ * <p>Executes the task in a separate thread with a timeout. If the task does not
289+ * complete within the specified time limit, the execution is terminated and the
290+ * program exits with status code 1.
291+ *
292+ * @param task the task to be executed
293+ * @param seconds the time budget in seconds
194294 */
195295 public static void runWithTimeout (Runnable task , long seconds ) {
196296 Duration timeout = Duration .ofSeconds (seconds );
@@ -207,4 +307,5 @@ public static void runWithTimeout(Runnable task, long seconds) {
207307 executor .shutdown ();
208308 }
209309 }
310+
210311}
0 commit comments