2525
2626import java .util .ArrayList ;
2727import java .util .Arrays ;
28- import java .util .Collections ;
2928import java .util .List ;
3029
3130import static org .elasticsearch .exponentialhistogram .ExponentialHistogram .MAX_INDEX ;
@@ -61,7 +60,8 @@ public ExponentialHistogramEqualityTests(Modification modification) {
6160
6261 public void testEquality () {
6362 ExponentialHistogram histo = randomHistogram ();
64- ExponentialHistogram copy = copyWithModification (histo , null );
63+ ReleasableExponentialHistogram copy = ExponentialHistogram .builder (histo , breaker ()).build ();
64+ autoReleaseOnTestEnd (copy );
6565 ExponentialHistogram modifiedCopy = copyWithModification (histo , modification );
6666
6767 assertThat (histo , equalTo (copy ));
@@ -86,46 +86,31 @@ private ExponentialHistogram randomHistogram() {
8686 }
8787
8888 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 );
105104 }
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 );
121105
122- return copy ;
106+ ReleasableExponentialHistogram result = copyBuilder .build ();
107+ autoReleaseOnTestEnd (result );
108+ return result ;
123109 }
124110
125- private void copyBuckets (
126- FixedCapacityExponentialHistogram into ,
111+ private ExponentialHistogramBuilder modifyBuckets (
112+ ExponentialHistogramBuilder builder ,
127113 ExponentialHistogram .Buckets buckets ,
128- boolean modify ,
129114 boolean isPositive
130115 ) {
131116 List <Long > indices = new ArrayList <>();
@@ -136,29 +121,28 @@ private void copyBuckets(
136121 counts .add (it .peekCount ());
137122 it .advance ();
138123 }
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 );
146139 }
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 );
149144 }
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 ;
162146 }
163147
164148 private static long createRandomLongBetweenOtherThan (long min , long max , long notAllowedValue ) {
@@ -169,16 +153,4 @@ private static long createRandomLongBetweenOtherThan(long min, long max, long no
169153 } while (result == notAllowedValue );
170154 return result ;
171155 }
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- }
184156}
0 commit comments