|
| 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 org.elasticsearch.common.Strings; |
| 25 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 26 | +import org.elasticsearch.xcontent.json.JsonXContent; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | + |
| 32 | +public class ExponentialHistogramXContentTests extends ExponentialHistogramTestCase { |
| 33 | + |
| 34 | + public void testEmptyHistogram() { |
| 35 | + ExponentialHistogram emptyHistogram = ExponentialHistogram.empty(); |
| 36 | + assertSerializedHistogram(ExponentialHistogram.empty(), "{\"scale\":" + emptyHistogram.scale() + "}"); |
| 37 | + } |
| 38 | + |
| 39 | + public void testFullHistogram() { |
| 40 | + FixedCapacityExponentialHistogram histo = createAutoReleasedHistogram(100); |
| 41 | + histo.setZeroBucket(new ZeroBucket(0.1234, 42)); |
| 42 | + histo.resetBuckets(7); |
| 43 | + histo.tryAddBucket(-10, 15, false); |
| 44 | + histo.tryAddBucket(10, 5, false); |
| 45 | + histo.tryAddBucket(-11, 10, true); |
| 46 | + histo.tryAddBucket(11, 20, true); |
| 47 | + assertSerializedHistogram( |
| 48 | + histo, |
| 49 | + "{" |
| 50 | + + "\"scale\":7," |
| 51 | + + "\"zero\":{\"count\":42,\"threshold\":0.1234}," |
| 52 | + + "\"positive\":{\"indices\":[-11,11],\"counts\":[10,20]}," |
| 53 | + + "\"negative\":{\"indices\":[-10,10],\"counts\":[15,5]}" |
| 54 | + + "}" |
| 55 | + ); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + public void testOnlyZeroThreshold() { |
| 60 | + FixedCapacityExponentialHistogram histo = createAutoReleasedHistogram(10); |
| 61 | + histo.setZeroBucket(new ZeroBucket(5.0, 0)); |
| 62 | + histo.resetBuckets(3); |
| 63 | + assertSerializedHistogram(histo, "{\"scale\":3,\"zero\":{\"threshold\":5.0}}"); |
| 64 | + } |
| 65 | + |
| 66 | + public void testOnlyZeroCount() { |
| 67 | + FixedCapacityExponentialHistogram histo = createAutoReleasedHistogram(10); |
| 68 | + histo.setZeroBucket(new ZeroBucket(0.0, 7)); |
| 69 | + histo.resetBuckets(2); |
| 70 | + assertSerializedHistogram(histo, "{\"scale\":2,\"zero\":{\"count\":7}}"); |
| 71 | + } |
| 72 | + |
| 73 | + public void testOnlyPositiveBuckets() { |
| 74 | + FixedCapacityExponentialHistogram histo = createAutoReleasedHistogram(10); |
| 75 | + histo.resetBuckets(4); |
| 76 | + histo.tryAddBucket(-1, 3, true); |
| 77 | + histo.tryAddBucket(2, 5, true); |
| 78 | + assertSerializedHistogram(histo, "{\"scale\":4,\"positive\":{\"indices\":[-1,2],\"counts\":[3,5]}}"); |
| 79 | + } |
| 80 | + |
| 81 | + public void testOnlyNegativeBuckets() { |
| 82 | + FixedCapacityExponentialHistogram histo = createAutoReleasedHistogram(10); |
| 83 | + histo.resetBuckets(5); |
| 84 | + histo.tryAddBucket(-1, 4, false); |
| 85 | + histo.tryAddBucket(2, 6, false); |
| 86 | + assertSerializedHistogram(histo, "{\"scale\":5,\"negative\":{\"indices\":[-1,2],\"counts\":[4,6]}}"); |
| 87 | + } |
| 88 | + |
| 89 | + private static void assertSerializedHistogram(ExponentialHistogram histo, String expectedJson) { |
| 90 | + try (XContentBuilder builder = JsonXContent.contentBuilder()) { |
| 91 | + ExponentialHistogramXContent.serialize(builder, histo); |
| 92 | + assertThat(Strings.toString(builder), equalTo(expectedJson)); |
| 93 | + } catch (IOException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments