Technoboy- commented on a change in pull request #13833:
URL: https://github.com/apache/pulsar/pull/13833#discussion_r792340524



##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/stats/LedgerOffloaderMetricsTest.java
##########
@@ -0,0 +1,247 @@
+/**
+ * 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.pulsar.broker.stats;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import com.google.common.collect.Multimap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.LongAdder;
+import org.apache.bookkeeper.mledger.LedgerOffloader;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerConfig;
+import org.apache.bookkeeper.mledger.impl.LedgerOffloaderMXBeanImpl;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.service.BrokerService;
+import org.apache.pulsar.broker.service.BrokerTestBase;
+import org.apache.pulsar.broker.service.Topic;
+import org.apache.pulsar.broker.service.persistent.PersistentTopic;
+import org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsGenerator;
+import org.apache.pulsar.common.util.SimpleTextOutputStream;
+import org.junit.Assert;
+import org.mockito.Mockito;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class LedgerOffloaderMetricsTest  extends BrokerTestBase {
+    @BeforeMethod(alwaysRun = true)
+    @Override
+    protected void setup() throws Exception {
+        super.baseSetup();
+
+
+    }
+
+    @AfterMethod(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+
+    public String convertByteBufToString(ByteBuf buf) {
+        String str;
+        if(buf.hasArray()) {
+            str = new String(buf.array(), buf.arrayOffset() + 
buf.readerIndex(), buf.readableBytes());
+        } else {
+            byte[] bytes = new byte[buf.readableBytes()];
+            buf.getBytes(buf.readerIndex(), bytes);
+            str = new String(bytes, 0, buf.readableBytes());
+        }
+        return str;
+    }
+
+    @Test(timeOut = 3000)
+    public void testTopicLevelMetrics() throws Exception {
+        String ns1 = "prop/ns-abc1";
+        admin.namespaces().createNamespace(ns1);
+
+        ByteBuf buf = ByteBufAllocator.DEFAULT.heapBuffer();
+        SimpleTextOutputStream stream = new SimpleTextOutputStream(buf);
+
+        String []topics = new String[3];
+
+        LedgerOffloaderMXBeanImpl mbean = new 
LedgerOffloaderMXBeanImpl("test");
+        LedgerOffloader offloader = Mockito.mock(LedgerOffloader.class);
+        Topic topic = Mockito.mock(PersistentTopic.class);
+        CompletableFuture<Optional<Topic>> topicFuture = new 
CompletableFuture<>();
+        Optional<Topic> topicOptional = Optional.of(topic);
+        topicFuture.complete(topicOptional);
+        BrokerService brokerService = spy(pulsar.getBrokerService());
+        doReturn(brokerService).when(pulsar).getBrokerService();
+
+
+        for (int i = 0; i < 3; i++) {
+            String topicName = "persistent://prop/ns-abc1/testMetrics" + 
UUID.randomUUID();
+            topics[i] = topicName;
+            admin.topics().createNonPartitionedTopic(topicName);
+
+            
doReturn(topicFuture).when(brokerService).getTopicIfExists(topicName);
+            Assert.assertTrue(topic instanceof PersistentTopic);
+
+            ManagedLedger ledgerM = Mockito.mock(ManagedLedger.class);
+            doReturn(ledgerM).when(((PersistentTopic) 
topic)).getManagedLedger();
+            ManagedLedgerConfig config = 
Mockito.mock(ManagedLedgerConfig.class);
+            doReturn(config).when(ledgerM).getConfig();
+            doReturn(offloader).when(config).getLedgerOffloader();
+
+            Mockito.when(offloader.getStats()).thenReturn(mbean);
+
+            mbean.recordOffloadError(topicName);
+            mbean.recordOffloadError(topicName);
+            mbean.recordOffloadBytes(topicName, 100);
+            mbean.recordOffloadTime(topicName, 10000000000L, 
TimeUnit.NANOSECONDS);
+            mbean.recordReadLedgerLatency(topicName, 1000, 
TimeUnit.NANOSECONDS);
+            mbean.recordReadOffloadError(topicName);
+            mbean.recordReadOffloadError(topicName);
+            mbean.recordReadOffloadIndexLatency(topicName, 1000000L, 
TimeUnit.NANOSECONDS);
+            mbean.recordReadOffloadBytes(topicName, 100000);
+            mbean.recordStreamingWriteToStorageError(topicName);
+            mbean.recordStreamingWriteToStorageError(topicName);
+            mbean.recordStreamingWriteToStorageBytes(topicName, 1000001);
+            mbean.recordWriteToStorageError(topicName);
+            mbean.recordWriteToStorageError(topicName);
+            mbean.recordWriteToStorageLatency(topicName, 20000, 
TimeUnit.NANOSECONDS);
+        }
+
+        Method parseMetricMethod = PrometheusMetricsGenerator.class.
+                getDeclaredMethod("generateLedgerOffloaderMetrics",
+                        PulsarService.class, SimpleTextOutputStream.class,
+                        boolean.class);
+        parseMetricMethod.setAccessible(true);
+        parseMetricMethod.invoke(null, pulsar, stream, true);
+
+
+        String metricsStr = convertByteBufToString(buf);
+//        System.out.println(metricsStr);

Review comment:
       Remove this?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to