|
48 | 48 | import org.elasticsearch.client.ml.DeleteJobRequest;
|
49 | 49 | import org.elasticsearch.client.ml.DeleteJobResponse;
|
50 | 50 | import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
|
| 51 | +import org.elasticsearch.client.ml.EstimateMemoryUsageResponse; |
51 | 52 | import org.elasticsearch.client.ml.EvaluateDataFrameRequest;
|
52 | 53 | import org.elasticsearch.client.ml.EvaluateDataFrameResponse;
|
53 | 54 | import org.elasticsearch.client.ml.FindFileStructureRequest;
|
|
194 | 195 | import java.util.concurrent.TimeUnit;
|
195 | 196 | import java.util.stream.Collectors;
|
196 | 197 |
|
| 198 | +import static org.hamcrest.Matchers.allOf; |
197 | 199 | import static org.hamcrest.Matchers.closeTo;
|
198 | 200 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
199 | 201 | import static org.hamcrest.Matchers.equalTo;
|
200 | 202 | import static org.hamcrest.Matchers.greaterThan;
|
201 | 203 | import static org.hamcrest.Matchers.hasSize;
|
| 204 | +import static org.hamcrest.Matchers.lessThan; |
202 | 205 | import static org.hamcrest.core.Is.is;
|
203 | 206 |
|
204 | 207 | public class MlClientDocumentationIT extends ESRestHighLevelClientTestCase {
|
@@ -3262,6 +3265,72 @@ public void onFailure(Exception e) {
|
3262 | 3265 | }
|
3263 | 3266 | }
|
3264 | 3267 |
|
| 3268 | + public void testEstimateMemoryUsage() throws Exception { |
| 3269 | + createIndex("estimate-test-source-index"); |
| 3270 | + BulkRequest bulkRequest = |
| 3271 | + new BulkRequest("estimate-test-source-index") |
| 3272 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 3273 | + for (int i = 0; i < 10; ++i) { |
| 3274 | + bulkRequest.add(new IndexRequest().source(XContentType.JSON, "timestamp", 123456789L, "total", 10L)); |
| 3275 | + } |
| 3276 | + RestHighLevelClient client = highLevelClient(); |
| 3277 | + client.bulk(bulkRequest, RequestOptions.DEFAULT); |
| 3278 | + { |
| 3279 | + // tag::estimate-memory-usage-request |
| 3280 | + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() |
| 3281 | + .setSource(DataFrameAnalyticsSource.builder().setIndex("estimate-test-source-index").build()) |
| 3282 | + .setAnalysis(OutlierDetection.createDefault()) |
| 3283 | + .build(); |
| 3284 | + PutDataFrameAnalyticsRequest request = new PutDataFrameAnalyticsRequest(config); // <1> |
| 3285 | + // end::estimate-memory-usage-request |
| 3286 | + |
| 3287 | + // tag::estimate-memory-usage-execute |
| 3288 | + EstimateMemoryUsageResponse response = client.machineLearning().estimateMemoryUsage(request, RequestOptions.DEFAULT); |
| 3289 | + // end::estimate-memory-usage-execute |
| 3290 | + |
| 3291 | + // tag::estimate-memory-usage-response |
| 3292 | + ByteSizeValue expectedMemoryWithoutDisk = response.getExpectedMemoryWithoutDisk(); // <1> |
| 3293 | + ByteSizeValue expectedMemoryWithDisk = response.getExpectedMemoryWithDisk(); // <2> |
| 3294 | + // end::estimate-memory-usage-response |
| 3295 | + |
| 3296 | + // We are pretty liberal here as this test does not aim at verifying concrete numbers but rather end-to-end user workflow. |
| 3297 | + ByteSizeValue lowerBound = new ByteSizeValue(1, ByteSizeUnit.KB); |
| 3298 | + ByteSizeValue upperBound = new ByteSizeValue(1, ByteSizeUnit.GB); |
| 3299 | + assertThat(expectedMemoryWithoutDisk, allOf(greaterThan(lowerBound), lessThan(upperBound))); |
| 3300 | + assertThat(expectedMemoryWithDisk, allOf(greaterThan(lowerBound), lessThan(upperBound))); |
| 3301 | + } |
| 3302 | + { |
| 3303 | + DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder() |
| 3304 | + .setSource(DataFrameAnalyticsSource.builder().setIndex("estimate-test-source-index").build()) |
| 3305 | + .setAnalysis(OutlierDetection.createDefault()) |
| 3306 | + .build(); |
| 3307 | + PutDataFrameAnalyticsRequest request = new PutDataFrameAnalyticsRequest(config); |
| 3308 | + // tag::estimate-memory-usage-execute-listener |
| 3309 | + ActionListener<EstimateMemoryUsageResponse> listener = new ActionListener<>() { |
| 3310 | + @Override |
| 3311 | + public void onResponse(EstimateMemoryUsageResponse response) { |
| 3312 | + // <1> |
| 3313 | + } |
| 3314 | + |
| 3315 | + @Override |
| 3316 | + public void onFailure(Exception e) { |
| 3317 | + // <2> |
| 3318 | + } |
| 3319 | + }; |
| 3320 | + // end::estimate-memory-usage-execute-listener |
| 3321 | + |
| 3322 | + // Replace the empty listener by a blocking listener in test |
| 3323 | + final CountDownLatch latch = new CountDownLatch(1); |
| 3324 | + listener = new LatchedActionListener<>(listener, latch); |
| 3325 | + |
| 3326 | + // tag::estimate-memory-usage-execute-async |
| 3327 | + client.machineLearning().estimateMemoryUsageAsync(request, RequestOptions.DEFAULT, listener); // <1> |
| 3328 | + // end::estimate-memory-usage-execute-async |
| 3329 | + |
| 3330 | + assertTrue(latch.await(30L, TimeUnit.SECONDS)); |
| 3331 | + } |
| 3332 | + } |
| 3333 | + |
3265 | 3334 | public void testCreateFilter() throws Exception {
|
3266 | 3335 | RestHighLevelClient client = highLevelClient();
|
3267 | 3336 | {
|
|
0 commit comments