Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge to Distribution #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Map;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

@ThreadSafe
public class Distribution
Expand Down Expand Up @@ -57,7 +58,12 @@ public synchronized void add(long value, long count)
digest.add(value, count);
total.add(value * count);
}

public synchronized void merge(Distribution distribution)
{
requireNonNull(distribution, "distribution is null");
total.merge(distribution.total);
digest.merge(distribution.digest);
}
@Managed
public synchronized double getMaxError()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.facebook.airlift.stats;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class TestDistribution
{
@Test
public void testMerge()
{
Distribution distribution1 = new Distribution();
Distribution distribution2 = new Distribution();

for (int i = 0; i < 10; i++) {
distribution1.add(i);
}

for (int i = 10; i < 20; i++) {
distribution2.add(i);
}

distribution1.merge(distribution2);

assertEquals(distribution1.getP50(), 10);
assertEquals(distribution1.getMin(), 0);
assertEquals(distribution1.getMax(), 19);
assertEquals(distribution1.getCount(), 20.0);
}

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Expected decayCounter to have alpha.*")
public void testMergeWithIncompatibleAlphaFails()
{
Distribution distribution1 = new Distribution(0.5);
Distribution distribution2 = new Distribution(0.7);

distribution1.merge(distribution2);
}
}