Skip to content

Commit

Permalink
Added UTs
Browse files Browse the repository at this point in the history
Signed-off-by: Atharva Sharma <[email protected]>
  • Loading branch information
atharvasharma61 committed May 31, 2024
1 parent e9b5d57 commit 538d682
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.performanceanalyzer.collectors.telemetry;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;

import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.opensearch.performanceanalyzer.OpenSearchResources;
import org.opensearch.performanceanalyzer.commons.metrics.AllMetrics;
import org.opensearch.performanceanalyzer.commons.metrics.MetricsConfiguration;
import org.opensearch.telemetry.metrics.Histogram;
import org.opensearch.telemetry.metrics.MetricsRegistry;

public class RTFDisksCollectorTests {
private RTFDisksCollector rtfDisksCollector;
private static MetricsRegistry metricsRegistry;
private static Histogram diskWaitTimeHistogram;
private static Histogram diskServiceRateHistogram;
private static Histogram diskUtilizationHistogram;

private static final String OS_TYPE = System.getProperty("os.name");

@Before
public void init() {
initMocks(this);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
MetricsConfiguration.CONFIG_MAP.put(RTFDisksCollector.class, MetricsConfiguration.cdefault);

metricsRegistry = mock(MetricsRegistry.class);
diskWaitTimeHistogram = mock(Histogram.class);
diskServiceRateHistogram = mock(Histogram.class);
diskUtilizationHistogram = mock(Histogram.class);
OpenSearchResources.INSTANCE.setMetricsRegistry(metricsRegistry);

when(metricsRegistry.createHistogram(anyString(), anyString(), anyString()))
.thenAnswer(
invocationOnMock -> {
String histogramName = (String) invocationOnMock.getArguments()[0];
if (histogramName.contains(AllMetrics.DiskValue.Constants.WAIT_VALUE)) {
return diskWaitTimeHistogram;
} else if (histogramName.contains(
AllMetrics.DiskValue.Constants.SRATE_VALUE)) {
return diskServiceRateHistogram;
}
return diskUtilizationHistogram;
});

rtfDisksCollector = new RTFDisksCollector();
}

@Test
public void testCollectMetrics() throws IOException {
if (isLinux()) {
rtfDisksCollector.collectMetrics(System.currentTimeMillis());
verify(diskUtilizationHistogram, atLeastOnce()).record(anyDouble(), any());
verify(diskServiceRateHistogram, atLeastOnce()).record(anyDouble(), any());
verify(diskWaitTimeHistogram, atLeastOnce()).record(anyDouble(), any());
}
}

private static boolean isLinux() {
return OS_TYPE.toLowerCase().contains("linux");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.performanceanalyzer.collectors.telemetry;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.MockitoAnnotations.initMocks;

import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.opensearch.performanceanalyzer.OpenSearchResources;
import org.opensearch.performanceanalyzer.commons.metrics.AllMetrics;
import org.opensearch.performanceanalyzer.commons.metrics.MetricsConfiguration;
import org.opensearch.telemetry.metrics.Histogram;
import org.opensearch.telemetry.metrics.MetricsRegistry;

public class RTFHeapMetricsCollectorTests {
private RTFHeapMetricsCollector rtfHeapMetricsCollector;

private static MetricsRegistry metricsRegistry;
private static Histogram gcCollectionEventHistogram;
private static Histogram gcCollectionTimeHistogram;
private static Histogram heapUsedHistogram;

@Before
public void init() {
initMocks(this);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
MetricsConfiguration.CONFIG_MAP.put(
RTFHeapMetricsCollector.class, MetricsConfiguration.cdefault);

metricsRegistry = mock(MetricsRegistry.class);
gcCollectionEventHistogram = mock(Histogram.class);
gcCollectionTimeHistogram = mock(Histogram.class);
heapUsedHistogram = mock(Histogram.class);
OpenSearchResources.INSTANCE.setMetricsRegistry(metricsRegistry);

when(metricsRegistry.createHistogram(anyString(), anyString(), anyString()))
.thenAnswer(
invocationOnMock -> {
String histogramName = (String) invocationOnMock.getArguments()[0];
if (histogramName.contains(
AllMetrics.HeapValue.Constants.COLLECTION_COUNT_VALUE)) {
return gcCollectionEventHistogram;
} else if (histogramName.contains(
AllMetrics.HeapValue.Constants.COLLECTION_TIME_VALUE)) {
return gcCollectionTimeHistogram;
}
return heapUsedHistogram;
});
rtfHeapMetricsCollector = new RTFHeapMetricsCollector();
}

@Test
public void testCollectMetrics() throws IOException {
rtfHeapMetricsCollector.collectMetrics(System.currentTimeMillis());
verify(heapUsedHistogram, atLeastOnce()).record(anyDouble(), any());
verify(gcCollectionTimeHistogram, atLeastOnce()).record(anyDouble(), any());
verify(gcCollectionEventHistogram, atLeastOnce()).record(anyDouble(), any());
verify(metricsRegistry, atLeastOnce())
.createGauge(anyString(), anyString(), anyString(), any(), any());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.performanceanalyzer.collectors.telemetry;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.MockitoAnnotations.initMocks;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.opensearch.performanceanalyzer.OpenSearchResources;
import org.opensearch.performanceanalyzer.commons.metrics.AllMetrics;
import org.opensearch.performanceanalyzer.commons.metrics.MetricsConfiguration;
import org.opensearch.telemetry.metrics.Histogram;
import org.opensearch.telemetry.metrics.MetricsRegistry;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.threadpool.ThreadPoolStats;

public class RTFThreadPoolMetricsCollectorTests {
private RTFThreadPoolMetricsCollector rtfThreadPoolMetricsCollector;
private static MetricsRegistry metricsRegistry;
private static Histogram threadPoolQueueSizeHistogram;
private static Histogram threadPoolRejectedReqsHistogram;
private static Histogram threadPoolTotalThreadsHistogram;
private static Histogram threadPoolActiveThreadsHistogram;
private static Histogram threadPoolQueueCapacityHistogram;
@Mock private ThreadPool mockThreadPool;

@Before
public void init() {
initMocks(this);
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
MetricsConfiguration.CONFIG_MAP.put(
RTFThreadPoolMetricsCollector.class, MetricsConfiguration.cdefault);
metricsRegistry = mock(MetricsRegistry.class);
threadPoolQueueSizeHistogram = mock(Histogram.class);
threadPoolRejectedReqsHistogram = mock(Histogram.class);
threadPoolActiveThreadsHistogram = mock(Histogram.class);
threadPoolTotalThreadsHistogram = mock(Histogram.class);
threadPoolQueueCapacityHistogram = mock(Histogram.class);

OpenSearchResources.INSTANCE.setMetricsRegistry(metricsRegistry);
OpenSearchResources.INSTANCE.setThreadPool(mockThreadPool);

when(metricsRegistry.createHistogram(anyString(), anyString(), anyString()))
.thenAnswer(
invocationOnMock -> {
String histogramName = (String) invocationOnMock.getArguments()[0];
if (histogramName.contains(
AllMetrics.ThreadPoolValue.Constants.QUEUE_SIZE_VALUE)) {
return threadPoolQueueSizeHistogram;
} else if (histogramName.contains(
AllMetrics.ThreadPoolValue.Constants.REJECTED_VALUE)) {
return threadPoolRejectedReqsHistogram;
} else if (histogramName.contains(
AllMetrics.ThreadPoolValue.Constants.THREADS_ACTIVE_VALUE)) {
return threadPoolActiveThreadsHistogram;
} else if (histogramName.contains(
AllMetrics.ThreadPoolValue.Constants.QUEUE_CAPACITY_VALUE)) {
return threadPoolQueueCapacityHistogram;
}
return threadPoolTotalThreadsHistogram;
});

rtfThreadPoolMetricsCollector = new RTFThreadPoolMetricsCollector();
}

@Test
public void testCollectMetrics() throws IOException {
when(mockThreadPool.stats()).thenReturn(generateThreadPoolStat());
rtfThreadPoolMetricsCollector.collectMetrics(System.currentTimeMillis());
verify(mockThreadPool, atLeastOnce()).stats();
verify(threadPoolQueueSizeHistogram, atLeastOnce()).record(anyDouble(), any());
verify(threadPoolRejectedReqsHistogram, atLeastOnce()).record(anyDouble(), any());
verify(threadPoolActiveThreadsHistogram, atLeastOnce()).record(anyDouble(), any());
verify(threadPoolTotalThreadsHistogram, atLeastOnce()).record(anyDouble(), any());
}

private ThreadPoolStats generateThreadPoolStat() {
List<ThreadPoolStats.Stats> stats = new ArrayList<>();
stats.add(new ThreadPoolStats.Stats("write", 0, 0, 0, 2, 0, 0L, 20L));
return new ThreadPoolStats(stats);
}
}

0 comments on commit 538d682

Please sign in to comment.