aboutsummaryrefslogtreecommitdiff
path: root/test/com/google/caliper
diff options
context:
space:
mode:
Diffstat (limited to 'test/com/google/caliper')
-rw-r--r--test/com/google/caliper/AllTests.java2
-rw-r--r--test/com/google/caliper/DefaultBenchmarkSuiteTest.java91
-rw-r--r--test/com/google/caliper/examples/ArraySortBenchmark.java (renamed from test/com/google/caliper/examples/SortBenchmarkSuite.java)29
-rw-r--r--test/com/google/caliper/examples/BoxedDoubleToStringBenchmark.java80
-rw-r--r--test/com/google/caliper/examples/CharacterBenchmark.java173
-rw-r--r--test/com/google/caliper/examples/DoubleToStringBenchmarkSuite.java77
-rw-r--r--test/com/google/caliper/examples/EnumSetContainsBenchmark.java (renamed from test/com/google/caliper/examples/EnumSetContainsBenchmarkSuite.java)25
-rw-r--r--test/com/google/caliper/examples/ExpensiveObjectsBenchmark.java71
-rw-r--r--test/com/google/caliper/examples/FormatterBenchmark.java77
-rw-r--r--test/com/google/caliper/examples/FormatterBenchmarkSuite.java98
-rw-r--r--test/com/google/caliper/examples/IntModBenchmark.java90
-rw-r--r--test/com/google/caliper/examples/ListIterationBenchmark.java (renamed from test/com/google/caliper/examples/ListIterationBenchmarkSuite.java)48
-rw-r--r--test/com/google/caliper/examples/PrimitiveDoubleToStringBenchmark.java80
-rw-r--r--test/com/google/caliper/examples/StringBuilderBenchmark.java132
14 files changed, 750 insertions, 323 deletions
diff --git a/test/com/google/caliper/AllTests.java b/test/com/google/caliper/AllTests.java
index 55f620a..510cc0a 100644
--- a/test/com/google/caliper/AllTests.java
+++ b/test/com/google/caliper/AllTests.java
@@ -22,7 +22,7 @@ import junit.framework.TestSuite;
public final class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite();
- suite.addTestSuite(DefaultBenchmarkSuiteTest.class);
+ // tests go here :)
return suite;
}
}
diff --git a/test/com/google/caliper/DefaultBenchmarkSuiteTest.java b/test/com/google/caliper/DefaultBenchmarkSuiteTest.java
deleted file mode 100644
index 232cb4c..0000000
--- a/test/com/google/caliper/DefaultBenchmarkSuiteTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (C) 2009 Google Inc.
- *
- * 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.google.caliper;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-
-import junit.framework.TestCase;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-public class DefaultBenchmarkSuiteTest extends TestCase {
-
- public void testIntrospection() {
- SampleBenchmarkSuite suite = new SampleBenchmarkSuite();
- assertEquals(ImmutableSet.of("a", "b"), suite.parameterNames());
- assertEquals(ImmutableSet.of("1", "2", "3"), suite.parameterValues("a"));
- assertEquals(ImmutableSet.of("4"), suite.parameterValues("b"));
- assertEquals(ImmutableSet.of(
- SampleBenchmarkSuite.MultiplyBenchmark.class,
- SampleBenchmarkSuite.DivideBenchmark.class),
- suite.benchmarkClasses());
-
- }
-
- public void testCreateBenchmark() {
- SampleBenchmarkSuite originalSuite = new SampleBenchmarkSuite();
- Benchmark benchmark = originalSuite.createBenchmark(
- SampleBenchmarkSuite.MultiplyBenchmark.class,
- ImmutableMap.of("a", "2", "b", "4"));
-
- SampleBenchmarkSuite.MultiplyBenchmark multiplyBenchmark
- = (SampleBenchmarkSuite.MultiplyBenchmark) benchmark;
-
- SampleBenchmarkSuite multiplySuite = multiplyBenchmark.suite();
- assertNotSame(originalSuite, multiplySuite);
- assertEquals(2, multiplySuite.a);
- assertEquals(4, multiplySuite.b);
- }
-
- static class SampleBenchmarkSuite extends DefaultBenchmarkSuite {
- @Param int a;
-
- private static Collection<Integer> aValues = Arrays.asList(1, 2, 3);
-
- @Param int b;
-
- private static Collection<Integer> bValues() {
- return Arrays.asList(4);
- }
-
- class MultiplyBenchmark extends Benchmark {
- @Override public Object run(int trials) throws Exception {
- int result = 0;
- for (int i = 0; i < trials; i++) {
- result ^= a * b;
- }
- return result;
- }
-
- SampleBenchmarkSuite suite() {
- return SampleBenchmarkSuite.this;
- }
- }
-
- class DivideBenchmark extends Benchmark {
- @Override public Object run(int trials) throws Exception {
- int result = 0;
- for (int i = 0; i < trials; i++) {
- result ^= a / b;
- }
- return result;
- }
- }
- }
-}
diff --git a/test/com/google/caliper/examples/SortBenchmarkSuite.java b/test/com/google/caliper/examples/ArraySortBenchmark.java
index e92fc10..2978fa2 100644
--- a/test/com/google/caliper/examples/SortBenchmarkSuite.java
+++ b/test/com/google/caliper/examples/ArraySortBenchmark.java
@@ -16,20 +16,19 @@
package com.google.caliper.examples;
-import com.google.caliper.Benchmark;
-import com.google.caliper.DefaultBenchmarkSuite;
import com.google.caliper.Param;
import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
import java.util.Arrays;
+import java.util.Collection;
import java.util.EnumSet;
import java.util.Random;
-import java.util.Collection;
/**
* Measures sorting on different distributions of integers.
*/
-public class SortBenchmarkSuite extends DefaultBenchmarkSuite {
+public class ArraySortBenchmark extends SimpleBenchmark {
@Param int length;
@@ -37,7 +36,7 @@ public class SortBenchmarkSuite extends DefaultBenchmarkSuite {
@Param Distribution distribution;
- static Collection<Distribution> distributionValues = EnumSet.allOf(Distribution.class);
+ static final Collection<Distribution> distributionValues = EnumSet.allOf(Distribution.class);
int[] values;
int[] copy;
@@ -47,16 +46,14 @@ public class SortBenchmarkSuite extends DefaultBenchmarkSuite {
copy = new int[length];
}
- class ArraysSortBenchmark extends Benchmark {
- public Object run(int trials) throws Exception {
- int result = 0;
- for (int i = 0; i < trials; i++) {
- System.arraycopy(values, 0, copy, 0, values.length);
- Arrays.sort(copy);
- result ^= copy[0];
- }
- return result;
+ public int timeSort(int reps) {
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ System.arraycopy(values, 0, copy, 0, values.length);
+ Arrays.sort(copy);
+ dummy ^= copy[0];
}
+ return dummy;
}
enum Distribution {
@@ -109,7 +106,7 @@ public class SortBenchmarkSuite extends DefaultBenchmarkSuite {
abstract int[] create(int length);
}
- public static void main(String[] args) {
- Runner.main(SortBenchmarkSuite.class, args);
+ public static void main(String[] args) throws Exception {
+ Runner.main(ArraySortBenchmark.class, args);
}
}
diff --git a/test/com/google/caliper/examples/BoxedDoubleToStringBenchmark.java b/test/com/google/caliper/examples/BoxedDoubleToStringBenchmark.java
new file mode 100644
index 0000000..5e6cbfa
--- /dev/null
+++ b/test/com/google/caliper/examples/BoxedDoubleToStringBenchmark.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.google.caliper.examples;
+
+import com.google.caliper.SimpleBenchmark;
+import com.google.caliper.Param;
+import com.google.caliper.Runner;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Measures the various ways the JDK converts boxed Doubles to Strings.
+ */
+public class BoxedDoubleToStringBenchmark extends SimpleBenchmark {
+
+ @Param private Double d;
+
+ private static final Collection<Double> dValues = Arrays.asList(
+ Math.PI,
+ -0.0d,
+ Double.NEGATIVE_INFINITY,
+ Double.NaN
+ );
+
+ public int timeStringFormat(int reps) {
+ Double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy += String.format("%f", value).length();
+ }
+ return dummy;
+ }
+
+ public int timeToString(int reps) {
+ Double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy += value.toString().length();
+ }
+ return dummy;
+ }
+
+ public int timeStringValueOf(int reps) {
+ Double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy += String.valueOf(value).length();
+ }
+ return dummy;
+ }
+
+ public int timeQuoteTrick(int reps) {
+ Double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy = ("" + value).length();
+ }
+ return dummy;
+ }
+
+ // TODO: remove this from all examples when IDE plugins are ready
+ public static void main(String[] args) throws Exception {
+ Runner.main(BoxedDoubleToStringBenchmark.class, args);
+ }
+}
diff --git a/test/com/google/caliper/examples/CharacterBenchmark.java b/test/com/google/caliper/examples/CharacterBenchmark.java
new file mode 100644
index 0000000..3ffeb01
--- /dev/null
+++ b/test/com/google/caliper/examples/CharacterBenchmark.java
@@ -0,0 +1,173 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.google.caliper.examples;
+
+import com.google.caliper.Benchmark;
+import com.google.caliper.Param;
+import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
+
+import java.util.Collection;
+import java.util.EnumSet;
+
+/**
+ * Tests various Character methods, intended for testing multiple
+ * implementations against each other.
+ */
+public class CharacterBenchmark extends SimpleBenchmark {
+
+ @Param CharacterSet characterSet;
+ static Collection<CharacterSet> characterSetValues = EnumSet.allOf(CharacterSet.class);
+
+ char[] values;
+
+ @Override protected void setUp() throws Exception {
+ values = characterSet.chars;
+ }
+
+ enum CharacterSet {
+ ASCII(128),
+ UNICODE(65536);
+ char[] chars;
+ CharacterSet(int size) {
+ chars = new char[size];
+ for (int i = 0; i < chars.length; ++i) {
+ chars[i] = (char) i;
+ }
+ }
+ }
+
+ public void timeDigit(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.digit(ch, 10);
+ }
+ }
+ }
+
+ public void timeGetNumericValue(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.getNumericValue(ch);
+ }
+ }
+ }
+
+ public void timeIsDigit(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isDigit(ch);
+ }
+ }
+ }
+
+ public void timeIsIdentifierIgnorable(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isIdentifierIgnorable(ch);
+ }
+ }
+ }
+
+ public void timeIsJavaIdentifierPart(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isJavaIdentifierPart(ch);
+ }
+ }
+ }
+
+ public void timeIsJavaIdentifierStart(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isJavaIdentifierStart(ch);
+ }
+ }
+ }
+
+ public void timeIsLetter(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isLetter(ch);
+ }
+ }
+ }
+
+ public void timeIsLetterOrDigit(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isLetterOrDigit(ch);
+ }
+ }
+ }
+
+ public void timeIsLowerCase(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isLowerCase(ch);
+ }
+ }
+ }
+
+ public void timeIsSpaceChar(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isSpaceChar(ch);
+ }
+ }
+ }
+
+ public void timeIsUpperCase(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isUpperCase(ch);
+ }
+ }
+ }
+
+ public void timeIsWhitespace(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.isWhitespace(ch);
+ }
+ }
+ }
+
+ public void timeIsNull(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ boolean b = (ch == ' ');
+ }
+ }
+ }
+
+ public void timeToLowerCase(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.toLowerCase(ch);
+ }
+ }
+ }
+
+ public void timeToUpperCase(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ for (char ch = 0; ch < '}'; ++ch) {
+ Character.toUpperCase(ch);
+ }
+ }
+ }
+}
diff --git a/test/com/google/caliper/examples/DoubleToStringBenchmarkSuite.java b/test/com/google/caliper/examples/DoubleToStringBenchmarkSuite.java
deleted file mode 100644
index a5d5234..0000000
--- a/test/com/google/caliper/examples/DoubleToStringBenchmarkSuite.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (C) 2009 Google Inc.
- *
- * 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.google.caliper.examples;
-
-import com.google.caliper.Benchmark;
-import com.google.caliper.DefaultBenchmarkSuite;
-import com.google.caliper.Param;
-import com.google.caliper.Runner;
-
-import java.util.Arrays;
-import java.util.Collection;
-
-/**
- * Measures the various ways the JDK converts doubles to Strings.
- */
-public class DoubleToStringBenchmarkSuite extends DefaultBenchmarkSuite {
-
- @Param private Double d;
-
- private static Collection<Double> dValues = Arrays.asList(
- Math.PI,
- -0.0d,
- Double.NEGATIVE_INFINITY,
- Double.NaN
- );
-
- class FormatterBenchmark extends Benchmark {
- public Object run(int trials) {
- Double value = d;
- String result = null;
- for (int i = 0; i < trials; i++) {
- result = String.format("%f", value);
- }
- return result;
- }
- }
-
- class ToStringBenchmark extends Benchmark {
- public Object run(int trials) {
- Double value = d;
- String result = null;
- for (int i = 0; i < trials; i++) {
- result = value.toString();
- }
- return result;
- }
- }
-
- class ConcatenationBenchmark extends Benchmark {
- public Object run(int trials) {
- Double value = d;
- String result = null;
- for (int i = 0; i < trials; i++) {
- result = "" + value;
- }
- return result;
- }
- }
-
- public static void main(String[] args) {
- Runner.main(DoubleToStringBenchmarkSuite.class, args);
- }
-}
diff --git a/test/com/google/caliper/examples/EnumSetContainsBenchmarkSuite.java b/test/com/google/caliper/examples/EnumSetContainsBenchmark.java
index da8f0f5..a9f6f2f 100644
--- a/test/com/google/caliper/examples/EnumSetContainsBenchmarkSuite.java
+++ b/test/com/google/caliper/examples/EnumSetContainsBenchmark.java
@@ -16,10 +16,9 @@
package com.google.caliper.examples;
-import com.google.caliper.Benchmark;
-import com.google.caliper.DefaultBenchmarkSuite;
import com.google.caliper.Param;
import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
import java.util.Collection;
import java.util.EnumSet;
@@ -28,11 +27,11 @@ import java.util.Set;
/**
* Measures EnumSet#contains().
*/
-public class EnumSetContainsBenchmarkSuite extends DefaultBenchmarkSuite {
+public class EnumSetContainsBenchmark extends SimpleBenchmark {
@Param private SetMaker setMaker;
- private static Collection<SetMaker> setMakerValues = EnumSet.allOf(SetMaker.class);
+ private static final Collection<SetMaker> setMakerValues = EnumSet.allOf(SetMaker.class);
enum SetMaker {
ENUM_SET {
@@ -85,19 +84,15 @@ public class EnumSetContainsBenchmarkSuite extends DefaultBenchmarkSuite {
this.testValues = setMaker.testValues();
}
- class ContainsBenchmark extends Benchmark {
- @Override public Object run(int trials) throws Exception {
- int count = 0;
- for (int i = 0; i < trials; i++) {
- for (Object value : testValues) {
- count ^= (set.contains(value) ? i : 0);
- }
- }
- return count > 0;
+ public int timeContains(int reps) throws Exception {
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy ^= (set.contains(testValues[i % testValues.length]) ? i : 0);
}
+ return dummy;
}
- public static void main(String[] args) {
- Runner.main(EnumSetContainsBenchmarkSuite.class, args);
+ public static void main(String[] args) throws Exception {
+ Runner.main(EnumSetContainsBenchmark.class, args);
}
} \ No newline at end of file
diff --git a/test/com/google/caliper/examples/ExpensiveObjectsBenchmark.java b/test/com/google/caliper/examples/ExpensiveObjectsBenchmark.java
new file mode 100644
index 0000000..2dbcf58
--- /dev/null
+++ b/test/com/google/caliper/examples/ExpensiveObjectsBenchmark.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.google.caliper.examples;
+
+import com.google.caliper.Benchmark;
+import com.google.caliper.Param;
+import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
+
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+import java.util.Locale;
+
+/**
+ * Benchmarks creation and cloning various expensive objects.
+ */
+public class ExpensiveObjectsBenchmark extends SimpleBenchmark {
+ public void timeNewDecimalFormatSymbols(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ new DecimalFormatSymbols(Locale.US);
+ }
+ }
+
+ public void timeClonedDecimalFormatSymbols(int reps) {
+ DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
+ for (int i = 0; i < reps; ++i) {
+ dfs.clone();
+ }
+ }
+
+ public void timeNewNumberFormat(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ NumberFormat.getInstance(Locale.US);
+ }
+ }
+
+ public void timeClonedNumberFormat(int reps) {
+ NumberFormat nf = NumberFormat.getInstance(Locale.US);
+ for (int i = 0; i < reps; ++i) {
+ nf.clone();
+ }
+ }
+
+ public void timeNewSimpleDateFormat(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ new SimpleDateFormat();
+ }
+ }
+
+ public void timeClonedSimpleDateFormat(int reps) {
+ SimpleDateFormat sdf = new SimpleDateFormat();
+ for (int i = 0; i < reps; ++i) {
+ sdf.clone();
+ }
+ }
+}
diff --git a/test/com/google/caliper/examples/FormatterBenchmark.java b/test/com/google/caliper/examples/FormatterBenchmark.java
new file mode 100644
index 0000000..f61f111
--- /dev/null
+++ b/test/com/google/caliper/examples/FormatterBenchmark.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.google.caliper.examples;
+
+import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
+
+import java.util.Formatter;
+
+/**
+ * Compares Formatter against hand-written StringBuilder code.
+ */
+public class FormatterBenchmark extends SimpleBenchmark {
+ public void timeFormatter_NoFormatting(int reps) {
+ for (int i = 0; i < reps; i++) {
+ Formatter f = new Formatter();
+ f.format("this is a reasonably short string that doesn't actually need any formatting");
+ }
+ }
+
+ public void timeStringBuilder_NoFormatting(int reps) {
+ for (int i = 0; i < reps; i++) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("this is a reasonably short string that doesn't actually need any formatting");
+ }
+ }
+
+ public void timeFormatter_OneInt(int reps) {
+ for (int i = 0; i < reps; i++) {
+ Formatter f = new Formatter();
+ f.format("this is a reasonably short string that has an int %d in it", i);
+ }
+ }
+
+ public void timeStringBuilder_OneInt(int reps) {
+ for (int i = 0; i < reps; i++) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("this is a reasonably short string that has an int ");
+ sb.append(i);
+ sb.append(" in it");
+ }
+ }
+
+ public void timeFormatter_OneString(int reps) {
+ for (int i = 0; i < reps; i++) {
+ Formatter f = new Formatter();
+ f.format("this is a reasonably short string that has a string %s in it", "hello");
+ }
+ }
+
+ public void timeStringBuilder_OneString(int reps) {
+ for (int i = 0; i < reps; i++) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("this is a reasonably short string that has a string ");
+ sb.append("hello");
+ sb.append(" in it");
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ Runner.main(FormatterBenchmark.class, args);
+ }
+}
diff --git a/test/com/google/caliper/examples/FormatterBenchmarkSuite.java b/test/com/google/caliper/examples/FormatterBenchmarkSuite.java
deleted file mode 100644
index 53dff71..0000000
--- a/test/com/google/caliper/examples/FormatterBenchmarkSuite.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright (C) 2009 Google Inc.
- *
- * 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.google.caliper.examples;
-
-import com.google.caliper.Benchmark;
-import com.google.caliper.DefaultBenchmarkSuite;
-import com.google.caliper.Param;
-import com.google.caliper.Runner;
-
-import java.util.Formatter;
-
-/**
- * Compares Formatter against hand-written StringBuilder code.
- */
-public class FormatterBenchmarkSuite extends DefaultBenchmarkSuite {
-
- class Formatter_NoFormatting extends Benchmark {
- public Object run(int trials) {
- for (int i = 0; i < trials; i++) {
- Formatter f = new Formatter();
- f.format("this is a reasonably short string that doesn't actually need any formatting");
- }
- return null;
- }
- }
-
- class StringBuilder_NoFormatting extends Benchmark {
- public Object run(int trials) {
- for (int i = 0; i < trials; i++) {
- StringBuilder sb = new StringBuilder();
- sb.append("this is a reasonably short string that doesn't actually need any formatting");
- }
- return null;
- }
- }
-
- class Formatter_OneInt extends Benchmark {
- public Object run(int trials) {
- for (int i = 0; i < trials; i++) {
- Formatter f = new Formatter();
- f.format("this is a reasonably short string that has an int %d in it", i);
- }
- return null;
- }
- }
-
- class StringBuilder_OneInt extends Benchmark {
- public Object run(int trials) {
- for (int i = 0; i < trials; i++) {
- StringBuilder sb = new StringBuilder();
- sb.append("this is a reasonably short string that has an int ");
- sb.append(i);
- sb.append(" in it");
- }
- return null;
- }
- }
-
- class Formatter_OneString extends Benchmark {
- public Object run(int trials) {
- for (int i = 0; i < trials; i++) {
- Formatter f = new Formatter();
- f.format("this is a reasonably short string that has a string %s in it", "hello");
- }
- return null;
- }
- }
-
- class StringBuilder_OneString extends Benchmark {
- public Object run(int trials) {
- for (int i = 0; i < trials; i++) {
- StringBuilder sb = new StringBuilder();
- sb.append("this is a reasonably short string that has a string ");
- sb.append("hello");
- sb.append(" in it");
- }
- return null;
- }
- }
-
- public static void main(String[] args) {
- Runner.main(FormatterBenchmarkSuite.class, args);
- }
-}
diff --git a/test/com/google/caliper/examples/IntModBenchmark.java b/test/com/google/caliper/examples/IntModBenchmark.java
new file mode 100644
index 0000000..86e85e7
--- /dev/null
+++ b/test/com/google/caliper/examples/IntModBenchmark.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.google.caliper.examples;
+
+import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
+
+/**
+ * Measures several candidate implementations for mod().
+ */
+public class IntModBenchmark extends SimpleBenchmark {
+ private static final int M = (1 << 16) - 1;
+
+ public int timeConditional(int reps) {
+ int dummy = 5;
+ for (int i = 0; i < reps; i++) {
+ dummy += Integer.MAX_VALUE + conditionalMod(dummy, M);
+ }
+ return dummy;
+ }
+
+ private static int conditionalMod(int a, int m) {
+ int r = a % m;
+ return r < 0 ? r + m : r;
+ }
+
+ public int timeDoubleRemainder(int reps) {
+ int dummy = 5;
+ for (int i = 0; i < reps; i++) {
+ dummy += Integer.MAX_VALUE + doubleRemainderMod(dummy, M);
+ }
+ return dummy;
+ }
+
+ private static int doubleRemainderMod(int a, int m) {
+ return (int) (((a % m) + (long) m) % m);
+ }
+
+ public int timeRightShiftingMod(int reps) {
+ int dummy = 5;
+ for (int i = 0; i < reps; i++) {
+ dummy += Integer.MAX_VALUE + rightShiftingMod(dummy, M);
+ }
+ return dummy;
+ }
+
+ private static int rightShiftingMod(int a, int m) {
+ long r = a % m;
+ return (int) (r + ((r >> 63) & m));
+ }
+
+ public int timeLeftShiftingMod(int reps) {
+ int dummy = 5;
+ for (int i = 0; i < reps; i++) {
+ dummy += Integer.MAX_VALUE + leftShiftingMod(dummy, M);
+ }
+ return dummy;
+ }
+
+ private static int leftShiftingMod(int a, int m) {
+ return (int) ((a + (((long) m) << 32)) % m);
+ }
+
+ public int timeWrongMod(int reps) {
+ int dummy = 5;
+ for (int i = 0; i < reps; i++) {
+ dummy += Integer.MAX_VALUE + dummy % M;
+ }
+ return dummy;
+ }
+
+ // TODO: remove this from all examples when IDE plugins are ready
+ public static void main(String[] args) throws Exception {
+ Runner.main(IntModBenchmark.class, args);
+ }
+} \ No newline at end of file
diff --git a/test/com/google/caliper/examples/ListIterationBenchmarkSuite.java b/test/com/google/caliper/examples/ListIterationBenchmark.java
index 0ed7197..53bcdf8 100644
--- a/test/com/google/caliper/examples/ListIterationBenchmarkSuite.java
+++ b/test/com/google/caliper/examples/ListIterationBenchmark.java
@@ -16,26 +16,27 @@
package com.google.caliper.examples;
-import com.google.caliper.Benchmark;
-import com.google.caliper.DefaultBenchmarkSuite;
import com.google.caliper.Param;
import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
-import java.util.*;
+import java.util.AbstractList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
/**
* Measures iterating through list elements.
*/
-public class ListIterationBenchmarkSuite extends DefaultBenchmarkSuite {
-
+public class ListIterationBenchmark extends SimpleBenchmark {
@Param private int length;
- private static Collection<Integer> lengthValues = Arrays.asList(0, 10, 100, 1000);
+ private static final Collection<Integer> lengthValues = Arrays.asList(0, 10, 100, 1000);
private List<Object> list;
private Object[] array;
- @Override protected void setUp() throws Exception {
+ @Override protected void setUp() {
array = new Object[length];
for (int i = 0; i < length; i++) {
array[i] = new Object();
@@ -52,31 +53,28 @@ public class ListIterationBenchmarkSuite extends DefaultBenchmarkSuite {
};
}
- class ListIterateBenchmark extends Benchmark {
- @Override public Object run(int trials) throws Exception {
- int count = 0;
- for (int i = 0; i < trials; i++) {
- for (Object value : list) {
- count ^= (value == Boolean.TRUE) ? i : 0;
- }
+ public int timeListIteration(int reps) {
+ int count = 0;
+ for (int i = 0; i < reps; i++) {
+ for (Object value : list) {
+ count ^= value.hashCode(); // prevent overoptimization
}
- return count > 0;
}
+ return count; // ignored
}
- class ArrayIterateBenchmark extends Benchmark {
- @Override public Object run(int trials) throws Exception {
- int count = 0;
- for (int i = 0; i < trials; i++) {
- for (Object value : array) {
- count ^= (value == Boolean.TRUE) ? i : 0;
- }
+ public int timeArrayIteration(int reps) {
+ int count = 0;
+ for (int i = 0; i < reps; i++) {
+ for (Object value : array) {
+ count ^= value.hashCode(); // prevent overoptimization
}
- return count > 0;
}
+ return count; // ignored
}
- public static void main(String[] args) {
- Runner.main(ListIterationBenchmarkSuite.class, args);
+ // TODO: remove this from all examples when IDE plugins are ready
+ public static void main(String[] args) throws Exception {
+ Runner.main(ListIterationBenchmark.class, args);
}
} \ No newline at end of file
diff --git a/test/com/google/caliper/examples/PrimitiveDoubleToStringBenchmark.java b/test/com/google/caliper/examples/PrimitiveDoubleToStringBenchmark.java
new file mode 100644
index 0000000..592acdc
--- /dev/null
+++ b/test/com/google/caliper/examples/PrimitiveDoubleToStringBenchmark.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.google.caliper.examples;
+
+import com.google.caliper.SimpleBenchmark;
+import com.google.caliper.Param;
+import com.google.caliper.Runner;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Measures the various ways the JDK converts primitive doubles to Strings.
+ */
+public class PrimitiveDoubleToStringBenchmark extends SimpleBenchmark {
+
+ @Param private double d;
+
+ private static final Collection<Double> dValues = Arrays.asList(
+ Math.PI,
+ -0.0d,
+ Double.NEGATIVE_INFINITY,
+ Double.NaN
+ );
+
+ public int timeStringFormat(int reps) {
+ double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy += String.format("%f", value).length();
+ }
+ return dummy;
+ }
+
+ public int timeToString(int reps) {
+ double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy += ((Double) value).toString().length();
+ }
+ return dummy;
+ }
+
+ public int timeStringValueOf(int reps) {
+ double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy += String.valueOf(value).length();
+ }
+ return dummy;
+ }
+
+ public int timeQuoteTrick(int reps) {
+ double value = d;
+ int dummy = 0;
+ for (int i = 0; i < reps; i++) {
+ dummy = ("" + value).length();
+ }
+ return dummy;
+ }
+
+ // TODO: remove this from all examples when IDE plugins are ready
+ public static void main(String[] args) throws Exception {
+ Runner.main(PrimitiveDoubleToStringBenchmark.class, args);
+ }
+} \ No newline at end of file
diff --git a/test/com/google/caliper/examples/StringBuilderBenchmark.java b/test/com/google/caliper/examples/StringBuilderBenchmark.java
new file mode 100644
index 0000000..2fa6819
--- /dev/null
+++ b/test/com/google/caliper/examples/StringBuilderBenchmark.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2009 Google Inc.
+ *
+ * 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.google.caliper.examples;
+
+import com.google.caliper.Benchmark;
+import com.google.caliper.Param;
+import com.google.caliper.Runner;
+import com.google.caliper.SimpleBenchmark;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * Tests the performance of various StringBuilder methods.
+ */
+public class StringBuilderBenchmark extends SimpleBenchmark {
+
+ @Param int length;
+ static Collection<Integer> lengthValues = Arrays.asList(1, 10, 100);
+
+ public void timeAppendBoolean(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(true);
+ }
+ }
+ }
+
+ public void timeAppendChar(int reps) {
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append('c');
+ }
+ }
+ }
+
+ public void timeAppendCharArray(int reps) {
+ char[] chars = "chars".toCharArray();
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(chars);
+ }
+ }
+ }
+
+ public void timeAppendCharSequence(int reps) {
+ CharSequence cs = "chars";
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(cs);
+ }
+ }
+ }
+
+ public void timeAppendDouble(int reps) {
+ double d = 1.2;
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(d);
+ }
+ }
+ }
+
+ public void timeAppendFloat(int reps) {
+ float f = 1.2f;
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(f);
+ }
+ }
+ }
+
+ public void timeAppendInt(int reps) {
+ int n = 123;
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(n);
+ }
+ }
+ }
+
+ public void timeAppendLong(int reps) {
+ long l = 123;
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(l);
+ }
+ }
+ }
+
+ public void timeAppendObject(int reps) {
+ Object o = new Object();
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(o);
+ }
+ }
+ }
+
+ public void timeAppendString(int reps) {
+ String s = "chars";
+ for (int i = 0; i < reps; ++i) {
+ StringBuilder sb = new StringBuilder();
+ for (int j = 0; j < length; ++j) {
+ sb.append(s);
+ }
+ }
+ }
+}