diff --git a/modules/extension/src/main/java/org/springside/modules/metrics/Counting.java b/modules/extension/src/main/java/org/springside/modules/metrics/Counting.java new file mode 100644 index 00000000..d5dafb91 --- /dev/null +++ b/modules/extension/src/main/java/org/springside/modules/metrics/Counting.java @@ -0,0 +1,5 @@ +package org.springside.modules.metrics; + +public interface Counting { + +} diff --git a/modules/metrics/pom.xml b/modules/metrics/pom.xml new file mode 100644 index 00000000..fb3a7b3e --- /dev/null +++ b/modules/metrics/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.springside + springside-parent + 4.1.1-SNAPSHOT + ../parent/ + + springside-metrics + jar + Springside :: Module :: Metrics + + + + org.slf4j + slf4j-api + + + + + junit + junit + + + + org.mockito + mockito-core + + + + ch.qos.logback + logback-classic + test + + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + + + + \ No newline at end of file diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/Counter.java b/modules/metrics/src/main/java/org/springside/modules/metrics/Counter.java new file mode 100644 index 00000000..02f136a1 --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/Counter.java @@ -0,0 +1,71 @@ +package org.springside.modules.metrics; + +import java.util.concurrent.atomic.AtomicLong; + +import org.springside.modules.metrics.utils.Clock; + +public class Counter { + + private AtomicLong counter = new AtomicLong(0); + private long lastReportCounter = 0L; + + private Clock clock; + private long startTime; + private long lastReportTime; + + public Counter() { + this(Clock.DEFAULT); + } + + public Counter(Clock clock) { + this.clock = clock; + startTime = clock.getCurrentTime(); + lastReportTime = startTime; + } + + public void inc() { + counter.incrementAndGet(); + } + + public void inc(long n) { + counter.addAndGet(n); + } + + public void dec() { + counter.decrementAndGet(); + } + + public void dec(long n) { + counter.addAndGet(-n); + } + + public long reset() { + return counter.getAndSet(0); + } + + public long getCount() { + return counter.get(); + } + + public double getLastRate() { + long currentCounter = counter.get(); + long reportTime = clock.getCurrentTime(); + + double rate = 0.0; + if (currentCounter != 0) { + rate = ((currentCounter - lastReportCounter) / (reportTime - lastReportTime)) * 1000; + } + lastReportCounter = currentCounter; + lastReportTime = reportTime; + + return rate; + } + + public double getMeanRate() { + if (counter.get() == 0) { + return 0.0; + } else { + return (counter.get() / (clock.getCurrentTime() - startTime)) * 1000; + } + } +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/Execution.java b/modules/metrics/src/main/java/org/springside/modules/metrics/Execution.java new file mode 100644 index 00000000..d5bd8a2a --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/Execution.java @@ -0,0 +1,61 @@ +package org.springside.modules.metrics; + +import org.springside.modules.metrics.utils.Clock; + +public class Execution { + + public static class ExecutiomTimer { + private final Execution execution; + private final Clock clock; + private final long startTime; + + private ExecutiomTimer(Execution execution, Clock clock) { + this.execution = execution; + this.clock = clock; + this.startTime = clock.getCurrentTime(); + } + + public long stop() { + final long elapsed = clock.getCurrentTime() - startTime; + execution.update(elapsed); + return elapsed; + } + } + + private Counter counter; + private Histogram histogram; + private Clock clock; + + public Execution() { + this(Clock.DEFAULT); + } + + public Execution(Clock clock) { + this.clock = clock; + } + + public ExecutiomTimer start() { + return new ExecutiomTimer(this, clock); + } + + private void update(long duration) { + histogram.update(duration); + counter.inc(); + } + + public HistogramSnapshot getSnapshot() { + return histogram.getSnapshot(); + } + + public long getCount() { + return counter.getCount(); + } + + public double getLastRate() { + return counter.getLastRate(); + } + + public double getMeanRate() { + return counter.getMeanRate(); + } +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/Histogram.java b/modules/metrics/src/main/java/org/springside/modules/metrics/Histogram.java new file mode 100644 index 00000000..34c11f3a --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/Histogram.java @@ -0,0 +1,65 @@ +package org.springside.modules.metrics; + +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +import org.springside.modules.metrics.utils.Clock; + +public class Histogram { + + // allow for this many duplicate ticks before overwriting measurements + private static final int COLLISION_BUFFER = 256; + // only trim on updating once every N + private static final int TRIM_THRESHOLD = 256; + + private final Clock clock; + private final ConcurrentSkipListMap measurements; + private final long window; + private final AtomicLong lastTick; + private final AtomicLong count; + + public Histogram(long window, TimeUnit windowUnit) { + this(window, windowUnit, Clock.DEFAULT); + } + + public Histogram(long window, TimeUnit windowUnit, Clock clock) { + this.clock = clock; + this.measurements = new ConcurrentSkipListMap(); + this.window = windowUnit.toMillis(window) * COLLISION_BUFFER; + this.lastTick = new AtomicLong(); + this.count = new AtomicLong(); + } + + public void update(long value) { + if ((count.incrementAndGet() % TRIM_THRESHOLD) == 0) { + trim(); + } + measurements.put(getTick(), value); + } + + public HistogramSnapshot getSnapshot() { + trim(); + return new HistogramSnapshot(measurements.values()); + } + + /** + * TODO:?? + */ + private long getTick() { + for (;;) { + final long oldTick = lastTick.get(); + final long tick = clock.getCurrentTime() * COLLISION_BUFFER; + + final long newTick = tick > oldTick ? tick : oldTick + 1; + if (lastTick.compareAndSet(oldTick, newTick)) { + return newTick; + } + } + } + + private void trim() { + measurements.headMap(getTick() - window).clear(); + } + +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/HistogramSnapshot.java b/modules/metrics/src/main/java/org/springside/modules/metrics/HistogramSnapshot.java new file mode 100644 index 00000000..268b5d8b --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/HistogramSnapshot.java @@ -0,0 +1,115 @@ +package org.springside.modules.metrics; + +import static java.lang.Math.*; + +import java.util.Arrays; +import java.util.Collection; + +public class HistogramSnapshot { + + private final long[] values; + + /** + * Create a new {@link HistogramSnapshot} with the given values. + * + * @param values an unordered set of values in the reservoir + */ + public HistogramSnapshot(Collection values) { + final Object[] copy = values.toArray(); + this.values = new long[copy.length]; + for (int i = 0; i < copy.length; i++) { + this.values[i] = (Long) copy[i]; + } + Arrays.sort(this.values); + } + + /** + * Create a new {@link HistogramSnapshot} with the given values. + * + * @param values an unordered set of values in the reservoir + */ + public HistogramSnapshot(long[] values) { + this.values = Arrays.copyOf(values, values.length); + Arrays.sort(this.values); + } + + /** + * Returns the value at the given quantile. + * + * @param quantile a given quantile, in {@code [0..1]} + * @return the value in the distribution at {@code quantile} + */ + public double getValue(double quantile) { + if ((quantile < 0.0) || (quantile > 1.0)) { + throw new IllegalArgumentException(quantile + " is not in [0..1]"); + } + + if (values.length == 0) { + return 0.0; + } + + final double pos = quantile * (values.length + 1); + + if (pos < 1) { + return values[0]; + } + + if (pos >= values.length) { + return values[values.length - 1]; + } + + final double lower = values[(int) pos - 1]; + final double upper = values[(int) pos]; + return lower + ((pos - floor(pos)) * (upper - lower)); + } + + /** + * Returns the value at the 99th percentile in the distribution. + * + * @return the value at the 99th percentile + */ + public double get90th() { + return getValue(0.9); + } + + /** + * Returns the highest value in the snapshot. + * + * @return the highest value + */ + public long getMax() { + if (values.length == 0) { + return 0; + } + return values[values.length - 1]; + } + + /** + * Returns the lowest value in the snapshot. + * + * @return the lowest value + */ + public long getMin() { + if (values.length == 0) { + return 0; + } + return values[0]; + } + + /** + * Returns the arithmetic mean of the values in the snapshot. + * + * @return the arithmetic mean + */ + public double getMean() { + if (values.length == 0) { + return 0; + } + + double sum = 0; + for (long value : values) { + sum += value; + } + return sum / values.length; + } +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/MetricRegistry.java b/modules/metrics/src/main/java/org/springside/modules/metrics/MetricRegistry.java new file mode 100644 index 00000000..b2992c1b --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/MetricRegistry.java @@ -0,0 +1,5 @@ +package org.springside.modules.metrics; + +public class MetricRegistry { + +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/report/ConsoleReporter.java b/modules/metrics/src/main/java/org/springside/modules/metrics/report/ConsoleReporter.java new file mode 100644 index 00000000..68b4a7ab --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/report/ConsoleReporter.java @@ -0,0 +1,41 @@ +package org.springside.modules.metrics.report; + +import java.io.PrintStream; +import java.util.Map; +import java.util.SortedMap; + +import org.springside.modules.metrics.report.metrics.CounterMetric; +import org.springside.modules.metrics.report.metrics.HistogramMetric; + +public class ConsoleReporter implements Reporter { + private static final int CONSOLE_WIDTH = 80; + + private PrintStream output = System.out; + + @Override + public void report(SortedMap counters, SortedMap histograms) { + if (!counters.isEmpty()) { + printWithBanner("-- Counters", '-'); + for (Map.Entry entry : counters.entrySet()) { + output.println(entry.getKey()); + printCounter(entry.getValue()); + } + output.println(); + } + } + + private void printWithBanner(String s, char c) { + output.print(s); + output.print(' '); + for (int i = 0; i < (CONSOLE_WIDTH - s.length() - 1); i++) { + output.print(c); + } + output.println(); + } + + private void printCounter(CounterMetric counter) { + output.printf(" count = %d%n", counter.coute); + output.printf(" last rate = %2.2f events/%s%n", counter.lastRate); + output.printf(" mean rate = %2.2f events/%s%n", counter.meanRate); + } +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/report/GraphiteReporter.java b/modules/metrics/src/main/java/org/springside/modules/metrics/report/GraphiteReporter.java new file mode 100644 index 00000000..81d72983 --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/report/GraphiteReporter.java @@ -0,0 +1,5 @@ +package org.springside.modules.metrics.report; + +public class GraphiteReporter { + +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/report/ReportScheduler.java b/modules/metrics/src/main/java/org/springside/modules/metrics/report/ReportScheduler.java new file mode 100644 index 00000000..cd1fa1c0 --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/report/ReportScheduler.java @@ -0,0 +1,20 @@ +package org.springside.modules.metrics.report; + +import java.util.List; + +import org.springside.modules.metrics.MetricRegistry; + +public class ReportScheduler { + + private List reporters; + private MetricRegistry metricRegistry; + + public ReportScheduler(MetricRegistry metricRegistry, List repoters) { + this.reporters = repoters; + } + + public void report() { + + } + +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/report/Reporter.java b/modules/metrics/src/main/java/org/springside/modules/metrics/report/Reporter.java new file mode 100644 index 00000000..c410ffc2 --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/report/Reporter.java @@ -0,0 +1,10 @@ +package org.springside.modules.metrics.report; + +import java.util.SortedMap; + +import org.springside.modules.metrics.report.metrics.CounterMetric; +import org.springside.modules.metrics.report.metrics.HistogramMetric; + +public interface Reporter { + void report(SortedMap counters, SortedMap histograms); +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/report/metrics/CounterMetric.java b/modules/metrics/src/main/java/org/springside/modules/metrics/report/metrics/CounterMetric.java new file mode 100644 index 00000000..72adb6c2 --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/report/metrics/CounterMetric.java @@ -0,0 +1,7 @@ +package org.springside.modules.metrics.report.metrics; + +public class CounterMetric { + public long coute; + public double lastRate; + public double meanRate; +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/report/metrics/HistogramMetric.java b/modules/metrics/src/main/java/org/springside/modules/metrics/report/metrics/HistogramMetric.java new file mode 100644 index 00000000..3910a864 --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/report/metrics/HistogramMetric.java @@ -0,0 +1,5 @@ +package org.springside.modules.metrics.report.metrics; + +public class HistogramMetric { + +} diff --git a/modules/metrics/src/main/java/org/springside/modules/metrics/utils/Clock.java b/modules/metrics/src/main/java/org/springside/modules/metrics/utils/Clock.java new file mode 100644 index 00000000..2219affa --- /dev/null +++ b/modules/metrics/src/main/java/org/springside/modules/metrics/utils/Clock.java @@ -0,0 +1,81 @@ +package org.springside.modules.metrics.utils; + +import java.util.Date; + +/** + * 日期提供者,使用它而不是直接取得系统时间,方便测试。 + * + * @author calvin + */ +public interface Clock { + + Date getCurrentDate(); + + long getCurrentTime(); + + static final Clock DEFAULT = new DefaultDateProvider(); + + /** + * 默认时间提供者,返回当前的时间,线程安全。 + */ + public static class DefaultDateProvider implements Clock { + + @Override + public Date getCurrentDate() { + return new Date(); + } + + @Override + public long getCurrentTime() { + return System.currentTimeMillis(); + } + + } + + /** + * 可配置的时间提供者,用于测试. + */ + public static class MockedDateProvider implements Clock { + + private long time; + + public MockedDateProvider(Date date) { + this.time = date.getTime(); + } + + public MockedDateProvider(long time) { + this.time = time; + } + + @Override + public Date getCurrentDate() { + return new Date(time); + } + + @Override + public long getCurrentTime() { + return time; + } + + /** + * 重新设置时间。 + */ + public void update(Date newDate) { + time = newDate.getTime(); + } + + /** + * 重新设置时间。 + */ + public void update(long newTime) { + this.time = newTime; + } + + /** + * 增加时间戳. + */ + public void incrementTime(int millis) { + time += millis; + } + } +} diff --git a/modules/pom.xml b/modules/pom.xml index e86420bf..0e1bdd18 100644 --- a/modules/pom.xml +++ b/modules/pom.xml @@ -16,6 +16,7 @@ parent core extension + metrics test \ No newline at end of file