public class Metric extends Object
| Constructor and Description |
|---|
Metric(boolean enabled)
Create a metric tracking object and start the nanosecond timer.
|
| Modifier and Type | Method and Description |
|---|---|
void |
checkpoint(String description,
Object... args)
Set a mark for timing.
|
long |
done()
Indicate we are done (stop the timer).
|
long |
done(String description,
Object... args)
Set a final mark for timing and stop the timer.
|
long |
elapsedMillis()
Find out how many milliseconds have elapsed since this timer was started.
|
long |
elapsedNanos()
Find out how many nanoseconds have elapsed since this timer was started.
|
String |
getMessage()
Construct and return a message based on the timing and checkpoints.
|
void |
printMessage(StringBuilder buf)
Construct and print a message based on the timing and checkpoints.
|
public Metric(boolean enabled)
Create a metric tracking object and start the nanosecond timer. Times
are obtained using System.nanoTime(). The canonical way to use
this looks something like this:
Metric metric = new Metric(log.isDebugEnabled);
...
metric.checkpoint("received");
...
metric.checkpoint("processed");
...
metric.done("sent");
...
if (log.isDebugEnabled()) {
log.debug("Processed: " + metric.getMessage());
}
enabled - true if timings will be taken, false to
optimize out the time trackingpublic long elapsedMillis()
false was
passed in the constructorpublic long elapsedNanos()
false was
passed in the constructorpublic void checkpoint(String description, Object... args)
checkpoint("sent", 25, 3) will result in a
description "sent[25,3]". The string evaluation and concatenation is
lazy, and won't be done if this metric is disabled.description - a label for this mark; may not be null; spaces
and tabs will be converted to underscoresargs - additional information to append to the description; will
print "null" if null; evaluated with String.valueOf() lazily
and sanitized of most non-alphanumeric characterspublic long done(String description, Object... args)
description - a label for this mark; may not be null; spaces
and tabs will be converted to underscoresfalse was passed in the constructorpublic long done()
false was passed in the constructorpublic String getMessage()
This will automatically call the done() method to stop the timer if you haven't already done so.
"metricsDisabled"
if false was passed in the constructor.printMessage(StringBuilder)public void printMessage(StringBuilder buf)
"metricsDisabled" will be printed if false was
passed in the constructor.
This will automatically call the done() method to stop the timer if you haven't already done so.
buf - the message will be printed to this buildergetMessage()Copyright © 2020. All rights reserved.