#243 metrics module, make clock default and other setting parameters

This commit is contained in:
Calvin
2013-12-16 15:15:32 +08:00
parent 1da5fb3dba
commit d149265724
9 changed files with 67 additions and 67 deletions
@@ -5,19 +5,14 @@ import java.util.concurrent.atomic.AtomicLong;
import org.springside.modules.metrics.utils.Clock;
public class Counter {
public static Clock clock = Clock.DEFAULT;
private AtomicLong counter = new AtomicLong(0);
private Clock clock;
private long lastReportCount = 0L;
private long lastReportTime;
public Counter(Clock clock) {
if (clock == null) {
throw new IllegalArgumentException("Clock can't be null");
}
this.clock = clock;
public Counter() {
lastReportTime = clock.getCurrentTime();
}
@@ -3,16 +3,12 @@ package org.springside.modules.metrics;
import org.springside.modules.metrics.utils.Clock;
public class Execution {
public static Clock clock = Clock.DEFAULT;
private Counter counter;
private Histogram histogram;
private Clock clock;
public Execution(Clock clock, Double[] pcts) {
if (clock == null) {
throw new IllegalArgumentException("Clock can't be null ");
}
this.clock = clock;
counter = new Counter(clock);
public Execution(Double... pcts) {
counter = new Counter();
histogram = new Histogram(pcts);
}
@@ -11,10 +11,7 @@ public class Histogram {
private Double[] pcts;
private Object lock = new Object();
public Histogram(Double[] pcts) {
if (pcts == null) {
throw new IllegalArgumentException("Pcts can't be null");
}
public Histogram(Double... pcts) {
this.pcts = pcts;
}
@@ -7,14 +7,10 @@ import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springside.modules.metrics.utils.Clock;
public class MetricRegistry {
public static final MetricRegistry INSTANCE = new MetricRegistry();
private Clock defaultClock = Clock.DEFAULT;
private Double[] defaultPcts = new Double[] { 90d };
private ConcurrentMap<String, Object> metrics = new ConcurrentHashMap<String, Object>();
@@ -35,25 +31,33 @@ public class MetricRegistry {
if (metrics.containsKey(name)) {
return (Counter) metrics.get(name);
} else {
Counter counter = new Counter(defaultClock);
Counter counter = new Counter();
return (Counter) register(name, counter);
}
}
public Histogram histogram(String name) {
return histogram(name, defaultPcts);
}
public Histogram histogram(String name, Double... pcts) {
if (metrics.containsKey(name)) {
return (Histogram) metrics.get(name);
} else {
Histogram histogram = new Histogram(defaultPcts);
Histogram histogram = new Histogram(pcts);
return (Histogram) register(name, histogram);
}
}
public Execution execution(String name) {
return execution(name, defaultPcts);
}
public Execution execution(String name, Double... pcts) {
if (metrics.containsKey(name)) {
return (Execution) metrics.get(name);
} else {
Execution execution = new Execution(defaultClock, defaultPcts);
Execution execution = new Execution(pcts);
return (Execution) register(name, execution);
}
}
@@ -89,10 +93,6 @@ public class MetricRegistry {
return Collections.unmodifiableSortedMap(result);
}
public void setDefaultClock(Clock clock) {
this.defaultClock = clock;
}
public void setDefaultPcts(Double[] defaultPcts) {
this.defaultPcts = defaultPcts;
}
@@ -2,16 +2,23 @@ package org.springside.modules.metrics;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springside.modules.metrics.utils.Clock.MockedClock;
public class CounterTest {
private MockedClock clock = new MockedClock();
@Before
public void setup() {
Counter.clock = clock;
}
@Test
public void normal() {
MockedClock clock = new MockedClock();
Counter counter = new Counter(clock);
Counter counter = new Counter();
counter.inc(10);
counter.inc(20);
counter.inc(30);
@@ -31,8 +38,7 @@ public class CounterTest {
@Test
public void incAndDec() {
MockedClock clock = new MockedClock();
Counter counter = new Counter(clock);
Counter counter = new Counter();
counter.inc(20);
counter.inc();
@@ -47,8 +53,7 @@ public class CounterTest {
@Test
public void reset() {
MockedClock clock = new MockedClock();
Counter counter = new Counter(clock);
Counter counter = new Counter();
counter.inc(20);
clock.increaseTime(1000);
@@ -68,17 +73,11 @@ public class CounterTest {
@Test
public void empty() {
MockedClock clock = new MockedClock();
Counter counter = new Counter(clock);
Counter counter = new Counter();
clock.increaseTime(1000);
CounterMetric metric = counter.getMetric();
assertEquals(0, metric.count);
assertEquals(0d, metric.lastRate, 0);
}
@Test(expected = IllegalArgumentException.class)
public void checkArgument() {
Counter counter = new Counter(null);
}
}
@@ -11,7 +11,9 @@ public class ExecutionTest {
@Test
public void normal() {
MockedClock clock = new MockedClock();
Execution execution = new Execution(clock, new Double[] { 90d });
Execution.clock = clock;
Counter.clock = clock;
Execution execution = new Execution(new Double[] { 90d });
ExecutionTimer timer = execution.start();
clock.increaseTime(200);
@@ -8,7 +8,7 @@ public class HistogramTest {
@Test
public void normal() {
Histogram histogram = new Histogram(new Double[] { 90d, 95d });
Histogram histogram = new Histogram(90d, 95d);
for (int i = 1; i <= 100; i++) {
histogram.update(i);
@@ -37,7 +37,7 @@ public class HistogramTest {
@Test
public void fewData() {
Histogram histogram = new Histogram(new Double[] { 90d, 95d });
Histogram histogram = new Histogram(90d, 95d);
histogram.update(1);
HistogramMetric metric = histogram.getMetric();
@@ -58,7 +58,7 @@ public class HistogramTest {
@Test
public void emptyMesures() {
Histogram histogram = new Histogram(new Double[] { 90d, 95d });
Histogram histogram = new Histogram(90d, 95d);
HistogramMetric metric = histogram.getMetric();
@@ -68,14 +68,9 @@ public class HistogramTest {
assertEquals(0, metric.pcts.get(90d), 0);
}
@Test(expected = IllegalArgumentException.class)
public void checkArgument() {
Histogram histogram = new Histogram(null);
}
@Test()
public void emptyPcts() {
Histogram histogram = new Histogram(new Double[] {});
Histogram histogram = new Histogram();
for (int i = 1; i <= 3; i++) {
histogram.update(i);
}
@@ -83,5 +78,6 @@ public class HistogramTest {
HistogramMetric metric = histogram.getMetric();
assertEquals(3, metric.max);
assertTrue(metric.pcts.isEmpty());
assertNull(metric.pcts.get(90d));
}
}
@@ -5,7 +5,6 @@ import static org.junit.Assert.*;
import java.util.Map;
import org.junit.Test;
import org.springside.modules.metrics.utils.Clock.MockedClock;
public class MetricRegistryTest {
@@ -62,21 +61,12 @@ public class MetricRegistryTest {
}
@Test
public void updateDefault() {
// default clock
MockedClock clock = new MockedClock();
public void defaultPcts() {
MetricRegistry metricRegistry = new MetricRegistry();
metricRegistry.setDefaultClock(clock);
Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.new.counter"));
counter.inc(100000);
clock.increaseTime(50000);
assertEquals(2000, counter.getMetric().lastRate, 1);
// default pcts
metricRegistry.setDefaultPcts(new Double[] { 50d });
Histogram histogram = metricRegistry.histogram(MetricRegistry.name("UserService", "getUser.new.histogram"));
// set pcts 60,70
Histogram histogram = metricRegistry.histogram(MetricRegistry.name("UserService", "getUser.histogram.setPcts"),
60d, 70d);
for (int i = 1; i <= 100; i++) {
histogram.update(i);
@@ -84,6 +74,30 @@ public class MetricRegistryTest {
HistogramMetric metric = histogram.getMetric();
assertEquals(60, metric.pcts.get(60d), 0);
assertEquals(70, metric.pcts.get(70d), 0);
// default 90
Histogram histogramWithDefaultPcts = metricRegistry.histogram(MetricRegistry.name("UserService",
"getUser.histogram.default"));
for (int i = 1; i <= 100; i++) {
histogramWithDefaultPcts.update(i);
}
metric = histogramWithDefaultPcts.getMetric();
assertEquals(90, metric.pcts.get(90d), 0);
// new default 50
metricRegistry.setDefaultPcts(new Double[] { 50d });
Histogram histogramWithNewDefaultPcts = metricRegistry.histogram(MetricRegistry.name("UserService",
"getUser.histogram.newDefault"));
for (int i = 1; i <= 100; i++) {
histogramWithNewDefaultPcts.update(i);
}
metric = histogramWithNewDefaultPcts.getMetric();
assertEquals(50, metric.pcts.get(50d), 0);
assertNull(metric.pcts.get(90d));
}
@@ -42,7 +42,8 @@ public class ReporterTest {
private void runReport(Reporter reporter) {
MetricRegistry metricRegistry = new MetricRegistry();
MockedClock clock = new MockedClock();
metricRegistry.setDefaultClock(clock);
Counter.clock = clock;
Execution.clock = clock;
// counter
Counter counter = metricRegistry.counter(MetricRegistry.name("UserService", "getUser.counter"));