@@ -73,7 +73,7 @@ public final class DefaultPerformanceMetrics implements PerformanceMetrics, Auto
7373 private final Duration interval ;
7474 private final TimeUnit latencyCollectionTimeUnit ;
7575 private final AtomicBoolean firstReport = new AtomicBoolean (false );
76- private final AtomicBoolean closed = new AtomicBoolean (false );
76+ private final AtomicBoolean started = new AtomicBoolean (false );
7777 private final AtomicReference <Histogram > consumedLatency , confirmedLatency ;
7878 private final MetricsFormatter formatter ;
7979
@@ -162,27 +162,29 @@ private static Runnable wrapInCatch(Runnable runnable) {
162162
163163 @ Override
164164 public void start () {
165- startTime .set (System .nanoTime ());
166- lastTick .set (startTime .get ());
167- startTimeForTotal .set (startTime .get ());
168-
169- scheduledExecutorService .scheduleAtFixedRate (wrapInCatch (() -> {
170- if (this .closed .get ()) {
171- return ;
172- }
173- if (noActivity ()) {
174- this .publishedRate .accumulate (0 );
175- this .confirmedRate .accumulate (0 );
176- this .nackedRate .accumulate (0 );
177- this .returnedRate .accumulate (0 );
178- this .receivedRate .accumulate (0 );
179- this .confirmedLatency .set (histogram ());
180- this .consumedLatency .set (histogram ());
181- } else {
182- metrics (System .nanoTime ());
183- }
184-
185- }), interval .getSeconds (), interval .getSeconds (), TimeUnit .SECONDS );
165+ if (this .started .compareAndSet (false , true )) {
166+ startTime .set (System .nanoTime ());
167+ lastTick .set (startTime .get ());
168+ startTimeForTotal .set (startTime .get ());
169+
170+ scheduledExecutorService .scheduleAtFixedRate (wrapInCatch (() -> {
171+ if (!this .started .get ()) {
172+ return ;
173+ }
174+ if (noActivity ()) {
175+ this .publishedRate .accumulate (0 );
176+ this .confirmedRate .accumulate (0 );
177+ this .nackedRate .accumulate (0 );
178+ this .returnedRate .accumulate (0 );
179+ this .receivedRate .accumulate (0 );
180+ this .confirmedLatency .set (histogram ());
181+ this .consumedLatency .set (histogram ());
182+ } else {
183+ metrics (System .nanoTime ());
184+ }
185+
186+ }), interval .getSeconds (), interval .getSeconds (), TimeUnit .SECONDS );
187+ }
186188 }
187189
188190 void metrics (long currentTime ) {
@@ -211,7 +213,7 @@ void metrics(long currentTime) {
211213 this .confirmedLatency .set (histogram ());
212214 this .consumedLatency .set (histogram ());
213215
214- if (! this .closed .get ()) {
216+ if (this .started .get ()) {
215217 if (this .firstReport .compareAndSet (false , true )) {
216218 this .formatter .header ();
217219 }
@@ -320,9 +322,15 @@ public void resetGlobals() {
320322
321323 @ Override
322324 public void close () {
323- if (this .closed .compareAndSet (false , true )) {
325+ if (this .started .compareAndSet (false , true )) {
324326 this .scheduledExecutorService .shutdownNow ();
325327 printFinal ();
326328 }
327329 }
330+
331+ // for testing
332+ void started (boolean value ) {
333+ this .started .set (value );
334+ }
335+
328336}
0 commit comments