Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions src/main/java/com/volta/HttpSender.java

This file was deleted.

56 changes: 56 additions & 0 deletions src/main/java/com/volta/stats/StatsCollector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.volta.stats;

import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

public class StatsCollector {
private final LongAdder totalRequests = new LongAdder();
private final LongAdder successCount = new LongAdder();
private final LongAdder totalLatencyMs = new LongAdder();
private final LongAccumulator maxLatencyMs = new LongAccumulator(Math::max, Long.MIN_VALUE);
private final LongAccumulator minLatencyMs = new LongAccumulator(Math::min, Long.MAX_VALUE);

public void record(int statusCode, long latencyMs) throws IllegalArgumentException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep in mind that this code is not thread-safe, so errorCount may end up becoming negative

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IllegalArgumentException is unchecked exception

if (statusCode < 100 || statusCode > 599) {
throw new IllegalArgumentException("statusCode must be integer in [100, 599]");
}
if (latencyMs < 0) {
throw new IllegalArgumentException("latencyMs must be non-negative");
}

totalRequests.increment();
if (200 <= statusCode && statusCode < 300) {
successCount.increment();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are 1xx or 3xx statuses as errors?

}
totalLatencyMs.add(latencyMs);
maxLatencyMs.accumulate(latencyMs);
minLatencyMs.accumulate(latencyMs);
}

public StatsSnapshot getSnapshot() {
long snapshotTotalRequests = totalRequests.sum();
long snapshotSuccessCount = successCount.sum();
long snapshotTotalLatencyMs = totalLatencyMs.sum();

long snapshotMinLatencyMs = (snapshotTotalRequests == 0 ? 0 : minLatencyMs.get());
long snapshotMaxLatencyMs = (snapshotTotalRequests == 0 ? 0 : maxLatencyMs.get());
long snapshotAvgLatencyMs =
(snapshotTotalRequests == 0 ? 0 : snapshotTotalLatencyMs / snapshotTotalRequests);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use double-precision arithmetic where possible. Integer arithmetic can introduce significant error


return new StatsSnapshot(
snapshotTotalRequests,
snapshotSuccessCount,
snapshotTotalRequests - snapshotSuccessCount,
snapshotAvgLatencyMs,
snapshotMinLatencyMs,
snapshotMaxLatencyMs);
}

public void reset() {
totalRequests.reset();
successCount.reset();
totalLatencyMs.reset();
minLatencyMs.reset();
maxLatencyMs.reset();
}
Comment on lines +30 to +55
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add method getSnapshotAndReset()

}
9 changes: 9 additions & 0 deletions src/main/java/com/volta/stats/StatsSnapshot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.volta.stats;

public record StatsSnapshot(
long totalRequests,
long successCount,
long errorCount,
long avgLatencyMs,
long minLatencyMs,
long maxLatencyMs) {}
35 changes: 0 additions & 35 deletions src/test/java/com/volta/HttpSenderTest.java

This file was deleted.

Loading
Loading