我已将 java 集成测试移动到使用 elasticsearch 测试容器,而不是使用嵌入式 elasticsearch。测试速度变慢了 1 小时,这对生产力造成了巨大影响。我正在寻找加快速度的方法。
我尝试reuse
在 Elasticsearch 容器上使用参数,但这并没有产生任何影响。我的最新配置是
private static final String ELASTICSEARCH_VERSION = "7.11.2";
private static ElasticsearchContainer elasticsearchContainer;
private static final DockerImageName ELASTICSEARCH_IMAGE =
DockerImageName
.parse("docker.elastic.co/elasticsearch/elasticsearch")
.withTag(ELASTICSEARCH_VERSION);
elasticsearchContainer = new ElasticsearchContainer(ELASTICSEARCH_IMAGE)
.withEnv("foo", "bar").withSharedMemorySize(1000000000L);
elasticsearchContainer.addExposedPorts(9200, 9300);
elasticsearchContainer.withStartupTimeout(Duration.of(5, ChronoUnit.MINUTES));
elasticsearchContainer.start();
private static RestHighLevelClient getRestHighLevelClient(ElasticsearchContainer container) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME,
ELASTICSEARCH_PASSWORD));
RestClientBuilder restClientBuilder = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider)
.setKeepAliveStrategy((response, context) -> 3 * 60 * 1000));
// Try to prevent SocketTimeoutException when fetching larger batch size
restClientBuilder.setRequestConfigCallback(
requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(2 * 60 * 1000));
return new RestHighLevelClient(restClientBuilder);
}
Gradle 配置(gradle daemon 崩溃一次,因此增加到 2g)
org.gradle.jvmargs=-Xms2g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
关于使测试更快的任何建议?