diff --git a/pom.xml b/pom.xml index e2f9e91..c1e8c1b 100755 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ lm-lib de.extio lm-lib - 1.6.18 + 1.6.19 21 diff --git a/src/main/java/de/extio/lmlib/agent/AgentContext.java b/src/main/java/de/extio/lmlib/agent/AgentContext.java index 3089178..9c38c96 100755 --- a/src/main/java/de/extio/lmlib/agent/AgentContext.java +++ b/src/main/java/de/extio/lmlib/agent/AgentContext.java @@ -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 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 agents) { this.agents = agents; this.context = new ConcurrentHashMap<>(); - this.requestStatistic = new AgentRequestStatistic(); + this.requestStatistic = AgentRequestStatistic.create(); } public AgentContext(final AgentContext other) { diff --git a/src/main/java/de/extio/lmlib/agent/AgentRequestStatistic.java b/src/main/java/de/extio/lmlib/agent/AgentRequestStatistic.java index 43e1877..672c56d 100755 --- a/src/main/java/de/extio/lmlib/agent/AgentRequestStatistic.java +++ b/src/main/java/de/extio/lmlib/agent/AgentRequestStatistic.java @@ -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()="); - builder.append(this.getEffectiveDuration()); - builder.append(", getTps()="); + if (this.start != null) { + builder.append(this.getEffectiveDuration()); + builder.append(", getTps()="); + } builder.append(this.getTps()); builder.append(", getOutTps()="); builder.append(this.getOutTps());