Skip to content

Commit

Permalink
tests tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
ywangd committed Sep 26, 2023
1 parent 2873701 commit 4504e52
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
25 changes: 25 additions & 0 deletions server/src/main/java/org/elasticsearch/http/HttpRouteStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.IntStream;

public record HttpRouteStats(
Expand Down Expand Up @@ -120,4 +122,27 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeVLongArray(responseSizeHistogram);
out.writeVLongArray(responseTimeHistogram);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HttpRouteStats that = (HttpRouteStats) o;
return requestCount == that.requestCount
&& totalRequestSize == that.totalRequestSize
&& responseCount == that.responseCount
&& totalResponseSize == that.totalResponseSize
&& Arrays.equals(requestSizeHistogram, that.requestSizeHistogram)
&& Arrays.equals(responseSizeHistogram, that.responseSizeHistogram)
&& Arrays.equals(responseTimeHistogram, that.responseTimeHistogram);
}

@Override
public int hashCode() {
int result = Objects.hash(requestCount, totalRequestSize, responseCount, totalResponseSize);
result = 31 * result + Arrays.hashCode(requestSizeHistogram);
result = 31 * result + Arrays.hashCode(responseSizeHistogram);
result = 31 * result + Arrays.hashCode(responseTimeHistogram);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ public void sendResponse(RestResponse response) {
try {
close();
methodHandlers.addRequestStats(contentLength);
methodHandlers.addResponseTime(System.currentTimeMillis() - startTime);
if (response.isChunked() == false) {
methodHandlers.addResponseStats(response.content().length());
} else {
Expand All @@ -783,7 +784,6 @@ public void sendResponse(RestResponse response) {
delegate.sendResponse(response);
success = true;
} finally {
methodHandlers.addResponseTime(System.currentTimeMillis() - startTime);
if (success == false) {
releaseOutputBuffer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.core.Tuple;
import org.elasticsearch.discovery.DiscoveryStats;
import org.elasticsearch.http.HttpStats;
import org.elasticsearch.http.HttpStatsTests;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.index.bulk.stats.BulkStats;
Expand Down Expand Up @@ -859,7 +860,12 @@ public static NodeStats createNodeStats() {
);
clientStats.add(cs);
}
httpStats = new HttpStats(randomNonNegativeLong(), randomNonNegativeLong(), clientStats, Map.of());
httpStats = new HttpStats(
randomNonNegativeLong(),
randomNonNegativeLong(),
clientStats,
randomMap(1, 3, () -> new Tuple<>(randomAlphaOfLength(10), HttpStatsTests.randomHttpRouteStats()))
);
}
AllCircuitBreakerStats allCircuitBreakerStats = null;
if (frequently()) {
Expand Down
19 changes: 18 additions & 1 deletion server/src/test/java/org/elasticsearch/http/HttpStatsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.test.ESTestCase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
Expand All @@ -32,6 +34,9 @@ public void testMerge() {
assertEquals(merged.getTotalOpen(), first.getTotalOpen() + second.getTotalOpen());
assertThat(merged.getClientStats(), hasSize(first.getClientStats().size() + second.getClientStats().size()));
assertEquals(merged.getClientStats(), Stream.concat(first.getClientStats().stream(), second.getClientStats().stream()).toList());
final Map<String, HttpRouteStats> m = new HashMap<>(first.httpRouteStats());
second.httpRouteStats().forEach((k, v) -> m.merge(k, v, HttpRouteStats::merge));
assertEquals(merged.httpRouteStats(), m);
}

public void testToXContent() {
Expand Down Expand Up @@ -74,7 +79,7 @@ public static HttpStats randomHttpStats() {
randomLongBetween(0, Long.MAX_VALUE),
randomLongBetween(0, Long.MAX_VALUE),
IntStream.range(1, randomIntBetween(2, 10)).mapToObj(HttpStatsTests::randomClients).toList(),
Map.of()
randomMap(1, 3, () -> new Tuple<>(randomAlphaOfLength(10), randomHttpRouteStats()))
);
}

Expand All @@ -94,4 +99,16 @@ public static HttpStats.ClientStats randomClients(int i) {
randomLong()
);
}

public static HttpRouteStats randomHttpRouteStats() {
return new HttpRouteStats(
randomLongBetween(0, 99),
randomLongBetween(0, 9999),
IntStream.range(0, 28).mapToLong(i -> randomLongBetween(0, 42)).toArray(),
randomLongBetween(0, 99),
randomLongBetween(0, 9999),
IntStream.range(0, 28).mapToLong(i -> randomLongBetween(0, 42)).toArray(),
IntStream.range(0, 18).mapToLong(i -> randomLongBetween(0, 42)).toArray()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.client.internal.node.NodeClient;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.http.HttpRouteStats;
import org.elasticsearch.http.HttpStats;
import org.elasticsearch.http.HttpStatsTests;
import org.elasticsearch.test.ESTestCase;
Expand All @@ -21,6 +22,7 @@

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -90,7 +92,11 @@ public void testHttpResponseMapper() {
.map(Collection::stream)
.reduce(Stream.of(), Stream::concat)
.toList(),
Map.of()
nodeStats.stream().map(NodeStats::getHttp).map(HttpStats::httpRouteStats).reduce(Map.of(), (l, r) -> {
final var m = new HashMap<>(l);
r.forEach((k, v) -> m.merge(k, v, HttpRouteStats::merge));
return Map.copyOf(m);
})
)
);
}
Expand Down

0 comments on commit 4504e52

Please sign in to comment.