Commits

Micha Kops  committed 5382d43

Examples for different benchmark visualizations added.

  • Participants
  • Parent commits ee3d048

Comments (0)

Files changed (7)

 		<dependency>
 			<groupId>com.carrotsearch</groupId>
 			<artifactId>junit-benchmarks</artifactId>
-			<version>0.5.0</version>
+			<version>0.6.0-SNAPSHOT</version>
 			<scope>test</scope>
 		</dependency>
 		<dependency>
 			<version>1.3.160</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-all</artifactId>
+			<version>1.3</version>
+			<scope>test</scope>
+		</dependency>
 	</dependencies>
 
 	<build>
 			<plugin>
 				<artifactId>maven-compiler-plugin</artifactId>
 				<configuration>
-					<source>1.7</source>
-					<target>1.7</target>
+					<source>1.6</source>
+					<target>1.6</target>
 				</configuration>
 			</plugin>
 		</plugins>

File src/test/java/com/hascode/tutorial/ClassUnderTest.java

+package com.hascode.tutorial;
+
+public class ClassUnderTest {
+	public boolean doTest() throws InterruptedException {
+		for (int i = 0; i < 3000000; i++)
+			new String("foo_" + i);
+
+		Thread.sleep(500);
+		return true;
+	}
+}

File src/test/java/com/hascode/tutorial/Foo.java

+package com.hascode.tutorial;
+
+
+public class Foo implements Comparable<Foo> {
+	private final Integer x;
+
+	public Foo(final int x) {
+		this.x = x;
+	}
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + x;
+		return result;
+	}
+
+	@Override
+	public boolean equals(final Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		Foo other = (Foo) obj;
+		if (x != other.x)
+			return false;
+		return true;
+	}
+
+	@Override
+	public int compareTo(final Foo o) {
+		return x.compareTo(o.x);
+	}
+}

File src/test/java/com/hascode/tutorial/GlobalBenchmarkOptionsExample.java

+package com.hascode.tutorial;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
+import com.carrotsearch.junitbenchmarks.BenchmarkRule;
+import com.carrotsearch.junitbenchmarks.Clock;
+
+@BenchmarkOptions(benchmarkRounds = 10, warmupRounds = 5, callgc = false, clock = Clock.REAL_TIME, concurrency = 4)
+public class GlobalBenchmarkOptionsExample {
+	ClassUnderTest classUnderTest = new ClassUnderTest();
+
+	@Rule
+	public BenchmarkRule benchmarkRun = new BenchmarkRule();
+
+	@Test
+	public void testSomething() throws Exception {
+		assertTrue(classUnderTest.doTest());
+	}
+}

File src/test/java/com/hascode/tutorial/GraphicOutputTests.java

-package com.hascode.tutorial;
-
-import static org.junit.Assert.assertTrue;
-
-import org.junit.Rule;
-import org.junit.Test;
-
-import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
-import com.carrotsearch.junitbenchmarks.BenchmarkRule;
-import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
-import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
-
-@AxisRange(min = 0, max = 1)
-@BenchmarkMethodChart(filePrefix = "benchmark-lists")
-public class GraphicOutputTests {
-	ClassUnderTest classUnderTest = new ClassUnderTest();
-
-	@Rule
-	public BenchmarkRule benchmarkRun = new BenchmarkRule();
-
-	@Test
-	public void testSomething() throws Exception {
-		assertTrue(classUnderTest.doTest());
-	}
-
-	@BenchmarkOptions(benchmarkRounds = 20, warmupRounds = 4)
-	@Test
-	public void testSomethingWithWarmups() throws Exception {
-		assertTrue(classUnderTest.doTest());
-	}
-
-	class ClassUnderTest {
-		public boolean doTest() throws InterruptedException {
-			for (int i = 0; i < 3000000; i++)
-				new String("foo_" + i);
-
-			Thread.sleep(500);
-			return true;
-		}
-	}
-
-}

File src/test/java/com/hascode/tutorial/HistoryChartOutputTests.java

+package com.hascode.tutorial;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.carrotsearch.junitbenchmarks.BenchmarkRule;
+import com.carrotsearch.junitbenchmarks.annotation.BenchmarkHistoryChart;
+import com.carrotsearch.junitbenchmarks.annotation.LabelType;
+
+@BenchmarkHistoryChart(filePrefix = "map-types-benchmark-history-chart", labelWith = LabelType.CUSTOM_KEY, maxRuns = 20)
+public class HistoryChartOutputTests {
+	@Rule
+	public BenchmarkRule benchmarkRun = new BenchmarkRule();
+
+	static final int MAX_ENTRIES = 1500000;
+
+	@Test
+	public void hashMap() throws Exception {
+		testMap(new HashMap<Integer, Foo>());
+	}
+
+	@Test
+	public void linkedHashMap() throws Exception {
+		testMap(new LinkedHashMap<Integer, Foo>());
+	}
+
+	@Test
+	public void treeMap() throws Exception {
+		testMap(new TreeMap<Integer, Foo>());
+	}
+
+	private void testMap(final Map<Integer, Foo> map) {
+		for (int i = 0; i < MAX_ENTRIES; i++) {
+			map.put(i, new Foo(i));
+		}
+		assertThat(map.size(), is(MAX_ENTRIES));
+		for (int i = 0; i < MAX_ENTRIES; i++) {
+			assertThat(map.get(i), is(notNullValue()));
+		}
+	}
+}

File src/test/java/com/hascode/tutorial/MapTypesBenchmarkWithBarChartExample.java

+package com.hascode.tutorial;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.carrotsearch.junitbenchmarks.BenchmarkRule;
+import com.carrotsearch.junitbenchmarks.annotation.AxisRange;
+import com.carrotsearch.junitbenchmarks.annotation.BenchmarkMethodChart;
+
+@AxisRange(min = 0, max = 1)
+@BenchmarkMethodChart(filePrefix = "map-types-benchmark-barchart")
+public class MapTypesBenchmarkWithBarChartExample {
+	@Rule
+	public BenchmarkRule benchmarkRun = new BenchmarkRule();
+
+	static final int MAX_ENTRIES = 1500000;
+
+	@Test
+	public void hashMap() throws Exception {
+		testMap(new HashMap<Integer, Foo>());
+	}
+
+	@Test
+	public void linkedHashMap() throws Exception {
+		testMap(new LinkedHashMap<Integer, Foo>());
+	}
+
+	@Test
+	public void treeMap() throws Exception {
+		testMap(new TreeMap<Integer, Foo>());
+	}
+
+	private void testMap(final Map<Integer, Foo> map) {
+		for (int i = 0; i < MAX_ENTRIES; i++) {
+			map.put(i, new Foo(i));
+		}
+		assertThat(map.size(), is(MAX_ENTRIES));
+		for (int i = 0; i < MAX_ENTRIES; i++) {
+			assertThat(map.get(i), is(notNullValue()));
+		}
+	}
+}