@@ -162,9 +162,16 @@ public void testSampleDataOverTime1() {
162162 MockedTime time = new MockedTime ();
163163 HystrixRollingPercentile p = new HystrixRollingPercentile (time , timeInMilliseconds , numberOfBuckets , enabled );
164164 int previousTime = 0 ;
165+
166+ int maxSoFar = -1 ;
167+
165168 for (int i = 0 ; i < SampleDataHolder1 .data .length ; i ++) {
166169 int timeInMillisecondsSinceStart = SampleDataHolder1 .data [i ][0 ];
167170 int latency = SampleDataHolder1 .data [i ][1 ];
171+ if (latency > maxSoFar ) {
172+ System .out .println ("New MAX latency : " + latency );
173+ maxSoFar = latency ;
174+ }
168175 time .increment (timeInMillisecondsSinceStart - previousTime );
169176 previousTime = timeInMillisecondsSinceStart ;
170177 p .addValue (latency );
@@ -181,6 +188,8 @@ public void testSampleDataOverTime1() {
181188 System .out .println ("Median: " + p .getPercentile (50 ));
182189 System .out .println ("Median: " + p .getPercentile (50 ));
183190
191+ System .out .println ("MAX : " + p .currentPercentileSnapshot .aggregateHistogram .getMaxValue ());
192+
184193 /*
185194 * In a loop as a use case was found where very different values were calculated in subsequent requests.
186195 */
@@ -216,6 +225,8 @@ public void testSampleDataOverTime2() {
216225 System .out .println ("99.5th: " + p .getPercentile (99.5 ));
217226 System .out .println ("99.99: " + p .getPercentile (99.99 ));
218227
228+ System .out .println ("MAX : " + p .currentPercentileSnapshot .aggregateHistogram .getMaxValue ());
229+
219230 if (p .getPercentile (50 ) > 90 || p .getPercentile (50 ) < 50 ) {
220231 fail ("We expect around 60-70 but got: " + p .getPercentile (50 ));
221232 }
@@ -394,6 +405,42 @@ public void run() {
394405 System .out .println (p .getMean () + " : " + p .getPercentile (50 ) + " : " + p .getPercentile (75 ) + " : " + p .getPercentile (90 ) + " : " + p .getPercentile (95 ) + " : " + p .getPercentile (99 ));
395406 }
396407
408+ @ Test
409+ public void testWriteThreadSafety () {
410+ final MockedTime time = new MockedTime ();
411+ final HystrixRollingPercentile p = new HystrixRollingPercentile (time , HystrixProperty .Factory .asProperty (100 ), HystrixProperty .Factory .asProperty (25 ), HystrixProperty .Factory .asProperty (true ));
412+
413+ final int NUM_THREADS = 1000 ;
414+ final int NUM_ITERATIONS = 1000000 ;
415+
416+ final CountDownLatch latch = new CountDownLatch (NUM_THREADS );
417+
418+ final Random r = new Random ();
419+
420+ final AtomicInteger added = new AtomicInteger (0 );
421+
422+ for (int i = 0 ; i < NUM_THREADS ; i ++) {
423+ threadPool .submit (new Runnable () {
424+ @ Override
425+ public void run () {
426+ for (int j = 1 ; j < NUM_ITERATIONS / NUM_THREADS + 1 ; j ++) {
427+ int nextInt = r .nextInt (100 );
428+ p .addValue (nextInt );
429+ added .getAndIncrement ();
430+ }
431+ latch .countDown ();
432+ }
433+ });
434+ }
435+
436+ try {
437+ latch .await (100 , TimeUnit .SECONDS );
438+ assertEquals (added .get (), p .buckets .peekLast ().bucketData .recorder .getIntervalHistogram ().getTotalCount ());
439+ } catch (InterruptedException ex ) {
440+ fail ("Timeout on all threads writing percentiles" );
441+ }
442+ }
443+
397444 @ Test
398445 public void testThreadSafetyMulti () {
399446 for (int i = 0 ; i < 100 ; i ++) {
0 commit comments