Skip to content

Commit

Permalink
fixed UT
Browse files Browse the repository at this point in the history
Signed-off-by: Atharva Sharma <[email protected]>
  • Loading branch information
atharvasharma61 committed Jun 3, 2024
1 parent 447e15f commit 369bd95
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ configOverridesWrapper, getCollectorName())) {
recordMetrics(diskMetricsGenerator);
}

private void recordMetrics(DiskMetricsGenerator diskMetricsGenerator) {
public void recordMetrics(DiskMetricsGenerator diskMetricsGenerator) {
for (String disk : diskMetricsGenerator.getAllDisks()) {
Tags diskNameTag = Tags.create().addTag("disk_name", disk);
double Disk_WaitTime = diskMetricsGenerator.getAwait(disk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,31 @@
import org.junit.Test;
import org.opensearch.performanceanalyzer.OpenSearchResources;
import org.opensearch.performanceanalyzer.collectors.CollectorTestBase;
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 extends CollectorTestBase {
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");
private static Histogram testHistogram;

@Before
public void init() {
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);
System.setProperty("os.name", "Linux");

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;
});
.thenReturn(testHistogram);

rtfDisksCollector = new RTFDisksCollector(mockController, mockWrapper);
rtfDisksCollector = spy(new RTFDisksCollector(mockController, mockWrapper));
}

@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");
rtfDisksCollector.collectMetrics(System.currentTimeMillis());
verify(rtfDisksCollector, atLeastOnce()).recordMetrics(any());
}
}

0 comments on commit 369bd95

Please sign in to comment.