提交 41f87a41 编写于 作者: D duhenglucky

Add unit test for MetricsService

上级 61258bf8
......@@ -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);
......
......@@ -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;
}
}
/*
* 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});
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册