Skip to content

Commit 55637b6

Browse files
committed
Restore + test percentage methods
1 parent e26b62f commit 55637b6

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

server/src/main/java/org/elasticsearch/cluster/ShardHeapUsage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ public void writeTo(StreamOutput out) throws IOException {
4141
public long estimatedFreeBytes() {
4242
return totalBytes - estimatedUsageBytes;
4343
}
44+
45+
public double estimatedFreeBytesAsPercentage() {
46+
return 100.0 - estimatedUsageAsPercentage();
47+
}
48+
49+
public double estimatedUsageAsPercentage() {
50+
return 100.0 * estimatedUsageBytes / (double) totalBytes;
51+
}
4452
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.cluster;
11+
12+
import org.elasticsearch.test.ESTestCase;
13+
14+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
15+
import static org.hamcrest.Matchers.lessThanOrEqualTo;
16+
17+
public class ShardHeapUsageTests extends ESTestCase {
18+
19+
public void testEstimatedUsageAsPercentage() {
20+
final long totalBytes = randomNonNegativeLong();
21+
final long estimatedUsageBytes = randomLongBetween(0, totalBytes);
22+
final ShardHeapUsage shardHeapUsage = new ShardHeapUsage(randomUUID(), randomIdentifier(), totalBytes, estimatedUsageBytes);
23+
assertThat(shardHeapUsage.estimatedFreeBytesAsPercentage(), greaterThanOrEqualTo(0.0));
24+
assertThat(shardHeapUsage.estimatedFreeBytesAsPercentage(), lessThanOrEqualTo(100.0));
25+
assertEquals(shardHeapUsage.estimatedUsageAsPercentage(), 100.0 * estimatedUsageBytes / totalBytes, 0.0001);
26+
}
27+
28+
public void testEstimatedFreeBytesAsPercentage() {
29+
final long totalBytes = randomNonNegativeLong();
30+
final long estimatedUsageBytes = randomLongBetween(0, totalBytes);
31+
final long estimatedFreeBytes = totalBytes - estimatedUsageBytes;
32+
final ShardHeapUsage shardHeapUsage = new ShardHeapUsage(randomUUID(), randomIdentifier(), totalBytes, estimatedUsageBytes);
33+
assertThat(shardHeapUsage.estimatedFreeBytesAsPercentage(), greaterThanOrEqualTo(0.0));
34+
assertThat(shardHeapUsage.estimatedFreeBytesAsPercentage(), lessThanOrEqualTo(100.0));
35+
assertEquals(shardHeapUsage.estimatedFreeBytesAsPercentage(), 100.0 * estimatedFreeBytes / totalBytes, 0.0001);
36+
}
37+
}

0 commit comments

Comments
 (0)