Skip to content

Commit

Permalink
Less flaky ProfilerTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Nov 29, 2018
1 parent 48a3830 commit ab72c91
Showing 1 changed file with 11 additions and 9 deletions.
Expand Up @@ -31,11 +31,13 @@


class ProfilerTest class ProfilerTest
{ {
private static final int COMPUTE_WORK_MILLIS = 1000;

@Test @Test
void profilerMustNoticeWhereTimeGoes() throws Exception void profilerMustNoticeWhereTimeGoes() throws Exception
{ {
Profiler profiler = Profiler.profiler(); Profiler profiler = Profiler.profiler();
try ( Profiler.ProfiledInterval interval = profiler.profile() ) try ( Profiler.ProfiledInterval ignored = profiler.profile() )
{ {
expensiveComputation(); expensiveComputation();
} }
Expand All @@ -47,7 +49,7 @@ void profilerMustNoticeWhereTimeGoes() throws Exception
void profilerMustLimitItselfToProfiledRegion() throws Exception void profilerMustLimitItselfToProfiledRegion() throws Exception
{ {
Profiler profiler = Profiler.profiler(); Profiler profiler = Profiler.profiler();
try ( Profiler.ProfiledInterval interval = profiler.profile() ) try ( Profiler.ProfiledInterval ignored = profiler.profile() )
{ {
expensiveComputation(); expensiveComputation();
} }
Expand All @@ -60,8 +62,8 @@ void profilerMustLimitItselfToProfiledRegion() throws Exception
void profilerMustWaitUntilAfterAnInitialDelay() throws Exception void profilerMustWaitUntilAfterAnInitialDelay() throws Exception
{ {
Profiler profiler = Profiler.profiler(); Profiler profiler = Profiler.profiler();
long initialDelayNanos = TimeUnit.MILLISECONDS.toNanos( 250 ); long initialDelayNanos = TimeUnit.MILLISECONDS.toNanos( COMPUTE_WORK_MILLIS * 3 );
try ( Profiler.ProfiledInterval interval = profiler.profile( Thread.currentThread(), initialDelayNanos ) ) try ( Profiler.ProfiledInterval ignored = profiler.profile( Thread.currentThread(), initialDelayNanos ) )
{ {
expensiveComputation(); expensiveComputation();
} }
Expand All @@ -73,22 +75,22 @@ void profilerMustWaitUntilAfterAnInitialDelay() throws Exception
private String getProfilerOutput( Profiler profiler ) throws InterruptedException private String getProfilerOutput( Profiler profiler ) throws InterruptedException
{ {
profiler.finish(); profiler.finish();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try ( PrintStream out = new PrintStream( baos ) ) try ( PrintStream out = new PrintStream( buffer ) )
{ {
profiler.printProfile( out, "Profile" ); profiler.printProfile( out, "Profile" );
out.flush(); out.flush();
} }
return baos.toString(); return buffer.toString();
} }


private void expensiveComputation() throws InterruptedException private void expensiveComputation() throws InterruptedException
{ {
Thread.sleep( 50 ); Thread.sleep( COMPUTE_WORK_MILLIS );
} }


private void otherIntenseWork() throws InterruptedException private void otherIntenseWork() throws InterruptedException
{ {
Thread.sleep( 50 ); Thread.sleep( COMPUTE_WORK_MILLIS );
} }
} }

0 comments on commit ab72c91

Please sign in to comment.