chore: Bump version to 1.6.19 and refactor AgentRequestStatistic for improved instantiation and duration handling
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<artifactId>lm-lib</artifactId>
|
||||
<groupId>de.extio</groupId>
|
||||
<name>lm-lib</name>
|
||||
<version>1.6.18</version>
|
||||
<version>1.6.19</version>
|
||||
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
|
||||
@@ -45,19 +45,19 @@ public final class AgentContext {
|
||||
public AgentContext() {
|
||||
this.agents = new HashMap<>();
|
||||
this.context = new ConcurrentHashMap<>();
|
||||
this.requestStatistic = new AgentRequestStatistic();
|
||||
this.requestStatistic = AgentRequestStatistic.create();
|
||||
}
|
||||
|
||||
public AgentContext(final Collection<? extends BaseAgent> agents) {
|
||||
this.agents = agents.stream().collect(Collectors.toMap(BaseAgent::name, agent -> agent));
|
||||
this.context = new ConcurrentHashMap<>();
|
||||
this.requestStatistic = new AgentRequestStatistic();
|
||||
this.requestStatistic = AgentRequestStatistic.create();
|
||||
}
|
||||
|
||||
public AgentContext(final Map<String, ? extends BaseAgent> agents) {
|
||||
this.agents = agents;
|
||||
this.context = new ConcurrentHashMap<>();
|
||||
this.requestStatistic = new AgentRequestStatistic();
|
||||
this.requestStatistic = AgentRequestStatistic.create();
|
||||
}
|
||||
|
||||
public AgentContext(final AgentContext other) {
|
||||
|
||||
@@ -11,6 +11,20 @@ import de.extio.lmlib.client.Completion;
|
||||
|
||||
public final class AgentRequestStatistic {
|
||||
|
||||
public static AgentRequestStatistic create() {
|
||||
return new AgentRequestStatistic(true);
|
||||
}
|
||||
|
||||
public static AgentRequestStatistic createWithoutEffectiveDuration() {
|
||||
return new AgentRequestStatistic(false);
|
||||
}
|
||||
|
||||
private AgentRequestStatistic(final boolean withEffectiveDuration) {
|
||||
if (withEffectiveDuration) {
|
||||
this.start = Instant.now();
|
||||
}
|
||||
}
|
||||
|
||||
private final AtomicInteger requests = new AtomicInteger();
|
||||
|
||||
private final AtomicInteger cachedPrompts = new AtomicInteger();
|
||||
@@ -19,7 +33,7 @@ public final class AgentRequestStatistic {
|
||||
|
||||
private final AtomicLong outTokens = new AtomicLong();
|
||||
|
||||
private volatile Instant start = Instant.now();
|
||||
private volatile Instant start;
|
||||
|
||||
private volatile Duration requestDuration = Duration.ofMillis(0L);
|
||||
|
||||
@@ -46,15 +60,20 @@ public final class AgentRequestStatistic {
|
||||
}
|
||||
|
||||
public Duration getEffectiveDuration() {
|
||||
if (this.start == null) {
|
||||
return null;
|
||||
}
|
||||
return Duration.between(this.start, Instant.now());
|
||||
}
|
||||
|
||||
public String getTps() {
|
||||
return new DecimalFormat("#.##").format((double) (this.inTokens.get() + this.outTokens.get()) / (double) this.getEffectiveDuration().toMillis() * 1000.0);
|
||||
final var duration = this.start == null ? this.getRequestDuration() : this.getEffectiveDuration();
|
||||
return new DecimalFormat("#.##").format((double) (this.inTokens.get() + this.outTokens.get()) / (double) duration.toMillis() * 1000.0);
|
||||
}
|
||||
|
||||
public String getOutTps() {
|
||||
return new DecimalFormat("#.##").format((double) this.outTokens.get() / (double) this.getEffectiveDuration().toMillis() * 1000.0);
|
||||
final var duration = this.start == null ? this.getRequestDuration() : this.getEffectiveDuration();
|
||||
return new DecimalFormat("#.##").format((double) this.outTokens.get() / (double) duration.toMillis() * 1000.0);
|
||||
}
|
||||
|
||||
public BigDecimal getCost() {
|
||||
@@ -82,7 +101,7 @@ public final class AgentRequestStatistic {
|
||||
this.getInTokens().addAndGet(other.getInTokens().get());
|
||||
this.getOutTokens().addAndGet(other.getOutTokens().get());
|
||||
synchronized (this) {
|
||||
if (other.start.isBefore(this.start)) {
|
||||
if (this.start != null && other.start.isBefore(this.start)) {
|
||||
this.start = other.start;
|
||||
}
|
||||
this.requestDuration = this.requestDuration.plus(other.getRequestDuration());
|
||||
@@ -104,8 +123,10 @@ public final class AgentRequestStatistic {
|
||||
builder.append(", requestDuration=");
|
||||
builder.append(this.requestDuration);
|
||||
builder.append(", getEffectiveDuration()=");
|
||||
if (this.start != null) {
|
||||
builder.append(this.getEffectiveDuration());
|
||||
builder.append(", getTps()=");
|
||||
}
|
||||
builder.append(this.getTps());
|
||||
builder.append(", getOutTps()=");
|
||||
builder.append(this.getOutTps());
|
||||
|
||||
Reference in New Issue
Block a user