From 41f87a4189933a1700473b72fff20e7b1d8aac10 Mon Sep 17 00:00:00 2001 From: duhenglucky Date: Mon, 18 Feb 2019 19:23:38 +0800 Subject: [PATCH] Add unit test for MetricsService --- .../snode/service/MetricsService.java | 2 +- .../service/impl/MetricsServiceImpl.java | 38 ++++++++--- .../snode/service/MetricsServiceImplTest.java | 67 +++++++++++++++++++ 3 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 snode/src/test/java/org/apache/rocketmq/snode/service/MetricsServiceImplTest.java diff --git a/snode/src/main/java/org/apache/rocketmq/snode/service/MetricsService.java b/snode/src/main/java/org/apache/rocketmq/snode/service/MetricsService.java index 6942ac22..f7dedef4 100644 --- a/snode/src/main/java/org/apache/rocketmq/snode/service/MetricsService.java +++ b/snode/src/main/java/org/apache/rocketmq/snode/service/MetricsService.java @@ -26,7 +26,7 @@ public interface MetricsService { void incRequestCount(int requestCode, boolean success); - void recordRequestSize(String topic, double latency); + void recordRequestSize(String topic, double size); Timer startTimer(int requestCode); diff --git a/snode/src/main/java/org/apache/rocketmq/snode/service/impl/MetricsServiceImpl.java b/snode/src/main/java/org/apache/rocketmq/snode/service/impl/MetricsServiceImpl.java index 84651fef..84b06beb 100644 --- a/snode/src/main/java/org/apache/rocketmq/snode/service/impl/MetricsServiceImpl.java +++ b/snode/src/main/java/org/apache/rocketmq/snode/service/impl/MetricsServiceImpl.java @@ -16,7 +16,6 @@ */ package org.apache.rocketmq.snode.service.impl; -import io.prometheus.client.CollectorRegistry; import io.prometheus.client.Counter; import io.prometheus.client.Summary; import io.prometheus.client.exporter.HTTPServer; @@ -47,29 +46,25 @@ public class MetricsServiceImpl implements MetricsService { } }); - StatsItemSet statsItemSet = new StatsItemSet("SnodeStats", scheduledExecutorService, log); + private StatsItemSet statsItemSet = new StatsItemSet("SnodeStats", scheduledExecutorService, log); - private final Counter requestTotal = Counter.build().name("request_total").help("request total count").labelNames("requestCode").register(); + private final static Counter requestTotal = Counter.build().name("request_total").help("request total count").labelNames("requestCode").register(); - private final Counter requestFailedTotal = Counter.build().name("request_failed_total").help("request total count").labelNames("requestCode").register(); + private final static Counter requestFailedTotal = Counter.build().name("request_failed_total").help("request total count").labelNames("requestCode").register(); - private final Summary requestLatency = Summary.build() + private final static Summary requestLatency = Summary.build() .quantile(0.5, 0.05) .quantile(0.9, 0.01) .quantile(0.99, 0.001) .name("requests_latency_seconds").labelNames("requestCode").help("Request latency in seconds.").register(); - private final Summary receivedBytes = Summary.build() + private final static Summary receivedBytes = Summary.build() .quantile(0.5, 0.05) .quantile(0.9, 0.01) .quantile(0.99, 0.001) .labelNames("topic") .name("sent_topic_size_bytes").help("Request size in bytes.").register(); - public Double getLabelsValue(String labelValue) { - return CollectorRegistry.defaultRegistry.getSampleValue("request_total", new String[] {"requestCode"}, new String[] {labelValue}); - } - @Override synchronized public void incRequestCount(int requestCode, boolean success) { if (!success) { @@ -135,4 +130,27 @@ public class MetricsServiceImpl implements MetricsService { } } + public StatsItemSet getStatsItemSet() { + return statsItemSet; + } + + public void setStatsItemSet(StatsItemSet statsItemSet) { + this.statsItemSet = statsItemSet; + } + + public Counter getRequestTotal() { + return requestTotal; + } + + public Counter getRequestFailedTotal() { + return requestFailedTotal; + } + + public Summary getRequestLatency() { + return requestLatency; + } + + public Summary getReceivedBytes() { + return receivedBytes; + } } diff --git a/snode/src/test/java/org/apache/rocketmq/snode/service/MetricsServiceImplTest.java b/snode/src/test/java/org/apache/rocketmq/snode/service/MetricsServiceImplTest.java new file mode 100644 index 00000000..cd74e81f --- /dev/null +++ b/snode/src/test/java/org/apache/rocketmq/snode/service/MetricsServiceImplTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.snode.service; + +import io.prometheus.client.CollectorRegistry; +import java.lang.reflect.Field; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.rocketmq.common.stats.StatsItemSet; +import org.apache.rocketmq.snode.service.impl.MetricsServiceImpl; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(MockitoJUnitRunner.class) +public class MetricsServiceImplTest { + private MetricsServiceImpl metricsService = new MetricsServiceImpl(); + + private final String requestCode = "310"; + + private final String topic = "Test"; + + @Test + public void testIncRequestCountFalse() throws Exception { + metricsService.incRequestCount(310, false); + Double requestFailedTotal = getLabelsValue("request_failed_total", requestCode); + Double requestTotal = getLabelsValue("request_total", requestCode); + assertThat(requestFailedTotal).isEqualTo(1.0); + assertThat(requestTotal); + } + + @Test + public void testIncRequestCountTrue() { + metricsService.incRequestCount(310, true); + Double requestTotal = getLabelsValue("request_total", requestCode); + assertThat(requestTotal).isEqualTo(1.0); + } + + @Test + public void testRequestSize() throws Exception { + Field field = MetricsServiceImpl.class.getDeclaredField("statsItemSet"); + field.setAccessible(true); + metricsService.recordRequestSize(topic, 100); + StatsItemSet statsItemSet = (StatsItemSet) field.get(metricsService); + AtomicLong requestSize = statsItemSet.getStatsItem("TotalSize@" + topic).getValue(); + assertThat(requestSize.intValue()).isEqualTo(100); + } + + public Double getLabelsValue(String name, String labelValue) { + return CollectorRegistry.defaultRegistry.getSampleValue(name, new String[] {"requestCode"}, new String[] {labelValue}); + } +} -- GitLab