-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report timing stats as part of the Job stats response (#42709)
- Loading branch information
1 parent
eaf76d2
commit 38f3a74
Showing
29 changed files
with
1,052 additions
and
102 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
...nt/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/process/TimingStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.ml.job.process; | ||
|
||
import org.elasticsearch.client.ml.job.config.Job; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; | ||
|
||
/** | ||
* Stats that give more insight into timing of various operations performed as part of anomaly detection job. | ||
*/ | ||
public class TimingStats implements ToXContentObject { | ||
|
||
public static final ParseField BUCKET_COUNT = new ParseField("bucket_count"); | ||
public static final ParseField MIN_BUCKET_PROCESSING_TIME_MS = new ParseField("minimum_bucket_processing_time_ms"); | ||
public static final ParseField MAX_BUCKET_PROCESSING_TIME_MS = new ParseField("maximum_bucket_processing_time_ms"); | ||
public static final ParseField AVG_BUCKET_PROCESSING_TIME_MS = new ParseField("average_bucket_processing_time_ms"); | ||
|
||
public static final ConstructingObjectParser<TimingStats, Void> PARSER = | ||
new ConstructingObjectParser<>( | ||
"timing_stats", | ||
true, | ||
args -> new TimingStats((String) args[0], (long) args[1], (Double) args[2], (Double) args[3], (Double) args[4])); | ||
|
||
static { | ||
PARSER.declareString(constructorArg(), Job.ID); | ||
PARSER.declareLong(constructorArg(), BUCKET_COUNT); | ||
PARSER.declareDouble(optionalConstructorArg(), MIN_BUCKET_PROCESSING_TIME_MS); | ||
PARSER.declareDouble(optionalConstructorArg(), MAX_BUCKET_PROCESSING_TIME_MS); | ||
PARSER.declareDouble(optionalConstructorArg(), AVG_BUCKET_PROCESSING_TIME_MS); | ||
} | ||
|
||
private final String jobId; | ||
private long bucketCount; | ||
private Double minBucketProcessingTimeMs; | ||
private Double maxBucketProcessingTimeMs; | ||
private Double avgBucketProcessingTimeMs; | ||
|
||
public TimingStats( | ||
String jobId, | ||
long bucketCount, | ||
@Nullable Double minBucketProcessingTimeMs, | ||
@Nullable Double maxBucketProcessingTimeMs, | ||
@Nullable Double avgBucketProcessingTimeMs) { | ||
this.jobId = jobId; | ||
this.bucketCount = bucketCount; | ||
this.minBucketProcessingTimeMs = minBucketProcessingTimeMs; | ||
this.maxBucketProcessingTimeMs = maxBucketProcessingTimeMs; | ||
this.avgBucketProcessingTimeMs = avgBucketProcessingTimeMs; | ||
} | ||
|
||
public String getJobId() { | ||
return jobId; | ||
} | ||
|
||
public long getBucketCount() { | ||
return bucketCount; | ||
} | ||
|
||
public Double getMinBucketProcessingTimeMs() { | ||
return minBucketProcessingTimeMs; | ||
} | ||
|
||
public Double getMaxBucketProcessingTimeMs() { | ||
return maxBucketProcessingTimeMs; | ||
} | ||
|
||
public Double getAvgBucketProcessingTimeMs() { | ||
return avgBucketProcessingTimeMs; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(Job.ID.getPreferredName(), jobId); | ||
builder.field(BUCKET_COUNT.getPreferredName(), bucketCount); | ||
if (minBucketProcessingTimeMs != null) { | ||
builder.field(MIN_BUCKET_PROCESSING_TIME_MS.getPreferredName(), minBucketProcessingTimeMs); | ||
} | ||
if (maxBucketProcessingTimeMs != null) { | ||
builder.field(MAX_BUCKET_PROCESSING_TIME_MS.getPreferredName(), maxBucketProcessingTimeMs); | ||
} | ||
if (avgBucketProcessingTimeMs != null) { | ||
builder.field(AVG_BUCKET_PROCESSING_TIME_MS.getPreferredName(), avgBucketProcessingTimeMs); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == this) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
TimingStats that = (TimingStats) o; | ||
return Objects.equals(this.jobId, that.jobId) | ||
&& this.bucketCount == that.bucketCount | ||
&& Objects.equals(this.minBucketProcessingTimeMs, that.minBucketProcessingTimeMs) | ||
&& Objects.equals(this.maxBucketProcessingTimeMs, that.maxBucketProcessingTimeMs) | ||
&& Objects.equals(this.avgBucketProcessingTimeMs, that.avgBucketProcessingTimeMs); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(jobId, bucketCount, minBucketProcessingTimeMs, maxBucketProcessingTimeMs, avgBucketProcessingTimeMs); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Strings.toString(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...st-high-level/src/test/java/org/elasticsearch/client/ml/job/process/TimingStatsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.ml.job.process; | ||
|
||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.test.AbstractXContentTestCase; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class TimingStatsTests extends AbstractXContentTestCase<TimingStats> { | ||
|
||
private static final String JOB_ID = "my-job-id"; | ||
|
||
public static TimingStats createTestInstance(String jobId) { | ||
return new TimingStats( | ||
jobId, | ||
randomLong(), | ||
randomBoolean() ? null : randomDouble(), | ||
randomBoolean() ? null : randomDouble(), | ||
randomBoolean() ? null : randomDouble()); | ||
} | ||
|
||
@Override | ||
public TimingStats createTestInstance() { | ||
return createTestInstance(randomAlphaOfLength(10)); | ||
} | ||
|
||
@Override | ||
protected TimingStats doParseInstance(XContentParser parser) { | ||
return TimingStats.PARSER.apply(parser, null); | ||
} | ||
|
||
@Override | ||
protected boolean supportsUnknownFields() { | ||
return true; | ||
} | ||
|
||
public void testConstructor() { | ||
TimingStats stats = new TimingStats(JOB_ID, 7, 1.0, 2.0, 1.23); | ||
|
||
assertThat(stats.getJobId(), equalTo(JOB_ID)); | ||
assertThat(stats.getBucketCount(), equalTo(7L)); | ||
assertThat(stats.getMinBucketProcessingTimeMs(), equalTo(1.0)); | ||
assertThat(stats.getMaxBucketProcessingTimeMs(), equalTo(2.0)); | ||
assertThat(stats.getAvgBucketProcessingTimeMs(), equalTo(1.23)); | ||
} | ||
|
||
public void testConstructor_NullValues() { | ||
TimingStats stats = new TimingStats(JOB_ID, 7, null, null, null); | ||
|
||
assertThat(stats.getJobId(), equalTo(JOB_ID)); | ||
assertThat(stats.getBucketCount(), equalTo(7L)); | ||
assertNull(stats.getMinBucketProcessingTimeMs()); | ||
assertNull(stats.getMaxBucketProcessingTimeMs()); | ||
assertNull(stats.getAvgBucketProcessingTimeMs()); | ||
} | ||
|
||
public void testEquals() { | ||
TimingStats stats1 = new TimingStats(JOB_ID, 7, 1.0, 2.0, 1.23); | ||
TimingStats stats2 = new TimingStats(JOB_ID, 7, 1.0, 2.0, 1.23); | ||
TimingStats stats3 = new TimingStats(JOB_ID, 7, 1.0, 3.0, 1.23); | ||
|
||
assertTrue(stats1.equals(stats1)); | ||
assertTrue(stats1.equals(stats2)); | ||
assertFalse(stats2.equals(stats3)); | ||
} | ||
|
||
public void testHashCode() { | ||
TimingStats stats1 = new TimingStats(JOB_ID, 7, 1.0, 2.0, 1.23); | ||
TimingStats stats2 = new TimingStats(JOB_ID, 7, 1.0, 2.0, 1.23); | ||
TimingStats stats3 = new TimingStats(JOB_ID, 7, 1.0, 3.0, 1.23); | ||
|
||
assertEquals(stats1.hashCode(), stats1.hashCode()); | ||
assertEquals(stats1.hashCode(), stats2.hashCode()); | ||
assertNotEquals(stats2.hashCode(), stats3.hashCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.