title: Why is performance of executing Mockito mocks so erratic?
body: <p>Would anyone have an explanation, or even better a suggested fix, for why the time taken to execute Mockito mocks is so erratic?  The simplest SSCCE I could come up with for this is below:</p>
  
  <pre><code>import static org.mockito.Mockito.mock;
  
  public class TestSimpleMockTiming
  {
      public static final void main (final String args [])
      {
          final Runnable theMock = mock (Runnable.class);
  
          int tookShort = 0;
          int tookMedium = 0;
          int tookLong = 0;
          int tookRidiculouslyLong = 0;
          long longest = 0;
  
          for (int n = 0; n &lt; 2000000; n++)
          {
              final long startTime = System.nanoTime ();
              theMock.run ();
              final long duration = System.nanoTime () - startTime;
  
              if (duration &lt; 1000000)                 // 0.001 seconds
                  tookShort++;
              else if (duration &lt; 100000000)      // 0.1 seconds
                  tookMedium++;
              else if (duration &lt; 1000000000)     // 1 second !!!
                  tookLong++;
              else
                  tookRidiculouslyLong++;
  
              longest = Math.max (longest, duration);
          }
  
          System.out.println (tookShort + ", " + tookMedium + ", " + tookLong + ", " + tookRidiculouslyLong);
          System.out.println ("Longest duration was " + longest + " ns");
      }
  }
  </code></pre>
  
  <p>If I run this (from within Eclipse, using JDK 1.7.45 on Win 7 x64) typical output looks like:</p>
  
  <pre><code>1999983, 4, 9, 4
  Longest duration was 5227445252 ns
  </code></pre>
  
  <p>So, while in the majority of situations the mock executes very fast, there's several executions that take even longer than 1 second.  That's an eternity for a method that does nothing.  From my experimenting with this, I don't believe the issue is the accuracy of System.nanoTime (), I think the mock really does take that long to execute.  Is there anything I can do to improve on this and make the timing behave more consistently?</p>
  
  <p>(FYI, why this is an issue is that I have a Swing app which contains various frames, and I try to write JUnit tests for the frames so that I can test that the layoutManagers behave correctly without having to fire up the whole app and navigate to the correct screen.  In one such test, the screen uses a javax.swing.Timer to implement scrolling, so the display will pan around an area when the mouse is held near the end of the frame.  I noticed the behaviour of this was very erratic, and the scrolling while usually fine would periodically freeze for up to a second and it looked dreadful.  I wrote an SSCCE around this, thinking the problem was that Swing Timers can't be depended on to fire at a consistent rate, and in the SSCCE it worked perfectly.</p>
  
  <p>After hours of tearing my hair out then trying to spot differences between my real code and the scrolling demo SSCCE, I started putting nano timers around blocks of code that ran repeatedly, noticed the time taken by my paintComponent method to be very erratic and eventually narrowed it down to a mock call.  Testing the screen from running the real app, the scrolling behaves smoothly, its only a problem from the JUnit test because of the mock call, which led to me testing a simple mock in isolation with the SSCCE posted above.)</p>
  
  <p>Many thanks!</p>