@@ -176,6 +176,45 @@ public void testEWMA() {
176176 * Window lookback for this test is 25. Window duration is 1 second. Window divisor is default
177177 * of 5.
178178 */
179+ // @Test
180+ // public void testDebt() {
181+ // final long samplesPerWindow = 10;
182+ // final long actualSamplesPerWindow = 50;
183+ // JfrThrottler throttler = new JfrThrottler(mutex);
184+ // throttler.beginTest(samplesPerWindow * WINDOWS_PER_PERIOD, WINDOWS_PER_PERIOD *
185+ // WINDOW_DURATION_MS);
186+ //
187+ // for (int p = 0; p < 50; p++) {
188+ // for (int i = 0; i < actualSamplesPerWindow; i++) {
189+ // throttler.sample();
190+ // }
191+ // expireAndRotate(throttler);
192+ // }
193+ // // now the sampling interval must be 50 / 10 = 5
194+ // assertTrue("Sampling interval is incorrect:" + throttler.getActiveWindowSamplingInterval(),
195+ // throttler.getActiveWindowSamplingInterval() == 5);
196+ //
197+ // // create debt by under sampling. Instead of 50, only sample 20 times. Debt should be 10 -
198+ // // (20/5) = 6
199+ // // samples
200+ // for (int i = 0; i < 20; i++) {
201+ // throttler.sample();
202+ // }
203+ // expireAndRotate(throttler);
204+ // assertTrue("Should have debt from under sampling.", throttler.getActiveWindowDebt() == 6);
205+ // // sampling interval should be 3 now. Take no samples and rotate. Results in accumulated
206+ // // debt 6 + 10
207+ // expireAndRotate(throttler);
208+ // assertTrue("Should have accumulated debt from under sampling consecutively.",
209+ // throttler.getActiveWindowDebt() == 16);
210+ // expireAndRotate(throttler);
211+ // expireAndRotate(throttler);
212+ // assertTrue("Debt is so high we should not skip any samples now.",
213+ // throttler.getActiveWindowSamplingInterval() == 1);
214+ // expireAndRotate(throttler);
215+ // assertTrue("Debt should be forgiven at beginning of new period.", throttler.getActiveWindowDebt()
216+ // == 0);
217+ // }
179218 @ Test
180219 public void testDebt () {
181220 final long samplesPerWindow = 10 ;
@@ -189,26 +228,36 @@ public void testDebt() {
189228 }
190229 expireAndRotate (throttler );
191230 }
192- // now the sampling interval must be 50 / 10 = 5
193- assertTrue ("Sampling interval is incorrect:" + throttler .getActiveWindowSamplingInterval (), throttler .getActiveWindowSamplingInterval () == 5 );
194231
195- // create debt by under sampling. Instead of 50, only sample 20 times. Debt should be 10 -
196- // (20/5) = 6
197- // samples
198- for (int i = 0 ; i < 20 ; i ++) {
232+ // Do not sample for this window. Rotate.
233+ expireAndRotate (throttler );
234+
235+ // Debt should be at least 10 because we took no samples last window.
236+ long debt = throttler .getActiveWindowDebt ();
237+ assertTrue ("Should have debt from under sampling." , debt >= 10 );
238+
239+ // Limit max potential samples to half samplesPerWindow. Meaning debt must increase by at
240+ // least samplesPerWindow/2.
241+ for (int i = 0 ; i < samplesPerWindow / 2 ; i ++) {
199242 throttler .sample ();
200243 }
201244 expireAndRotate (throttler );
202- assertTrue ("Should have debt from under sampling." , throttler .getActiveWindowDebt () == 6 );
203- // sampling interval should be 3 now. Take no samples and rotate. Results in accumulated
204- // debt 6 + 10
205- expireAndRotate (throttler );
206- assertTrue ("Should have accumulated debt from under sampling consecutively." , throttler .getActiveWindowDebt () == 16 );
207- expireAndRotate (throttler );
208- expireAndRotate (throttler );
209- assertTrue ("Debt is so high we should not skip any samples now." , throttler .getActiveWindowSamplingInterval () == 1 );
245+ assertTrue ("Should have debt from under sampling." , throttler .getActiveWindowDebt () >= debt + samplesPerWindow / 2 );
246+
247+ // Window lookback is 25. Do not sample for 25 windows.
248+ for (int i = 0 ; i < 25 ; i ++) {
249+ expireAndRotate (throttler );
250+ }
251+
252+ // At this point sampling interval must be 1 because the projected population must be 0.
253+ for (int i = 0 ; i < (samplesPerWindow + samplesPerWindow * WINDOWS_PER_PERIOD ); i ++) {
254+ throttler .sample ();
255+ }
256+
257+ assertFalse (throttler .sample ());
258+
210259 expireAndRotate (throttler );
211- assertTrue ("Debt should be forgiven at beginning of new period." , throttler .getActiveWindowDebt () == 0 );
260+ assertTrue (throttler .getActiveWindowDebt () == 0 );
212261 }
213262
214263 /**
@@ -241,13 +290,9 @@ public void testZeroRate() throws Throwable {
241290 assertTrue (throttler .sample ());
242291
243292 // Test applying throttling settings to an in-progress recording
244- Recording recording = startRecording (new String []{}, null , new HashMap <>()); // don't use
245- // default
246- // configuration
247- // because it
248- // includes
249- // ObjectAllocationSample
250- // by default
293+ // Avoid using default configuration becuase it enables ObjectAllocationSample events by
294+ // default
295+ Recording recording = startRecording (new String []{}, null , new HashMap <>());
251296 recording .enable (JfrEvent .ObjectAllocationSample .getName ()).with ("throttle" , "0/s" );
252297 final int alignedHeapChunkSize = com .oracle .svm .core .util .UnsignedUtils .safeToInt (HeapParameters .getAlignedHeapChunkSize ());
253298 allocateCharArray (alignedHeapChunkSize );
@@ -323,8 +368,7 @@ private void testDistribution(IncomingPopulation incomingPopulation, int sampleP
323368 final int windowCount = 10000 ;
324369 final int expectedSamplesPerWindow = 50 ;
325370 final int expectedSamples = expectedSamplesPerWindow * windowCount ;
326- final int windowLookBackCount = 50 ;
327- final double maxSampleBias = 0.11 ;
371+
328372 JfrThrottler throttler = new JfrThrottler (mutex );
329373 throttler .beginTest (expectedSamplesPerWindow * WINDOWS_PER_PERIOD , windowDurationMs * WINDOWS_PER_PERIOD );
330374
@@ -347,13 +391,11 @@ private void testDistribution(IncomingPopulation incomingPopulation, int sampleP
347391 expireAndRotate (throttler );
348392 }
349393 int targetSampleSize = samplePointsPerWindow * windowCount ;
350- System .out .println ("Population size:" + populationSize + " Sample size: " + sampleSize );
351394 expectNear (targetSampleSize , sampleSize , expectedSamples * errorFactor );
352395 assertDistributionProperties (distributionSlots , population , sample , populationSize , sampleSize );
353396 }
354397
355398 private static void expectNear (double value1 , double value2 , double error ) {
356- // System.out.println(value1 +" "+ value2);
357399 assertTrue (Math .abs (value1 - value2 ) <= error );
358400 }
359401
@@ -381,7 +423,6 @@ private static void assertDistributionProperties(int distributionSlots, int[] po
381423 sampleVariance = sampleVariance / (sampleSize - 1 );
382424 double populationStdev = Math .sqrt (populationVariance );
383425 double sampleStdev = Math .sqrt (sampleVariance );
384- // System.out.println("populationStdev:"+populationStdev +" sampleStdev:"+sampleStdev );
385426 expectNear (populationStdev , sampleStdev , 0.5 ); // 0.5 value copied from Hotspot test
386427 expectNear (populationMean , sampleMean , populationStdev );
387428 }
0 commit comments