Skip to content

Commit

Permalink
FastTimer to return rates if queried at the same second as update
Browse files Browse the repository at this point in the history
  • Loading branch information
dlg99 committed Dec 13, 2023
1 parent 5be03cf commit 2681b63
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,9 @@ private int getNow(int hash) {
*/
public double getRate(int seconds) {
seconds = Math.min(seconds, timeWindow - 2);
int t = getNow(getHash()) - 1; // start from last completed second
int secFrom = t - seconds;
int t = getNow(getHash());
// start from last completed second
int secFrom = t - seconds - 1;
long sum = 0;
for (int h = 0; h < HASH_SIZE; h++) {
for (int i = t; i > secFrom; i--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.bookkeeper.stats.codahale;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.codahale.metrics.Snapshot;
import java.util.ArrayList;
Expand Down Expand Up @@ -52,6 +53,17 @@ protected int getTime() {
};
}

@Test
public void testMeanRate() {
FastTimer t = getMockedFastTimer(1, FastTimer.Buckets.fine);

t.update(10, TimeUnit.NANOSECONDS);
assertTrue("should calculate mean before advancing time", t.getMeanRate() > 0);

incSec();
assertTrue("should calculate mean after advancing time", t.getMeanRate() > 0);
}

@Test
public void testBuckets() {
FastTimer t = new FastTimer(1, FastTimer.Buckets.fine);
Expand Down

0 comments on commit 2681b63

Please sign in to comment.