Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Improve Test Coverage to 81% #258

Merged
merged 32 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c58d69b
Merge branch 'master' of github.com:opendistro-for-elasticsearch/perf…
yu-sun-77 Nov 20, 2020
0f97299
Merge branch 'master' of github.com:opendistro-for-elasticsearch/perf…
yu-sun-77 Nov 21, 2020
1566405
Merge branch 'master' of github.com:opendistro-for-elasticsearch/perf…
yu-sun-77 Nov 23, 2020
77880c6
Merge branch 'master' of github.com:opendistro-for-elasticsearch/perf…
yu-sun-77 Dec 7, 2020
7fa096f
Merge branch 'master' of github.com:opendistro-for-elasticsearch/perf…
yu-sun-77 Dec 9, 2020
dbe2a71
Merge branch 'master' of github.com:opendistro-for-elasticsearch/perf…
yu-sun-77 Dec 17, 2020
3743ba7
pending tests to be tested on linuxOS only
yu-sun-77 Dec 11, 2020
19e8466
add UT for MasterServiceEventMetricsTests
Dec 16, 2020
e734d78
add UT for PerformanceAnalyzerSearchListenerTests
Dec 16, 2020
2bc19bd
move linuxOS check to @BeforeClass
yu-sun-77 Dec 17, 2020
3f1f722
add UT for MasterServiceMetricsTests
yu-sun-77 Dec 17, 2020
61bc1b6
add UT for NodeStatsSettingHandlerTests
yu-sun-77 Dec 17, 2020
e9be02d
add UT for WhoAmI package
yu-sun-77 Dec 18, 2020
5fe53ab
remove unused PA reader tests
yu-sun-77 Dec 18, 2020
caa2969
clean metricQueue before running every test
Dec 18, 2020
5120eae
pending tests to be tested on linuxOS only
yu-sun-77 Dec 11, 2020
0a0d563
add UT for MasterServiceEventMetricsTests
Dec 16, 2020
8b5022c
add UT for PerformanceAnalyzerSearchListenerTests
Dec 16, 2020
8f6dfb5
move linuxOS check to @BeforeClass
yu-sun-77 Dec 17, 2020
7111c2c
add UT for MasterServiceMetricsTests
yu-sun-77 Dec 17, 2020
15f65c5
add UT for NodeStatsSettingHandlerTests
yu-sun-77 Dec 17, 2020
18464ab
add UT for WhoAmI package
yu-sun-77 Dec 18, 2020
4143318
remove unused PA reader tests
yu-sun-77 Dec 18, 2020
8116379
clean metricQueue before running every test
Dec 18, 2020
4764b1f
add UT for PerformanceAnalyzerActionFilterTests
yu-sun-77 Jan 4, 2021
2b8f5d1
add UT for PerformanceAnalyzerActionListenerTests
yu-sun-77 Jan 4, 2021
4e1dc69
add UT for PerformanceAnalyzerControllerTests
yu-sun-77 Jan 4, 2021
8b8f994
add UT for EventLogFileHandlerTests
yu-sun-77 Jan 4, 2021
4dd1886
Merge branch 'coverage' of github.com:yu-sun-77/performance-analyzer …
yu-sun-77 Jan 4, 2021
2d56891
exclude test for ClusterSettingsManager.java
yu-sun-77 Jan 4, 2021
5a2af15
code review changes
yu-sun-77 Jan 7, 2021
1f0c24e
Merge branch 'master' of github.com:opendistro-for-elasticsearch/perf…
yu-sun-77 Jan 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ jacocoTestReport {
exclude: [
'**/FaultDetectionMetricsCollector.class',
'**/MasterThrottlingMetricsCollector.class',
'**/ClusterSettingsManager.class',
])
})
}
Expand All @@ -167,7 +168,7 @@ jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.6212
minimum = 0.8
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Queue;
import java.util.concurrent.ThreadPoolExecutor;

import com.google.common.annotations.VisibleForTesting;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.service.MasterService;
Expand All @@ -43,7 +44,6 @@ public class MasterServiceEventMetrics extends PerformanceAnalyzerMetricsCollect
MasterServiceEventMetrics.class).samplingInterval;
private static final Logger LOG = LogManager.getLogger(MasterServiceEventMetrics.class);
private static final String MASTER_NODE_NOT_UP_METRIC = "MasterNodeNotUp";
private long lastTaskInsertionOrder;
private static final int KEYS_PATH_LENGTH = 3;
private StringBuilder value;
private static final int TPEXECUTOR_ADD_PENDING_PARAM_COUNT = 3;
Expand All @@ -53,6 +53,9 @@ public class MasterServiceEventMetrics extends PerformanceAnalyzerMetricsCollect
private long currentThreadId;
private Object currentWorker;

@VisibleForTesting
long lastTaskInsertionOrder;

public MasterServiceEventMetrics() {
super(SAMPLING_TIME_INTERVAL, "MasterServiceEventMetrics");
masterServiceCurrentQueue = null;
Expand Down Expand Up @@ -137,7 +140,8 @@ public void collectMetrics(long startTime) {
}
}

private void generateFinishMetrics(long startTime) {
@VisibleForTesting
void generateFinishMetrics(long startTime) {
if (lastTaskInsertionOrder != -1) {
value.append(PerformanceAnalyzerMetrics.getCurrentTimeMetric());
PerformanceAnalyzerMetrics.addMetricEntry(value, MasterMetricValues.FINISH_TIME.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,42 +237,6 @@ public NodeStatsMetricsFixedShardsPerCollectionStatus(ShardStats shardStats) {
this.shardSizeInBytes = calculate(ShardStatsValue.SHARD_SIZE_IN_BYTES);
}

@SuppressWarnings("checkstyle:parameternumber")
public NodeStatsMetricsFixedShardsPerCollectionStatus(long indexingThrottleTime, long refreshCount, long refreshTime,
long flushCount, long flushTime, long mergeCount,
long mergeTime, long mergeCurrent, long indexBufferBytes,
long segmentCount, long segmentsMemory, long termsMemory,
long storedFieldsMemory, long termVectorsMemory,
long normsMemory, long pointsMemory, long docValuesMemory,
long indexWriterMemory, long versionMapMemory,
long bitsetMemory, long shardSizeInBytes) {
super();
this.shardStats = null;

this.indexingThrottleTime = indexingThrottleTime;
this.refreshCount = refreshCount;
this.refreshTime = refreshTime;
this.flushCount = flushCount;
this.flushTime = flushTime;
this.mergeCount = mergeCount;
this.mergeTime = mergeTime;
this.mergeCurrent = mergeCurrent;
this.indexBufferBytes = indexBufferBytes;
this.segmentCount = segmentCount;
this.segmentsMemory = segmentsMemory;
this.termsMemory = termsMemory;
this.storedFieldsMemory = storedFieldsMemory;
this.termVectorsMemory = termVectorsMemory;
this.normsMemory = normsMemory;
this.pointsMemory = pointsMemory;
this.docValuesMemory = docValuesMemory;
this.indexWriterMemory = indexWriterMemory;
this.versionMapMemory = versionMapMemory;
this.bitsetMemory = bitsetMemory;
this.shardSizeInBytes = shardSizeInBytes;
}


private long calculate(ShardStatsValue nodeMetric) {
return valueCalculators.get(nodeMetric.toString()).calculateValue(shardStats);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.amazon.opendistro.elasticsearch.performanceanalyzer.config;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.PerformanceAnalyzerPlugin;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ScheduledMetricCollectorsExecutor;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.PerformanceAnalyzerPlugin;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors.ScheduledMetricCollectorsExecutor;

public class PerformanceAnalyzerController {
private static final String PERFORMANCE_ANALYZER_ENABLED_CONF = "performance_analyzer_enabled.conf";
private static final String RCA_ENABLED_CONF = "rca_enabled.conf";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
public class WhoAmIResponse extends ActionResponse implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("whoami");
builder.endObject();
builder
.startObject()
.field("whoami", "whoami")
.endObject();
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public PerformanceAnalyzerSearchListener(final PerformanceAnalyzerController con

@Override
public String toString() {
return "PerformanceAnalyzerSearchListener";
return PerformanceAnalyzerSearchListener.class.getSimpleName();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.amazon.opendistro.elasticsearch.performanceanalyzer.action;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
Expand All @@ -40,6 +41,7 @@ public class PerformanceAnalyzerActionFilterTests {
@Mock private PerformanceAnalyzerController controller;
@Mock private SearchRequest searchRequest;
@Mock private BulkRequest bulkRequest;
@Mock private ActionRequest request;
@Mock private ActionListener<ActionResponse> listener;
@Mock private ActionFilterChain<ActionRequest, ActionResponse> chain;
@Mock private Task task;
Expand All @@ -63,9 +65,19 @@ public void testApplyWithBulkRequest() {
testApply(bulkRequest);
}

@Test
public void testApplyWithOtherRequest() {
testApply(request);
}

private void testApply(ActionRequest request) {
filter.apply(task, "_action", request, listener, chain);
verify(chain).proceed(eq(task), eq("_action"), eq(request), any());
}

@Test
public void testOrder() {
assertEquals(Integer.MIN_VALUE, filter.order());
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright <2019> Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright <2020> Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -15,49 +15,106 @@

package com.amazon.opendistro.elasticsearch.performanceanalyzer.action;

import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.CustomMetricsLocationTestBase;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PluginSettings;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
import static org.junit.Assert.assertEquals;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.reader_writer_shared.Event;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.util.TestUtil;
import java.util.List;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.rest.RestStatus;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

@SuppressWarnings("unchecked")
public class PerformanceAnalyzerActionListenerTests {
private static final String listenerId= "12345";
private long startTimeInMills = 1153721339;
private PerformanceAnalyzerActionListener actionListener;
private ActionListener originalActionListener;

@Before
public void init() {
originalActionListener = Mockito.mock(ActionListener.class);
actionListener = new PerformanceAnalyzerActionListener();
}

@Test
public void testGetMetricsPath() {
String expectedPath = PluginSettings.instance().getMetricsLocation()
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills) + "/"
+ PerformanceAnalyzerMetrics.sThreadsPath + "/"
+ PerformanceAnalyzerMetrics.sHttpPath + "/"
+ "bulk/bulkId/start";
String actualPath = actionListener.getMetricsPath(startTimeInMills, "bulk", "bulkId", "start");
assertEquals(expectedPath, actualPath);

try {
actionListener.getMetricsPath(startTimeInMills, "bulk");
fail("Negative scenario test: Should have been a RuntimeException");
} catch (RuntimeException ex) {
//- expecting exception...1 values passed; 3 expected
}
}

@Test
public void testOnResponseWithBulkResponse() {
BulkResponse bulkResponse = Mockito.mock(BulkResponse.class);
Mockito.when(bulkResponse.status()).thenReturn(RestStatus.OK);
actionListener.set(RequestType.bulk, listenerId , originalActionListener);
testOnResponse(bulkResponse);
}

@Test
public void testOnResponseWithSearchResponse() {
SearchResponse searchResponse = Mockito.mock(SearchResponse.class);
Mockito.when(searchResponse.status()).thenReturn(RestStatus.OK);
actionListener.set(RequestType.search, listenerId , originalActionListener);
testOnResponse(searchResponse);
}

@Ignore
public class PerformanceAnalyzerActionListenerTests extends CustomMetricsLocationTestBase {
@Test
public void testOnFailureWithElasticsearchException() {
ElasticsearchException exception = Mockito.mock(ElasticsearchException.class);
Mockito.when(exception.status()).thenReturn(RestStatus.INTERNAL_SERVER_ERROR);
actionListener.set(RequestType.search, listenerId , originalActionListener);
actionListener.onFailure(exception);
String[] metricsValues = readMetricsValue();
assertEquals("HTTPRespCode:500", metricsValues[2]);
assertTrue( metricsValues[3].contains("Exception:org.elasticsearch.ElasticsearchException"));
}

@Test
public void testHttpMetrics() {
System.setProperty("performanceanalyzer.metrics.log.enabled", "False");
long startTimeInMills = 1553725339;
PerformanceAnalyzerActionListener performanceanalyzerActionListener = new PerformanceAnalyzerActionListener();
performanceanalyzerActionListener.saveMetricValues("XYZADFAS", startTimeInMills, "bulk", "bulkId", "start");
String fetchedValue = PerformanceAnalyzerMetrics.getMetric(
PluginSettings.instance().getMetricsLocation()
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/threads/http/bulk/bulkId/start");
assertEquals("XYZADFAS", fetchedValue);

String startMetricsValue = performanceanalyzerActionListener.generateStartMetrics(123, "val2", 0).toString();
performanceanalyzerActionListener.saveMetricValues(startMetricsValue, startTimeInMills, "search", "searchId1", "start");
fetchedValue = PerformanceAnalyzerMetrics.getMetric(
PluginSettings.instance().getMetricsLocation()
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/threads/http/search/searchId1/start");
assertEquals(startMetricsValue, fetchedValue);

String finishMetricsValue = performanceanalyzerActionListener.generateFinishMetrics(456, 200, "val4").toString();
performanceanalyzerActionListener.saveMetricValues(finishMetricsValue, startTimeInMills, "search", "searchId1", "finish");
fetchedValue = PerformanceAnalyzerMetrics.getMetric(
PluginSettings.instance().getMetricsLocation()
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/threads/http/search/searchId1/finish");
assertEquals(finishMetricsValue, fetchedValue);

performanceanalyzerActionListener.saveMetricValues(finishMetricsValue, startTimeInMills, "search", "searchId2", "finish");
fetchedValue = PerformanceAnalyzerMetrics.getMetric(
PluginSettings.instance().getMetricsLocation()
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills)+"/threads/http/search/searchId2/finish");
assertEquals(finishMetricsValue, fetchedValue);

PerformanceAnalyzerMetrics.removeMetrics(PluginSettings.instance().getMetricsLocation()
+ PerformanceAnalyzerMetrics.getTimeInterval(startTimeInMills));
public void testOnFailureWithException() {
NullPointerException exception = new NullPointerException();
actionListener.set(RequestType.search, listenerId , originalActionListener);
actionListener.onFailure(exception);
String[] metricsValues = readMetricsValue();
assertEquals("HTTPRespCode:-1", metricsValues[2]);
assertTrue( metricsValues[3].contains("Exception:java.lang.NullPointerException"));
}

private void testOnResponse(ActionResponse response) {
actionListener.onResponse(response);

String[] metricsValues = readMetricsValue();
assertEquals("HTTPRespCode:200", metricsValues[2]);
assertEquals("Exception:", metricsValues[3]);
}

private String[] readMetricsValue() {
List<Event> metrics = TestUtil.readEvents();
assert metrics.size() == 1;
String[] metricsValues = metrics.get(0).value.split("\n");
assert metricsValues.length == 4;
return metricsValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public void init() {

MetricsConfiguration.CONFIG_MAP.put(CacheConfigMetricsCollector.class, MetricsConfiguration.cdefault);
collector = new CacheConfigMetricsCollector();

//clean metricQueue before running every test
TestUtil.readEvents();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public void init() {

MetricsConfiguration.CONFIG_MAP.put(CircuitBreakerCollector.class, MetricsConfiguration.cdefault);
collector = new CircuitBreakerCollector();

//clean metricQueue before running every test
TestUtil.readEvents();
}

@After
Expand Down
Loading