Test case assessment and minor rewrite

This commit is contained in:
Roman Shevchenko
2013-02-08 17:41:22 +01:00
parent 85bbc002c2
commit a5c3f9c19c
@@ -1,12 +1,26 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
public class StringBuilderSpinAllocatorTest {
private final String[] myStrings = new String[]{
"First String is the smallest",
"Second String is definitely larger than the first one",
@@ -14,17 +28,20 @@ public class StringBuilderSpinAllocatorTest {
"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() {
assumeTrue(!com.intellij.testFramework.PlatformTestUtil.COVERAGE_ENABLED_BUILD);
doTest(true);
doTest(false);
}
@Test
public void testPerformance() throws InterruptedException {
private void doTest(boolean warmUp) {
StringBuilder builder;
final int count = 1000000;
int count = warmUp ? 1000 : 1000000;
System.gc();
System.runFinalization();
TimeoutUtil.sleep(1000);
long start = System.nanoTime();
for (int i = 0; i < count; ++i) {
@@ -32,12 +49,11 @@ public class StringBuilderSpinAllocatorTest {
builder.append(myStrings[i & 3]);
builder.append(builder.toString());
}
final long regularTime = (System.nanoTime() - start)/1000;
System.out.println("StringBuilder regular allocations took: " + regularTime);
long regularTime = (System.nanoTime() - start) / 1000;
System.gc();
System.runFinalization();
Thread.sleep(2000);
TimeoutUtil.sleep(1000);
start = System.nanoTime();
for (int i = 0; i < count; ++i) {
@@ -46,10 +62,11 @@ public class StringBuilderSpinAllocatorTest {
builder.append(builder.toString());
StringBuilderSpinAllocator.dispose(builder);
}
final long spinTime = (System.nanoTime() - start)/1000;
System.out.println("StringBuilder spin allocations took: " + spinTime);
long spinTime = (System.nanoTime() - start) / 1000;
if (!com.intellij.testFramework.PlatformTestUtil.COVERAGE_ENABLED_BUILD) {
if (!warmUp) {
System.out.println("StringBuilder regular allocations took: " + regularTime);
System.out.println("StringBuilder spin allocations took: " + spinTime);
assertTrue("regular:" + regularTime + "mks, spin:" + spinTime + "mks", spinTime < regularTime);
}
}