package com.intellij.util;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class StringBuilderSpinAllocatorTest {

  private final String[] myStrings = new String[]{
    "First String is the smallest",
    "Second String is definitely larger than the first one",
    "Third String is a bit larger than the first one",
    "Fourth String is the largest amongst all the myStrings. Congrats! It must be even larger than it is."
  };

  @Before
  public void setUp() throws Exception {
    // warm-up
    new StringBuilder();
    StringBuilderSpinAllocator.dispose(StringBuilderSpinAllocator.alloc());
  }

  @Test
  public void testPerformance() throws InterruptedException {
    StringBuilder builder;
    final int count = 1000000;

    long start = System.nanoTime();
    for (int i = 0; i < count; ++i) {
      builder = new StringBuilder();
      builder.append(myStrings[i & 3]);
      builder.append(builder.toString());
    }
    final long regularTime = (System.nanoTime() - start)/1000;
    System.out.println("StringBuilder regular allocations took: " + regularTime);

    System.gc();
    System.runFinalization();
    Thread.sleep(2000);

    start = System.nanoTime();
    for (int i = 0; i < count; ++i) {
      builder = StringBuilderSpinAllocator.alloc();
      builder.append(myStrings[i & 3]);
      builder.append(builder.toString());
      StringBuilderSpinAllocator.dispose(builder);
    }
    final long spinTime = (System.nanoTime() - start)/1000;
    System.out.println("StringBuilder spin allocations took: " + spinTime);

    if (!com.intellij.testFramework.PlatformTestUtil.COVERAGE_ENABLED_BUILD) {
      assertTrue("regular:" + regularTime + "mks, spin:" + spinTime + "mks", spinTime < regularTime);
    }
  }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2000-2013 JetBrains s.r.o.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -626,33 +626,8 @@
 l;%0A%0A
-import org.junit.Before;%0A
 impo
@@ -688,16 +688,59 @@
 ertTrue;
+%0Aimport static org.junit.Assume.assumeTrue;
 %0A%0Apublic
@@ -779,17 +779,16 @@
 rTest %7B%0A
-%0A
   privat
@@ -1102,14 +1102,12 @@
 %0A  @
-Before
+Test
 %0A  p
@@ -1121,223 +1121,192 @@
 oid 
-setUp() throws Exception %7B%0A    // warm-up%0A    new StringBuilder();%0A    StringBuilderSpinAllocator.dispose(StringBuilderSpinAlloc
+testPerformance() %7B%0A    assumeTrue(!com.intellij.testFramework.Pl
 at
+f
 or
-.alloc());%0A  %7D%0A%0A  @Test%0A  public void testPerformance() throws InterruptedException
+mTestUtil.COVERAGE_ENABLED_BUILD);%0A    doTest(true);%0A    doTest(false);%0A  %7D%0A%0A  private void doTest(boolean warmUp)
  %7B%0A 
@@ -1335,22 +1335,16 @@
 er;%0A    
-final 
 int coun
@@ -1351,15 +1351,108 @@
 t = 
-1000000
+warmUp ? 1000 : 1000000;%0A%0A    System.gc();%0A    System.runFinalization();%0A    TimeoutUtil.sleep(1000)
 ;%0A%0A 
@@ -1649,30 +1649,24 @@
 ;%0A    %7D%0A    
-final 
 long regular
@@ -1703,96 +1703,16 @@
 art)
-/
+ / 
 1000;
-%0A    System.out.println(%22StringBuilder regular allocations took: %22 + regularTime);
 %0A%0A  
@@ -1765,21 +1765,26 @@
    T
-hread
+imeoutUtil
 .sleep(
-2
+1
 000)
@@ -2050,22 +2050,16 @@
   %7D%0A    
-final 
 long spi
@@ -2097,15 +2097,39 @@
 art)
-/
+ / 
 1000;%0A
+%0A    if (!warmUp) %7B%0A  
     
@@ -2162,20 +2162,23 @@
 Builder 
-spin
+regular
  allocat
@@ -2196,98 +2196,99 @@
 %22 + 
-spin
+regular
 Time);%0A
-%0A
     
-if (!com.intellij.testFramework.PlatformTestUtil.COVERAGE_ENABLED_BUILD) %7B
+  System.out.println(%22StringBuilder spin allocations took: %22 + spinTime);
 %0A   

Test case assessment and minor rewrite