public final class Stopwatch extends Object
HiLog
to save the records in debug-level logs.
Example code:
Stopwatch stopwatch = new Stopwatch(label, "main task");
// Records the completion time of subtask A split from the main task.
stopwatch.split("sub task A");
// Records the completion time of subtask B split from the main task.
stopwatch.split("sub task B");
// Records the completion time of subtask C split from the main task.
stopwatch.split("sub task C");
stopwatch.writeLog();
The following records are written into the logs:
8186 D 00000/StopwatchDemo: main task: begin 8186 D 00000/StopwatchDemo: main task: 11 ms, 11%, sub task A 8186 D 00000/StopwatchDemo: main task: 76 ms, 76%, sub task B 8186 D 00000/StopwatchDemo: main task: 13 ms, 13%, sub task C 8186 D 00000/StopwatchDemo: main task: end 100 ms
| 构造器 | 说明 |
|---|---|
Stopwatch(HiLogLabel label,
String taskName) |
Creates a stopwatch logger.
|
public Stopwatch(HiLogLabel label, String taskName)
You are advised to create one stopwatch logger for each main task.
label - Indicates the HiLog label.taskName - Indicates the task name.HiLogLabelpublic void split(String subtaskName)
This method is called after a subtask is executed. Stopwatch#writeLog() uses the recorded time to
calculate the subtask execution duration.
subtaskName - Indicates the subtask name.public void reset()
If a main task needs to be repeatedly executed, call this method to reset the stopwatch logger. After the reset, all of the history records will be cleared.
public void writeLog()
HiLog.
Note: This method does not clear history records, which will also be written into the logs.
To clear the history records, call Stopwatch#reset().