Skip to content

Commit 3c264cf

Browse files
authored
Add toString implementation to exponential histograms (#133969)
1 parent 7e5d81b commit 3c264cf

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/AbstractExponentialHistogram.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,54 @@ public boolean equals(Object obj) {
4646
}
4747
return false;
4848
}
49+
50+
@Override
51+
public String toString() {
52+
StringBuilder sb = new StringBuilder(getClassNameWithoutPackage()).append("{");
53+
sb.append("scale=").append(scale());
54+
sb.append(", sum=").append(sum());
55+
sb.append(", valueCount=").append(valueCount());
56+
sb.append(", min=").append(min());
57+
sb.append(", max=").append(max());
58+
ZeroBucket zb = zeroBucket();
59+
if (zb.zeroThreshold() != 0) {
60+
sb.append(", zeroThreshold=").append(zb.zeroThreshold());
61+
}
62+
if (zb.count() != 0) {
63+
sb.append(", zeroCount=").append(zb.count());
64+
}
65+
BucketIterator neg = negativeBuckets().iterator();
66+
if (neg.hasNext()) {
67+
sb.append(", negative=[");
68+
appendsBucketsAsString(sb, neg);
69+
sb.append("]");
70+
}
71+
BucketIterator pos = positiveBuckets().iterator();
72+
if (pos.hasNext()) {
73+
sb.append(", positive=[");
74+
appendsBucketsAsString(sb, pos);
75+
sb.append("]");
76+
}
77+
sb.append("}");
78+
return sb.toString();
79+
}
80+
81+
private String getClassNameWithoutPackage() {
82+
String fqn = getClass().getName();
83+
int lastDot = fqn.lastIndexOf('.');
84+
return fqn.substring(lastDot + 1);
85+
}
86+
87+
private static void appendsBucketsAsString(StringBuilder out, BucketIterator it) {
88+
boolean first = true;
89+
while (it.hasNext()) {
90+
if (first) {
91+
first = false;
92+
} else {
93+
out.append(", ");
94+
}
95+
out.append(it.peekIndex()).append(": ").append(it.peekCount());
96+
it.advance();
97+
}
98+
}
4999
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright Elasticsearch B.V., and/or licensed to Elasticsearch B.V.
3+
* under one or more license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* This file is based on a modification of https://github.com/open-telemetry/opentelemetry-java which is licensed under the Apache 2.0 License.
20+
*/
21+
22+
package org.elasticsearch.exponentialhistogram;
23+
24+
import static org.hamcrest.Matchers.equalTo;
25+
26+
public class ExponentialHistogramToStringTests extends ExponentialHistogramTestCase {
27+
28+
public void testFullHistogram() {
29+
try (FixedCapacityExponentialHistogram histogram = FixedCapacityExponentialHistogram.create(10, breaker())) {
30+
histogram.setZeroBucket(ZeroBucket.create(42.0, 7));
31+
histogram.resetBuckets(10);
32+
histogram.setMin(-100);
33+
histogram.setMax(200);
34+
histogram.setSum(1234.5);
35+
histogram.tryAddBucket(2, 3, false); // negative bucket
36+
histogram.tryAddBucket(3, 4, false); // negative bucket
37+
histogram.tryAddBucket(4, 2, false); // negative bucket
38+
histogram.tryAddBucket(1, 5, true); // positive bucket
39+
histogram.tryAddBucket(5, 7, true); // positive bucket
40+
histogram.tryAddBucket(6, 1, true); // positive bucket
41+
String expected = "FixedCapacityExponentialHistogram{"
42+
+ "scale=10, sum=1234.5, valueCount=29, min=-100.0, max=200.0, zeroThreshold=42.0, zeroCount=7,"
43+
+ " negative=[2: 3, 3: 4, 4: 2], positive=[1: 5, 5: 7, 6: 1]}";
44+
assertThat(histogram.toString(), equalTo(expected));
45+
}
46+
}
47+
48+
public void testEmptyHistogram() {
49+
ExponentialHistogram emptyHistogram = ExponentialHistogram.empty();
50+
String expected = "EmptyExponentialHistogram{scale=" + emptyHistogram.scale() + ", sum=0.0, valueCount=0, min=NaN, max=NaN}";
51+
assertThat(emptyHistogram.toString(), equalTo(expected));
52+
}
53+
}

0 commit comments

Comments
 (0)