This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 1dbd8bde0ea99defa63a5358409f86620d301f29 Author: zhengyangyong <yangyong.zh...@huawei.com> AuthorDate: Sat Dec 23 09:51:32 2017 +0800 JAV-539 & SCB-9 change time-related output from nanosecond to millisecond Signed-off-by: zhengyangyong <yangyong.zh...@huawei.com> --- .../metrics/core/metric/TimerMetric.java | 20 ++--- .../metrics/core/monitor/BasicMonitor.java | 6 ++ .../metrics/core/monitor/TimerMonitor.java | 9 ++- .../metrics/core/TestEventAndRunner.java | 88 +++++++++++----------- 4 files changed, 66 insertions(+), 57 deletions(-) diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/TimerMetric.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/TimerMetric.java index d2587cf..7212806 100644 --- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/TimerMetric.java +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/metric/TimerMetric.java @@ -26,17 +26,17 @@ public class TimerMetric { @JsonIgnore private final String prefix; - private final long total; + private final double total; private final long count; private final double average; - private final long min; + private final double min; - private final long max; + private final double max; - public long getTotal() { + public double getTotal() { return total; } @@ -48,11 +48,11 @@ public class TimerMetric { return average; } - public long getMin() { + public double getMin() { return min; } - public long getMax() { + public double getMax() { return max; } @@ -60,12 +60,12 @@ public class TimerMetric { this(prefix, 0, 0, 0, 0); } - public TimerMetric(String prefix, long total, long count, long min, long max) { + public TimerMetric(String prefix, double total, long count, double min, double max) { this.prefix = prefix; this.total = total; this.count = count; if (count != 0) { - this.average = (double) total / (double) count; + this.average = total / (double) count; } else { this.average = 0; } @@ -78,11 +78,11 @@ public class TimerMetric { getMin(this.min, metric.min), getMax(this.max, metric.max)); } - private long getMin(long value1, long value2) { + private double getMin(double value1, double value2) { return value1 == 0 || (value2 != 0 && value2 < value1) ? value2 : value1; } - private long getMax(long value1, long value2) { + private double getMax(double value1, double value2) { return value2 > value1 ? value2 : value1; } diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/BasicMonitor.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/BasicMonitor.java index 69227ac..6045d16 100644 --- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/BasicMonitor.java +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/BasicMonitor.java @@ -30,4 +30,10 @@ public class BasicMonitor { public long adjustValue(long value) { return value < 0 ? 0 : value; } + + //Counting use System.nano get more precise time + //so we need change unit to millisecond when ouput + public double convertNanosecondToMillisecond(long nanoValue){ + return nanoValue * 0.000001; + } } diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/TimerMonitor.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/TimerMonitor.java index a1aec71..9917b5e 100644 --- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/TimerMonitor.java +++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/monitor/TimerMonitor.java @@ -27,12 +27,15 @@ import io.servicecomb.metrics.core.metric.TimerMetric; public class TimerMonitor extends BasicMonitor { private final String prefix; + //nanosecond sum private final StepCounter total; private final StepCounter count; + //nanosecond min private final MinGauge min; + //nanosecond max private final MaxGauge max; public void update(long value) { @@ -54,9 +57,9 @@ public class TimerMonitor extends BasicMonitor { public TimerMetric toTimerMetric(int windowTimeIndex) { return new TimerMetric(this.prefix, - this.adjustValue(total.getCount(windowTimeIndex)), + this.convertNanosecondToMillisecond(this.adjustValue(total.getCount(windowTimeIndex))), this.adjustValue(count.getCount(windowTimeIndex)), - this.adjustValue(min.getValue(windowTimeIndex)), - this.adjustValue(max.getValue(windowTimeIndex))); + this.convertNanosecondToMillisecond(this.adjustValue(min.getValue(windowTimeIndex))), + this.convertNanosecondToMillisecond(this.adjustValue(max.getValue(windowTimeIndex)))); } } diff --git a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java index 9b3a194..ae518fd 100644 --- a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java +++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java @@ -92,46 +92,46 @@ public class TestEventAndRunner { Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getWaitInQueue(), 1); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getLifeTimeInQueue().getCount(), 3); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getLifeTimeInQueue().getTotal(), - TimeUnit.MILLISECONDS.toNanos(900)); + 900, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getLifeTimeInQueue().getAverage(), - TimeUnit.MILLISECONDS.toNanos(300), 0); + 300, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getLifeTimeInQueue().getMax(), - TimeUnit.MILLISECONDS.toNanos(500)); + 500, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getLifeTimeInQueue().getMin(), - TimeUnit.MILLISECONDS.toNanos(100)); + 100, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getExecutionTime().getCount(), 2); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getExecutionTime().getTotal(), - TimeUnit.MILLISECONDS.toNanos(600)); + 600, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getExecutionTime().getAverage(), - TimeUnit.MILLISECONDS.toNanos(300), 0); + 300, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getExecutionTime().getMax(), - TimeUnit.MILLISECONDS.toNanos(400)); + 400, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getExecutionTime().getMin(), - TimeUnit.MILLISECONDS.toNanos(200)); + 200, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getProducerLatency().getCount(), 2); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getProducerLatency().getTotal(), - TimeUnit.MILLISECONDS.toNanos(1000)); + 1000, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getProducerLatency().getAverage(), - TimeUnit.MILLISECONDS.toNanos(500), 0); + 500, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getProducerLatency().getMax(), - TimeUnit.MILLISECONDS.toNanos(700)); + 700, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getProducerLatency().getMin(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getProducerCall().getTps(), 3, 0); Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getProducerCall().getTotal(), 3); Assert.assertEquals(model.getInstanceMetric().getConsumerMetric().getConsumerLatency().getCount(), 1); Assert.assertEquals(model.getInstanceMetric().getConsumerMetric().getConsumerLatency().getTotal(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getInstanceMetric().getConsumerMetric().getConsumerLatency().getAverage(), - TimeUnit.MILLISECONDS.toNanos(300), 0); + 300, 0); Assert.assertEquals(model.getInstanceMetric().getConsumerMetric().getConsumerLatency().getMax(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getInstanceMetric().getConsumerMetric().getConsumerLatency().getMin(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getInstanceMetric().getConsumerMetric().getConsumerCall().getTps(), 1, 0); Assert.assertEquals(model.getInstanceMetric().getConsumerMetric().getConsumerCall().getTotal(), 1); @@ -140,33 +140,33 @@ public class TestEventAndRunner { Assert.assertEquals(model.getProducerMetrics().get("fun1").getWaitInQueue(), 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getLifeTimeInQueue().getCount(), 2); Assert.assertEquals(model.getProducerMetrics().get("fun1").getLifeTimeInQueue().getTotal(), - TimeUnit.MILLISECONDS.toNanos(400)); + 400, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getLifeTimeInQueue().getAverage(), - TimeUnit.MILLISECONDS.toNanos(200), 0); + 200, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getLifeTimeInQueue().getMax(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getLifeTimeInQueue().getMin(), - TimeUnit.MILLISECONDS.toNanos(100)); + 100, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getExecutionTime().getCount(), 2); Assert.assertEquals(model.getProducerMetrics().get("fun1").getExecutionTime().getTotal(), - TimeUnit.MILLISECONDS.toNanos(600)); + 600, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getExecutionTime().getAverage(), - TimeUnit.MILLISECONDS.toNanos(300), 0); + 300, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getExecutionTime().getMax(), - TimeUnit.MILLISECONDS.toNanos(400)); + 400, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getExecutionTime().getMin(), - TimeUnit.MILLISECONDS.toNanos(200)); + 200, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getProducerLatency().getCount(), 2); Assert.assertEquals(model.getProducerMetrics().get("fun1").getProducerLatency().getTotal(), - TimeUnit.MILLISECONDS.toNanos(1000)); + 1000, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getProducerLatency().getAverage(), - TimeUnit.MILLISECONDS.toNanos(500), 0); + 500, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getProducerLatency().getMax(), - TimeUnit.MILLISECONDS.toNanos(700)); + 700, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getProducerLatency().getMin(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getProducerCall().getTps(), 2, 0); Assert.assertEquals(model.getProducerMetrics().get("fun1").getProducerCall().getTotal(), 2); @@ -175,33 +175,33 @@ public class TestEventAndRunner { Assert.assertEquals(model.getProducerMetrics().get("fun3").getWaitInQueue(), 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getLifeTimeInQueue().getCount(), 1); Assert.assertEquals(model.getProducerMetrics().get("fun3").getLifeTimeInQueue().getTotal(), - TimeUnit.MILLISECONDS.toNanos(500)); + 500, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getLifeTimeInQueue().getAverage(), - TimeUnit.MILLISECONDS.toNanos(500), 0); + 500, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getLifeTimeInQueue().getMax(), - TimeUnit.MILLISECONDS.toNanos(500)); + 500, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getLifeTimeInQueue().getMin(), - TimeUnit.MILLISECONDS.toNanos(500)); + 500, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getExecutionTime().getCount(), 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getExecutionTime().getTotal(), - TimeUnit.MILLISECONDS.toNanos(0)); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getExecutionTime().getAverage(), - TimeUnit.MILLISECONDS.toNanos(0), 0); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getExecutionTime().getMax(), - TimeUnit.MILLISECONDS.toNanos(0)); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getExecutionTime().getMin(), - TimeUnit.MILLISECONDS.toNanos(0)); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getProducerLatency().getCount(), 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getProducerLatency().getTotal(), - TimeUnit.MILLISECONDS.toNanos(0)); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getProducerLatency().getAverage(), - TimeUnit.MILLISECONDS.toNanos(0), 0); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getProducerLatency().getMax(), - TimeUnit.MILLISECONDS.toNanos(0)); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getProducerLatency().getMin(), - TimeUnit.MILLISECONDS.toNanos(0)); + 0, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getProducerCall().getTps(), 1, 0); Assert.assertEquals(model.getProducerMetrics().get("fun3").getProducerCall().getTotal(), 1); @@ -211,13 +211,13 @@ public class TestEventAndRunner { Assert.assertEquals(model.getConsumerMetrics().get("fun2").getWaitInQueue(), 0); Assert.assertEquals(model.getConsumerMetrics().get("fun2").getConsumerLatency().getCount(), 1); Assert.assertEquals(model.getConsumerMetrics().get("fun2").getConsumerLatency().getTotal(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getConsumerMetrics().get("fun2").getConsumerLatency().getAverage(), - TimeUnit.MILLISECONDS.toNanos(300), 0); + 300, 0); Assert.assertEquals(model.getConsumerMetrics().get("fun2").getConsumerLatency().getMax(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getConsumerMetrics().get("fun2").getConsumerLatency().getMin(), - TimeUnit.MILLISECONDS.toNanos(300)); + 300, 0); Assert.assertEquals(model.getConsumerMetrics().get("fun2").getConsumerCall().getTps(), 1, 0); Assert.assertEquals(model.getConsumerMetrics().get("fun2").getConsumerCall().getTotal(), 1); -- To stop receiving notification emails like this one, please contact "commits@servicecomb.apache.org" <commits@servicecomb.apache.org>.