How is Stopwatch different from JAMon?
  • it is able to persists its data. By default it uses in-memory HSQL database but any other JDBC compliant database can be used.
  • it is modular. It can use custom engines and storages so different things can be measured (memory, load, etc).
  • it is Static. The only class one ever use is the static Stopwatch. There is no new object instance for each measurement.
  • it has JMX management interface to remotely start and stop Stopwatch as well as obtain reports.
[top]

Is Stopwatch thread safe?
The short answer is "Yes". The longer and more correct answer is "It depends". Stopwatch consist of single static class (com.commsen.stopwatch.Stopwatch) which methods are not synchronized and simply forward calls to the currently used engine. It is up to the engine to decide whether method invocations are synchronized. Currently all engines provided by default with Stopwatch synchronize calls.
[top]

Why measuring "nothing" does not report 0ms?
Executing this code:
for (int i = 0; i < 1000; i++) {
	long id = Stopwatch.start("g", "l");
	Stopwatch.stop(id);
}
Report report = Stopwatch.getSingleReport("g", "l");
System.out.print(report);
				
one may expect to see:
Group="g"  Label="l"  Count=1000  Min=0ms  Avg=0ms  Max=0ms  Total=0ms
				
but instead the output will be somethimg similar to:
Group="g"  Label="l"  Count=1000  Min=0ms  Avg=0ms  Max=1ms  Total=8mx
				
The reason is that running Stopwatch itself takes some milliseconds. An efforts have been made to minimize this overhead (on my machine it's ~0.8%) since it can not be completely eliminated.
[top]