Hierarchical timer for measuring how long an operation takes, with nested child timers and named lap checkpoints. A stopwatch nests under whatever stopwatch is already running on the current thread, via a thread-local stack, building a tree that mirrors the operations being timed. Used throughout the platform to profile slow requests and background jobs, and backs the console.time and console.timeEnd timers exposed to app scripts. Implements AutoCloseable so a stopwatch started in a try-with-resources block is stopped automatically when the block exits. Serialised as a "stopwatch" element in the server monitoring output.
Group: Server Monitoring
Implements: AutoCloseable
Properties
| Property | Returns | Description |
|---|---|---|
| children | List<ChildStopWatch> | The child stopwatches nested under this one, in the order they were started. Null if none have been added. |
| durationMs | Double | The elapsed duration in milliseconds, as a boxed value that is null while the stopwatch is still running. |
| laps | List<Lap> | The named lap checkpoints recorded against this stopwatch, in the order they were recorded. Null if none have been recorded. |
| name | String | The name this stopwatch was started or last modified with, used to identify it in logs and in the aggregated and tree output. |
Methods
close() · toAggregatedList() · findChild(String name) · stop() · startTime() · durationMillis() · getDurationMs() · showOutput(long rootStartTime, long startedAt, double duration, String indent, PrintWriter pw) · getChildren() · getLaps() · getName() · lap(int i) · lap(Object lapName) · lapEvery(String name, int i) · withBranch(Branch b) · withWarnThreshhold(int i) · withInfoThreshhold(int i) · addChild(KStopWatch ksw)
close()
Returns: void
Stops this stopwatch. Called automatically when this stopwatch is used as a resource in a try-with-resources block.
toAggregatedList()
Returns: List<StopWatchItem>
Flattens this stopwatch and all of its descendants into a list of totals grouped by name, summing the duration of every stopwatch and child that shares the same name, sorted by total duration descending.
findChild(String name)
Returns: KStopWatch
Searches this stopwatch and its descendants, depth-first, for one with the given name.
| Parameter | Description |
|---|---|
name | the name to search for |
stop()
Returns: double
Stops this stopwatch, records its elapsed duration, and if it has a parent restores that parent as the current stopwatch for this thread.
startTime()
Returns: long
The time this stopwatch was started, in nanoseconds as returned by System.nanoTime. Zero if the source logger did not have info logging enabled when the stopwatch was created.
durationMillis()
Returns: double
The elapsed duration between this stopwatch's start and stop times, in milliseconds. Zero if the stopwatch has not been stopped yet, or if its start time was not captured.
getDurationMs()
Returns: Double
The elapsed duration in milliseconds, as a boxed value that is null while the stopwatch is still running.
showOutput(long rootStartTime, long startedAt, double duration, String indent, PrintWriter pw)
Returns: void
Writes a human-readable breakdown of this stopwatch and its laps and children to the given writer, indented to show the tree structure. Stopwatches and children with a zero duration are skipped.
| Parameter | Description |
|---|---|
rootStartTime | the start time in nanoseconds of the top-level stopwatch, used to compute relative offsets |
startedAt | the start time in nanoseconds of this stopwatch |
duration | this stopwatch's elapsed duration in milliseconds |
indent | the indentation prefix to write before this stopwatch's line |
pw | the writer to append the output to |
getChildren()
Returns: List<ChildStopWatch>
The child stopwatches nested under this one, in the order they were started. Null if none have been added.
getLaps()
Returns: List<Lap>
The named lap checkpoints recorded against this stopwatch, in the order they were recorded. Null if none have been recorded.
getName()
Returns: String
The name this stopwatch was started or last modified with, used to identify it in logs and in the aggregated and tree output.
lap(int i)
Returns: void
Records a lap checkpoint named with the given number, as a string.
| Parameter | Description |
|---|---|
i | the number to use as the lap's name |
lap(Object lapName)
Returns: void
Records a lap checkpoint against this stopwatch, if logging is currently enabled, using the given value's string form as its name.
| Parameter | Description |
|---|---|
lapName | the value whose string form is used to name the lap |
lapEvery(String name, int i)
Returns: void
Records a lap checkpoint named as given, but only on every (i + 1)th call, resetting the internal counter each time a lap is recorded.
| Parameter | Description |
|---|---|
name | the name to record the lap under when the counter trips |
i | the number of calls to skip between recorded laps |
withBranch(Branch b)
Returns: KStopWatch
Appends the given branch's repository name to this stopwatch's name, if the branch is not null.
| Parameter | Description |
|---|---|
b | the branch whose repository name is appended to this stopwatch's name, or null to leave the name unchanged |
withWarnThreshhold(int i)
Returns: KStopWatch
Sets the duration threshold in milliseconds above which this stopwatch's timing is reported at warn level.
| Parameter | Description |
|---|---|
i | the warn threshold in milliseconds |
withInfoThreshhold(int i)
Returns: KStopWatch
Sets the duration threshold in milliseconds above which this stopwatch's timing is reported at info level.
| Parameter | Description |
|---|---|
i | the info threshold in milliseconds |
addChild(KStopWatch ksw)
Returns: void
Registers the given stopwatch as a child of this one, recording the time it was added. If more than 1000 children have already been tracked, the existing children are cleared first to bound memory use.
| Parameter | Description |
|---|---|
ksw | the child stopwatch to register |