From 1c1987195f96d6dd151448f6a1affd9f085b9a76 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Wed, 8 Feb 2012 11:51:44 +0100 Subject: [PATCH] Making UT more reliable. The original UT was too unreliable, mostly because it was "mixing" millis and nanos. Now, the test is simplified, and on my mac returns (if you uncomment the System.out lines) nice values (around 15% of limit TPS) --- ...FixedRateWalkerThrottleControllerTest.java | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/nexus/nexus-api/src/test/java/org/sonatype/nexus/proxy/walker/FixedRateWalkerThrottleControllerTest.java b/nexus/nexus-api/src/test/java/org/sonatype/nexus/proxy/walker/FixedRateWalkerThrottleControllerTest.java index 7ddc8497f7..6baaded038 100644 --- a/nexus/nexus-api/src/test/java/org/sonatype/nexus/proxy/walker/FixedRateWalkerThrottleControllerTest.java +++ b/nexus/nexus-api/src/test/java/org/sonatype/nexus/proxy/walker/FixedRateWalkerThrottleControllerTest.java @@ -34,11 +34,11 @@ public class FixedRateWalkerThrottleControllerTest @Test public void testDoesItHelpAtAll() { - // set unrealistic TPS and we do "almost nothing" (1 nano) in processItem method + // set unrealistic TPS and we do "little" (1ms) in processItem method (to get the ceiling to compare with) final int measuredTpsUnreal = performAndMeasureActualTps( 100000000, new ConstantNumberSequence( 1 ) ); - // set 500 TPS and we do "little" (1 nano) in processItem method + // set 500 TPS and we do "little" (1ms) in processItem method final int measuredTps500 = performAndMeasureActualTps( 500, new ConstantNumberSequence( 1 ) ); - // set 200 TPS and we do "little" (1 nano) in processItem method + // set 200 TPS and we do "little" (1ms) in processItem method final int measuredTps200 = performAndMeasureActualTps( 200, new ConstantNumberSequence( 1 ) ); assertThat( "TPS500 should less than Unreal one", measuredTps500, lessThan( measuredTpsUnreal ) ); @@ -54,11 +54,8 @@ protected int performAndMeasureActualTps( final int wantedTps, final NumberSeque fixedRateWalkerThrottleController.setSliceSize( 1 ); final TestThrottleInfo info = new TestThrottleInfo(); - final WalkerContext context = Mockito.mock( WalkerContext.class ); - - final int iterationCount = 10000; - + final int iterationCount = 1000; final long startTime = System.currentTimeMillis(); fixedRateWalkerThrottleController.walkStarted( context ); for ( int i = 0; i < iterationCount; i++ ) @@ -72,9 +69,10 @@ protected int performAndMeasureActualTps( final int wantedTps, final NumberSeque final int measuredTps = fixedRateWalkerThrottleController.calculateCPS( iterationCount, System.currentTimeMillis() - startTime ); - System.out.println( "Measured=" + measuredTps ); - System.out.println( "GlobalAvg=" + fixedRateWalkerThrottleController.getGlobalAverageTps() ); - System.out.println( "GlobalMax=" + fixedRateWalkerThrottleController.getGlobalMaximumTps() ); + // System.out.println( "MeasuredTps=" + measuredTps ); + // System.out.println( "lastSliceTps=" + fixedRateWalkerThrottleController.getLastSliceTps() ); + // System.out.println( "GlobalAvgTps=" + fixedRateWalkerThrottleController.getGlobalAverageTps() ); + // System.out.println( "GlobalMaxTps=" + fixedRateWalkerThrottleController.getGlobalMaximumTps() ); return measuredTps; } @@ -94,19 +92,6 @@ protected static void sleep( long millis ) } } - protected static void sleepNanos( long nanos ) - { - try - { - Thread.sleep( 0, (int) nanos ); - } - catch ( InterruptedException e ) - { - // need to kill test too - throw new RuntimeException( e ); - } - } - protected static class TestThrottleInfo implements ThrottleInfo { @@ -126,8 +111,8 @@ public TestThrottleInfo() public void simulateInvocation( final long spentTimeInProcessItem ) { // we need to sleep to keep getTotalTimeWalking() and totalProcessItemSpentMillis aligned - sleepNanos( spentTimeInProcessItem ); - totalProcessItemSpentMillis = totalProcessItemSpentMillis + ( spentTimeInProcessItem * 1000 ); + sleep( spentTimeInProcessItem ); + totalProcessItemSpentMillis = totalProcessItemSpentMillis + spentTimeInProcessItem; totalProcessItemInvocationCount++; }