-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Extract inferece model stats into its own file #134634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3227f90
97f6099
fc5027b
d2f4fa3
cc10a91
5fbb4e5
b5caabc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.inference.usage; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.inference.TaskType; | ||
import org.elasticsearch.xcontent.ToXContentObject; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class ModelStats implements ToXContentObject, Writeable { | ||
|
||
private final String service; | ||
private final TaskType taskType; | ||
private long count; | ||
|
||
public ModelStats(String service, TaskType taskType) { | ||
this(service, taskType, 0L); | ||
} | ||
|
||
public ModelStats(String service, TaskType taskType, long count) { | ||
this.service = service; | ||
this.taskType = taskType; | ||
this.count = count; | ||
} | ||
|
||
public ModelStats(ModelStats stats) { | ||
this(stats.service, stats.taskType, stats.count); | ||
} | ||
|
||
public ModelStats(StreamInput in) throws IOException { | ||
this.service = in.readString(); | ||
this.taskType = in.readEnum(TaskType.class); | ||
this.count = in.readLong(); | ||
} | ||
|
||
public void add() { | ||
count++; | ||
} | ||
|
||
public String service() { | ||
return service; | ||
} | ||
|
||
public TaskType taskType() { | ||
return taskType; | ||
} | ||
|
||
public long count() { | ||
return count; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
addXContentFragment(builder, params); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public void addXContentFragment(XContentBuilder builder, Params params) throws IOException { | ||
builder.field("service", service); | ||
builder.field("task_type", taskType.name()); | ||
builder.field("count", count); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(service); | ||
out.writeEnum(taskType); | ||
out.writeLong(count); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
ModelStats that = (ModelStats) o; | ||
return count == that.count && Objects.equals(service, that.service) && taskType == that.taskType; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(service, taskType, count); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.inference.usage; | ||
|
||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.inference.TaskType; | ||
import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
public class ModelStatsTests extends AbstractWireSerializingTestCase<ModelStats> { | ||
|
||
@Override | ||
protected Writeable.Reader<ModelStats> instanceReader() { | ||
return ModelStats::new; | ||
} | ||
|
||
@Override | ||
protected ModelStats createTestInstance() { | ||
return createRandomInstance(); | ||
} | ||
|
||
@Override | ||
protected ModelStats mutateInstance(ModelStats modelStats) throws IOException { | ||
ModelStats newModelStats = new ModelStats(modelStats); | ||
newModelStats.add(); | ||
return newModelStats; | ||
} | ||
Comment on lines
+28
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation will only test that changes to
|
||
|
||
public static ModelStats createRandomInstance() { | ||
return new ModelStats(randomIdentifier(), TaskType.values()[randomInt(TaskType.values().length - 1)], randomInt(10)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nitpick, but the second argument here would be more readable as |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a very minor point, but could there be a test of this method added to
ModelStatsTests
?