A "tag" is an unique grouping identifier. A "message" can be anything you like; it just travels with the StopWatch and is output with the toString method.
Tags must not contain whitespace, forward slash or commas. All other characters are allowed.
Most of the StopWatch methods return a reference to itself for easy chaining.
Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.
When testing code that uses this class, use {@link #createUnstarted(Ticker)} or {@link #createStarted(Ticker)} tosupply a fake or mock ticker. This allows you to simulate any valid behavior of the stopwatch.
Note: This class is not thread-safe. @author Kevin Bourrillion @since 10.0
Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.
When testing code that uses this class, use the {@linkplain #Stopwatch(Ticker) alternate constructor} to supply a fake or mock ticker. This allows you to simulate any valid behavior of the stopwatch.
Note: This class is not thread-safe. @author Kevin Bourrillion @since Guava release 10
Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.
When testing code that uses this class, use {@link #createUnstarted(Ticker)} or {@link #createStarted(Ticker)} tosupply a fake or mock ticker. This allows you to simulate any valid behavior of the stopwatch.
Note: This class is not thread-safe. @author Kevin Bourrillion @since 10.0
The value on the stopwatch may be inspected at any time using the {@link #toString} and {@link #toValue} methods.
Example 2
To time the various steps in a long startup or initialization task, your code may take the form:
Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); //..perform operation 1 log("Step 1 completed " + stopwatch + " after start."); //perform operation 2 log("Step 2 completed " + stopwatch + " after start."); //perform the last operation, operation 3 stopwatch.stop(); log("Final Step 3 completed " + stopwatch + " after start") ;
Implementation Note: This class uses {@link System#nanoTime()}, not {@link System#currentTimeMillis()}.
Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.
When testing code that uses this class, use the {@linkplain #Stopwatch(Ticker) alternate constructor} to supply a fake or mock ticker. This allows you to simulate any valid behavior of the stopwatch.
Note: This class is not thread-safe. @author Kevin Bourrillion @since 10.0
StopWatch
provides a convenient API for timings.
To start the watch, call {@link #start()}. At this point you can:
It is intended that the output methods {@link #toString()} and {@link #getTime()}should only be called after stop, split or suspend, however a suitable result will be returned at other points.
NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start, resume before suspend or unsplit before split.
1. split(), suspend(), or stop() cannot be invoked twice
2. unsplit() may only be called if the watch has been split()
3. resume() may only be called if the watch has been suspend()
4. start() cannot be called twice without calling reset()
StopWatch
provides a convenient API for timings.
To start the watch, call {@link #start()}. At this point you can:
It is intended that the output methods {@link #toString()} and {@link #getTime()} should only be called after stop,split or suspend, however a suitable result will be returned at other points.
NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start, resume before suspend or unsplit before split.
1. split(), suspend(), or stop() cannot be invoked twice
2. unsplit() may only be called if the watch has been split()
3. resume() may only be called if the watch has been suspend()
4. start() cannot be called twice without calling reset()
This class is not thread-safe
@author Apache Software Foundation @since 2.0 @version $Id: StopWatch.java 959577 2010-07-01 09:48:23Z sebb $StopWatch
provides a convenient API for timings.
To start the watch, call {@link #start()}. At this point you can:
It is intended that the output methods {@link #toString()} and {@link #getTime()}should only be called after stop, split or suspend, however a suitable result will be returned at other points.
NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start, resume before suspend or unsplit before split.
1. split(), suspend(), or stop() cannot be invoked twice
2. unsplit() may only be called if the watch has been split()
3. resume() may only be called if the watch has been suspend()
4. start() cannot be called twice without calling reset()
Conceals use of System.currentTimeMillis()
, improving the readability of application code and reducing the likelihood of calculation errors.
Note that this object is not designed to be thread-safe and does not use synchronization.
This class is normally used to verify performance during proof-of-concepts and in development, rather than as part of production applications. @author kimchy (Shay Banon)
The watch can be started, stopped and zeroed and can be queried for elapsed running time. The watch accumulates elapsed time over starts and stops such that only the time actually spent running is recorded. If the watch is zeroed, then the accumulated time is discarded and the watch starts again with zero acumulated time.
@author boucherb@users @version 1.7.2 @since 1.7.2
The watch can be started, stopped and zeroed and can be queried for elapsed running time. The watch accumulates elapsed time over starts and stops such that only the time actually spent running is recorded. If the watch is zeroed, then the accumulated time is discarded and the watch starts again with zero acumulated time.
@author boucherb@users @version 1.7.2 @since 1.7.2
Stopwatch stopwatch = SimonManager.getStopwatch("all-stmts"); Statement stmt = c.createStatement(); stopwatch.start(stmt); rs = stmt.executeQuery("select * from foo"); while (rs.next()) { Statement stmt2 = c.createStatement(); stopwatch.start(stmt2); stmt2.execute("delete from bar where foo_id=" + rs.getInt("id")); stopwatch.stop(stmt2); stmt2.close(); } stopwatch.stop(stmt); stmt.close();This is not quite a real-life example, but it should demonstrate the point. Better example is measuring HTTP session life-spans. Session is typically started and ended in different requests - those are processed in random threads. Hence session itself should be used as the split-key. Keyed start/stop is always safe if one split is measured for the object used as the key at a time (which is mostly exactly what is needed). This method can be used anytime even for single-threaded environment if lifespan of a particular object is measured as the key nicely indicate directly in the code what is measured. Using {@code this} as a key is not always safe as it might seem. While sometimes it is exactlywhat is needed it should not be used if {@code this} is not related to the time split. Ifmethod execution time is measured default start/stop should be used. When the method is later moved into different helper class and it is called from multiple threads it is still valid, while using {code this} would cause troubles. On the other hand, keyed methods are preferred if there is a clear relation between the key object and the split - best case is measuring life-span of the object.
StopWatch stopWatch = new StopWatch(); try { ...code being timed... log.info(stopWatch.stop("methodBeingTimed.success")); } catch (Exception e) { log.error(stopWatch.stop("methodBeingTimed.fail"), e); }Note that a StopWatch is reusable. That is, you can call start() and stop() in succession and the getElapsedTime() method will refer to the time since the most recent start() call. In general, most clients will find it simpler and cleaner to use the {@link LoggingStopWatch} class or one of itssubclasses in preference to this class. @author Alex Devine
Conceals use of System.currentTimeMillis()
, improving the readability of application code and reducing the likelihood of calculation errors.
Note that this object is not designed to be thread-safe and does not use synchronization.
This class is normally used to verify performance during proof-of-concepts and in development, rather than as part of production applications. @author Rod Johnson @author Juergen Hoeller @author Sam Brannen @since May 2, 2001
@author Marcel Ruff
Conceals use of System.currentTimeMillis()
, improving the readability of application code and reducing the likelihood of calculation errors.
Note that this object is not designed to be thread-safe and does not use synchronization.
This class is normally used to verify performance during proof-of-concepts and in development, rather than as part of production applications. @author Rod Johnson @author Juergen Hoeller @since May 2, 2001
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|