chore: Bump version to 1.6.19 and refactor AgentRequestStatistic for improved instantiation and duration handling

This commit is contained in:
extio-de
2025-09-27 08:48:02 +02:00
parent b5f9a21f8b
commit d77c11e809
3 changed files with 31 additions and 10 deletions

View File

@@ -4,7 +4,7 @@
<artifactId>lm-lib</artifactId> <artifactId>lm-lib</artifactId>
<groupId>de.extio</groupId> <groupId>de.extio</groupId>
<name>lm-lib</name> <name>lm-lib</name>
<version>1.6.18</version> <version>1.6.19</version>
<properties> <properties>
<java.version>21</java.version> <java.version>21</java.version>

View File

@@ -45,19 +45,19 @@ public final class AgentContext {
public AgentContext() { public AgentContext() {
this.agents = new HashMap<>(); this.agents = new HashMap<>();
this.context = new ConcurrentHashMap<>(); this.context = new ConcurrentHashMap<>();
this.requestStatistic = new AgentRequestStatistic(); this.requestStatistic = AgentRequestStatistic.create();
} }
public AgentContext(final Collection<? extends BaseAgent> agents) { public AgentContext(final Collection<? extends BaseAgent> agents) {
this.agents = agents.stream().collect(Collectors.toMap(BaseAgent::name, agent -> agent)); this.agents = agents.stream().collect(Collectors.toMap(BaseAgent::name, agent -> agent));
this.context = new ConcurrentHashMap<>(); this.context = new ConcurrentHashMap<>();
this.requestStatistic = new AgentRequestStatistic(); this.requestStatistic = AgentRequestStatistic.create();
} }
public AgentContext(final Map<String, ? extends BaseAgent> agents) { public AgentContext(final Map<String, ? extends BaseAgent> agents) {
this.agents = agents; this.agents = agents;
this.context = new ConcurrentHashMap<>(); this.context = new ConcurrentHashMap<>();
this.requestStatistic = new AgentRequestStatistic(); this.requestStatistic = AgentRequestStatistic.create();
} }
public AgentContext(final AgentContext other) { public AgentContext(final AgentContext other) {

View File

@@ -11,6 +11,20 @@ import de.extio.lmlib.client.Completion;
public final class AgentRequestStatistic { 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 requests = new AtomicInteger();
private final AtomicInteger cachedPrompts = new AtomicInteger(); private final AtomicInteger cachedPrompts = new AtomicInteger();
@@ -19,7 +33,7 @@ public final class AgentRequestStatistic {
private final AtomicLong outTokens = new AtomicLong(); private final AtomicLong outTokens = new AtomicLong();
private volatile Instant start = Instant.now(); private volatile Instant start;
private volatile Duration requestDuration = Duration.ofMillis(0L); private volatile Duration requestDuration = Duration.ofMillis(0L);
@@ -46,15 +60,20 @@ public final class AgentRequestStatistic {
} }
public Duration getEffectiveDuration() { public Duration getEffectiveDuration() {
if (this.start == null) {
return null;
}
return Duration.between(this.start, Instant.now()); return Duration.between(this.start, Instant.now());
} }
public String getTps() { 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() { 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() { public BigDecimal getCost() {
@@ -82,7 +101,7 @@ public final class AgentRequestStatistic {
this.getInTokens().addAndGet(other.getInTokens().get()); this.getInTokens().addAndGet(other.getInTokens().get());
this.getOutTokens().addAndGet(other.getOutTokens().get()); this.getOutTokens().addAndGet(other.getOutTokens().get());
synchronized (this) { synchronized (this) {
if (other.start.isBefore(this.start)) { if (this.start != null && other.start.isBefore(this.start)) {
this.start = other.start; this.start = other.start;
} }
this.requestDuration = this.requestDuration.plus(other.getRequestDuration()); this.requestDuration = this.requestDuration.plus(other.getRequestDuration());
@@ -104,8 +123,10 @@ public final class AgentRequestStatistic {
builder.append(", requestDuration="); builder.append(", requestDuration=");
builder.append(this.requestDuration); builder.append(this.requestDuration);
builder.append(", getEffectiveDuration()="); builder.append(", getEffectiveDuration()=");
builder.append(this.getEffectiveDuration()); if (this.start != null) {
builder.append(", getTps()="); builder.append(this.getEffectiveDuration());
builder.append(", getTps()=");
}
builder.append(this.getTps()); builder.append(this.getTps());
builder.append(", getOutTps()="); builder.append(", getOutTps()=");
builder.append(this.getOutTps()); builder.append(this.getOutTps());