25
25
26
26
import java .util .ArrayList ;
27
27
import java .util .Arrays ;
28
- import java .util .Collections ;
29
28
import java .util .List ;
30
29
31
30
import static org .elasticsearch .exponentialhistogram .ExponentialHistogram .MAX_INDEX ;
@@ -61,7 +60,8 @@ public ExponentialHistogramEqualityTests(Modification modification) {
61
60
62
61
public void testEquality () {
63
62
ExponentialHistogram histo = randomHistogram ();
64
- ExponentialHistogram copy = copyWithModification (histo , null );
63
+ ReleasableExponentialHistogram copy = ExponentialHistogram .builder (histo , breaker ()).build ();
64
+ autoReleaseOnTestEnd (copy );
65
65
ExponentialHistogram modifiedCopy = copyWithModification (histo , modification );
66
66
67
67
assertThat (histo , equalTo (copy ));
@@ -86,46 +86,31 @@ private ExponentialHistogram randomHistogram() {
86
86
}
87
87
88
88
private ExponentialHistogram copyWithModification (ExponentialHistogram toCopy , Modification modification ) {
89
- int totalBucketCount = getBucketCount (toCopy .positiveBuckets (), toCopy .negativeBuckets ());
90
- FixedCapacityExponentialHistogram copy = createAutoReleasedHistogram (totalBucketCount + 2 );
91
- if (modification == Modification .SCALE ) {
92
- copy .resetBuckets ((int ) createRandomLongBetweenOtherThan (MIN_SCALE , MAX_SCALE , toCopy .scale ()));
93
- } else {
94
- copy .resetBuckets (toCopy .scale ());
95
- }
96
- if (modification == Modification .SUM ) {
97
- copy .setSum (randomDouble ());
98
- } else {
99
- copy .setSum (toCopy .sum ());
100
- }
101
- if (modification == Modification .MIN ) {
102
- copy .setMin (randomDouble ());
103
- } else {
104
- copy .setMin (toCopy .min ());
89
+ ExponentialHistogramBuilder copyBuilder = ExponentialHistogram .builder (toCopy , breaker ());
90
+ switch (modification ) {
91
+ case SCALE -> copyBuilder .scale ((int ) createRandomLongBetweenOtherThan (MIN_SCALE , MAX_SCALE , toCopy .scale ()));
92
+ case SUM -> copyBuilder .sum (randomDouble ());
93
+ case MIN -> copyBuilder .min (randomDouble ());
94
+ case MAX -> copyBuilder .max (randomDouble ());
95
+ case ZERO_THRESHOLD -> copyBuilder .zeroBucket (ZeroBucket .create (randomDouble (), toCopy .zeroBucket ().count ()));
96
+ case ZERO_COUNT -> copyBuilder .zeroBucket (
97
+ ZeroBucket .create (
98
+ toCopy .zeroBucket ().zeroThreshold (),
99
+ createRandomLongBetweenOtherThan (0 , Long .MAX_VALUE , toCopy .zeroBucket ().count ())
100
+ )
101
+ );
102
+ case POSITIVE_BUCKETS -> modifyBuckets (copyBuilder , toCopy .positiveBuckets (), true );
103
+ case NEGATIVE_BUCKETS -> modifyBuckets (copyBuilder , toCopy .negativeBuckets (), false );
105
104
}
106
- if (modification == Modification .MAX ) {
107
- copy .setMax (randomDouble ());
108
- } else {
109
- copy .setMax (toCopy .max ());
110
- }
111
- long zeroCount = toCopy .zeroBucket ().count ();
112
- double zeroThreshold = toCopy .zeroBucket ().zeroThreshold ();
113
- if (modification == Modification .ZERO_COUNT ) {
114
- zeroCount = createRandomLongBetweenOtherThan (0 , Long .MAX_VALUE , zeroCount );
115
- } else if (modification == Modification .ZERO_THRESHOLD ) {
116
- zeroThreshold = randomDouble ();
117
- }
118
- copy .setZeroBucket (ZeroBucket .create (zeroThreshold , zeroCount ));
119
- copyBuckets (copy , toCopy .negativeBuckets (), modification == Modification .NEGATIVE_BUCKETS , false );
120
- copyBuckets (copy , toCopy .positiveBuckets (), modification == Modification .POSITIVE_BUCKETS , true );
121
105
122
- return copy ;
106
+ ReleasableExponentialHistogram result = copyBuilder .build ();
107
+ autoReleaseOnTestEnd (result );
108
+ return result ;
123
109
}
124
110
125
- private void copyBuckets (
126
- FixedCapacityExponentialHistogram into ,
111
+ private ExponentialHistogramBuilder modifyBuckets (
112
+ ExponentialHistogramBuilder builder ,
127
113
ExponentialHistogram .Buckets buckets ,
128
- boolean modify ,
129
114
boolean isPositive
130
115
) {
131
116
List <Long > indices = new ArrayList <>();
@@ -136,29 +121,28 @@ private void copyBuckets(
136
121
counts .add (it .peekCount ());
137
122
it .advance ();
138
123
}
139
- if (modify ) {
140
- if (counts .isEmpty () == false && randomBoolean ()) {
141
- int toModify = randomIntBetween (0 , indices .size () - 1 );
142
- counts .set (toModify , createRandomLongBetweenOtherThan (1 , Long .MAX_VALUE , counts .get (toModify )));
143
- } else {
144
- insertRandomBucket (indices , counts );
145
- }
124
+ long toModifyIndex ;
125
+ long toModifyCount ;
126
+ if (counts .isEmpty () == false && randomBoolean ()) {
127
+ // Modify existing bucket
128
+ int position = randomIntBetween (0 , indices .size () - 1 );
129
+ toModifyIndex = indices .get (position );
130
+ toModifyCount = createRandomLongBetweenOtherThan (1 , Long .MAX_VALUE , counts .get (position ));
131
+ } else {
132
+ // Add new bucket
133
+ long minIndex = indices .stream ().mapToLong (Long ::longValue ).min ().orElse (MIN_INDEX );
134
+ long maxIndex = indices .stream ().mapToLong (Long ::longValue ).min ().orElse (MAX_INDEX );
135
+ do {
136
+ toModifyIndex = randomLongBetween (Math .max (MIN_INDEX , minIndex - 10 ), Math .min (MAX_INDEX , maxIndex + 10 ));
137
+ } while (indices .contains (toModifyIndex ));
138
+ toModifyCount = randomLongBetween (1 , Long .MAX_VALUE );
146
139
}
147
- for (int i = 0 ; i < indices .size (); i ++) {
148
- into .tryAddBucket (indices .get (i ), counts .get (i ), isPositive );
140
+ if (isPositive ) {
141
+ builder .setPositiveBucket (toModifyIndex , toModifyCount );
142
+ } else {
143
+ builder .setNegativeBucket (toModifyIndex , toModifyCount );
149
144
}
150
- }
151
-
152
- private void insertRandomBucket (List <Long > indices , List <Long > counts ) {
153
- long minIndex = indices .stream ().mapToLong (Long ::longValue ).min ().orElse (MIN_INDEX );
154
- long maxIndex = indices .stream ().mapToLong (Long ::longValue ).min ().orElse (MAX_INDEX );
155
- long newIndex ;
156
- do {
157
- newIndex = randomLongBetween (Math .max (MIN_INDEX , minIndex - 10 ), Math .min (MAX_INDEX , maxIndex + 10 ));
158
- } while (indices .contains (newIndex ));
159
- int position = -(Collections .binarySearch (indices , newIndex ) + 1 );
160
- indices .add (position , newIndex );
161
- counts .add (position , randomLongBetween (1 , Long .MAX_VALUE ));
145
+ return builder ;
162
146
}
163
147
164
148
private static long createRandomLongBetweenOtherThan (long min , long max , long notAllowedValue ) {
@@ -169,16 +153,4 @@ private static long createRandomLongBetweenOtherThan(long min, long max, long no
169
153
} while (result == notAllowedValue );
170
154
return result ;
171
155
}
172
-
173
- private static int getBucketCount (ExponentialHistogram .Buckets ... buckets ) {
174
- int count = 0 ;
175
- for (ExponentialHistogram .Buckets val : buckets ) {
176
- BucketIterator it = val .iterator ();
177
- while (it .hasNext ()) {
178
- count ++;
179
- it .advance ();
180
- }
181
- }
182
- return count ;
183
- }
184
156
}
0 commit comments