Skip to content

Commit

Permalink
batch increment for BucketCounter (#950)
Browse files Browse the repository at this point in the history
Add `increment` method to BucketCounter to allow for
batch increments of the same amount.
  • Loading branch information
brharrington authored Mar 22, 2022
1 parent ae85efc commit 68cd52b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ public static BucketCounter get(Registry registry, Id id, LongFunction<String> f
counter(f.apply(amount)).increment();
}

/**
* Update the counter associated with the amount by {@code n}.
*
* @param amount
* Amount to use for determining the bucket.
* @param n
* The delta to apply to the counter.
*/
public void increment(long amount, int n) {
counter(f.apply(amount)).increment(n);
}

/** Return the count for a given bucket. */
Counter counter(String bucket) {
return Utils.computeIfAbsent(counters, bucket, counterFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ public void basic() {
Assertions.assertEquals(3, sum(r, "test"));
}


@Test
public void increment() {
Registry r = new DefaultRegistry();
BucketCounter c = BucketCounter.get(
r, r.createId("test"), BucketFunctions.latency(4, TimeUnit.SECONDS));

c.record(TimeUnit.MILLISECONDS.toNanos(3750));
Assertions.assertEquals(1, r.counters().count());
Assertions.assertEquals(1, sum(r, "test"));

c.increment(TimeUnit.MILLISECONDS.toNanos(3750), 5);
Assertions.assertEquals(1, r.counters().count());
Assertions.assertEquals(6, sum(r, "test"));
}

}

0 comments on commit 68cd52b

Please sign in to comment.