Ranides Atterwim 10 éve
szülő
commit
be9c5b58b5

+ 3 - 1
.gitignore

@@ -8,4 +8,6 @@
 /assira.drafts/target/
 /assira/pom.xml.versionsBackup
 /assira.test/target/
-/assira.drafts/nbproject/
+/assira.drafts/nbproject/
+/assira.benchmark/target/
+/assira.benchmark/nbproject/

+ 180 - 0
assira.benchmark/pom.xml

@@ -0,0 +1,180 @@
+<!--
+Copyright (c) 2014, Oracle America, Inc.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+   this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+ * Neither the name of Oracle nor the names of its contributors may be used
+   to endorse or promote products derived from this software without
+   specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+THE POSSIBILITY OF SUCH DAMAGE.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>net.ranides</groupId>
+    <artifactId>assira.benchmark</artifactId>
+    <version>2.0.1</version>
+    <packaging>jar</packaging>
+
+    <name>JMH benchmark sample: Java</name>
+
+    <!--
+       This is the demo/sample template build script for building Java benchmarks with JMH.
+       Edit as needed.
+    -->
+
+    <prerequisites>
+        <maven>3.0</maven>
+    </prerequisites>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.openjdk.jmh</groupId>
+            <artifactId>jmh-core</artifactId>
+            <version>${jmh.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.openjdk.jmh</groupId>
+            <artifactId>jmh-generator-annprocess</artifactId>
+            <version>${jmh.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>assira</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest-core</artifactId>
+            <version>1.3</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <jmh.version>1.12</jmh.version>
+        <javac.target>1.6</javac.target>
+        <uberjar.name>benchmarks</uberjar.name>
+    </properties>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.1</version>
+                <configuration>
+                    <compilerVersion>${javac.target}</compilerVersion>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>2.2</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <finalName>${uberjar.name}</finalName>
+                            <transformers>
+                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <mainClass>org.openjdk.jmh.Main</mainClass>
+                                </transformer>
+                            </transformers>
+                            <filters>
+                                <filter>
+                                    <!--
+                                        Shading signed JARs will fail without this.
+                                        http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
+                                    -->
+                                    <artifact>*:*</artifact>
+                                    <excludes>
+                                        <exclude>META-INF/*.SF</exclude>
+                                        <exclude>META-INF/*.DSA</exclude>
+                                        <exclude>META-INF/*.RSA</exclude>
+                                    </excludes>
+                                </filter>
+                            </filters>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+        <pluginManagement>
+            <plugins>
+                <plugin>
+                    <artifactId>maven-clean-plugin</artifactId>
+                    <version>2.5</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-deploy-plugin</artifactId>
+                    <version>2.8.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-install-plugin</artifactId>
+                    <version>2.5.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-jar-plugin</artifactId>
+                    <version>2.4</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-javadoc-plugin</artifactId>
+                    <version>2.9.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-resources-plugin</artifactId>
+                    <version>2.6</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-site-plugin</artifactId>
+                    <version>3.3</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-source-plugin</artifactId>
+                    <version>2.2.1</version>
+                </plugin>
+                <plugin>
+                    <artifactId>maven-surefire-plugin</artifactId>
+                    <version>2.17</version>
+                </plugin>
+            </plugins>
+        </pluginManagement>
+    </build>
+
+</project>

+ 201 - 0
assira.benchmark/src/main/java/net/ranides/benchmark/IntArrayBenchmark.java

@@ -0,0 +1,201 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.benchmark;
+
+import java.util.Comparator;
+import java.util.Random;
+import net.ranides.assira.collection.IntComparator;
+import net.ranides.assira.collection.arrays.ArraySort;
+import net.ranides.assira.collection.arrays.IntArraySort;
+import org.openjdk.jmh.annotations.Benchmark;
+
+/**
+ * Benchmark for IntArraySort. We want to compare it with generic "NativeArray".
+ * Results:
+ *      1. IntArray is ~75% faster than NativeArray
+ *      2. IntComparator is 2 times faster than Comparator
+ *      3. Real overhead is very small. Even change from (a-b) to Integer.compare(a,b) 
+ *         changes results by ~50%. That's great.
+ * 
+ * 
+ * IntComparator (a,b) -> a-b
+ * QuickSort
+ *      IntArray(int)            64 ns    75 %
+ *      NativeArray(int)         86 ns   100 %
+ *      NativeArray(Integer)    158 ns   183 %
+ * 
+ * MergeSort
+ *      IntArray(int)            30 ns    81 %
+ *      IntArray(natural)        63 ns   170 % Integer#cmp vs (a-b)
+ *      NativeArray(int)         37 ns   100 %
+ *      NativeArray(Integer)    134 ns   362 %
+ *      NativeArray(natural)    110 ns   300 % Integer#cmp vs (a-b)
+ * 
+ * 
+ * -----------------
+ * 
+ * 
+ * IntComparator (a,b) -> Integer::compare
+ * QuickSort
+ *      IntArray(int)            73 ns    77 %
+ *      NativeArray(int)         94 ns   100 %
+ *      NativeArray(Integer)    179 ns   190 %
+ * 
+ * MergeSort
+ *      IntArray(int)            64 ns    94 %
+ *      IntArray(natural)        62 ns    91 %
+ *      NativeArray(int)         68 ns   100 %
+ *      NativeArray(Integer)    208 ns   305 %
+ *      NativeArray(natural)     74 ns   108 %
+ * 
+ * 
+ * -----------------
+ * 
+ * 
+ * IntComparator (a,b) -> Integer::compare
+ * QuickSort
+ *      IntArray(int)            79 ns    79 %
+ *      NativeArray(int)        100 ns   100 %
+ *      NativeArray(Integer)    170 ns   170 %
+ * 
+ * MergeSort
+ *      IntArray(int)            65 ns    79 %
+ *      IntArray(natural)        67 ns    81 %
+ *      NativeArray(int)         82 ns   100 %
+ *      NativeArray(Integer)    206 ns   251 %
+ *      NativeArray(natural)     82 ns   100 %
+ * 
+ * 
+ * -----------------
+ * 
+ * 
+ * QuickSort
+ *      IntArray                129/s       77 ns     78 %  OK
+ *      NativeArray(int)        102/s       98 ns    100 %  OK
+ *      NativeArray(Integer)     59/s      169 ns    172 %  OK
+ * 
+ * MergeSort
+ *      IntArray                152/s       66 ns     80 %  OK
+ *      IntArray(natural)       137/s       73 ns     89 %  OK 
+ *      NativeArray(int)        122/s       82 ns    100 %  OK
+ *      NativeArray_Integer      45/s      222 ns    270 %  OK
+ *      NativeArray(natural)    142/s       70 ns     85 %  ~
+
+ * 
+ */
+public class IntArrayBenchmark {
+    
+    private static final int REPS = 100;
+    
+    int[] array(int size) {
+        Random rand = new Random(777);
+        int[] array = new int[size];
+        for(int i=0; i<size; i++) {
+            array[i] = rand.nextInt();
+        }
+        return array;
+    }
+   
+    @Benchmark
+    public int timeRandomize() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeQuickSort_IntArray() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            IntArraySort.quickSort(array, (int a,int b) -> Integer.compare(a, b));
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeQuickSort_NativeArray_int() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            ArraySort.quickSort(array, (IntComparator)Integer::compare);
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeQuickSort_NativeArray_Integer() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            ArraySort.quickSort(array, (Comparator<Integer>)(a,b) -> Integer.compare(a, b));
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeMergeSort_IntArray() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            IntArraySort.mergeSort(array, (int a,int b) -> Integer.compare(a, b));
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeMergeSort_IntArray_NC() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            IntArraySort.mergeSort(array);
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeMergeSort_NativeArray_int() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            ArraySort.mergeSort(array, (IntComparator)(a,b) -> Integer.compare(a, b));
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeMergeSort_NativeArray_Integer() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            ArraySort.mergeSort(array, (Comparator<Integer>)(a,b) -> Integer.compare(a, b));
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+    @Benchmark
+    public int timeMergeSort_NativeArray_NC() {
+        int ret = 0;
+        for(int i=0; i<REPS; i++) {
+            int[] array = array(1000);
+            ArraySort.mergeSort(array);
+            ret += array[0] + array[999];
+        }
+        return ret;
+    }
+    
+}

+ 44 - 0
assira.benchmark/src/main/java/net/ranides/benchmark/MyBenchmark.java

@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2014, Oracle America, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ *  * Neither the name of Oracle nor the names of its contributors may be used
+ *    to endorse or promote products derived from this software without
+ *    specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package net.ranides.benchmark;
+
+import org.openjdk.jmh.annotations.Benchmark;
+
+public class MyBenchmark {
+
+    @Benchmark
+    public void testMethod() {
+        // This is a demo/sample template for building your JMH benchmarks. Edit as needed.
+        // Put your benchmark code here.
+    }
+
+}

+ 31 - 0
assira.benchmark/src/main/java/net/ranides/benchmark/app/App.java

@@ -0,0 +1,31 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/JMH benchmark sample: Java
+ */
+package net.ranides.benchmark.app;
+
+import java.io.IOException;
+import org.openjdk.jmh.Main;
+import org.openjdk.jmh.runner.RunnerException;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class App {
+    
+    public static void main(String[] args) throws RunnerException, IOException {
+        Main.main(new String[]{
+            "-rf", "csv",
+            "-rff", "../doc/perf.log", 
+            
+            "-bm", "thrpt", // Benchmark mode. thrpt,  avgt, sample, ss, all
+            "-f", "3",      // How many times to fork a single benchmark.
+            "-i", "3",      // Number of measurement iterations to do
+            "-wi", "3"      // Number of warmup iterations to do. 
+        });
+    }
+    
+}

BIN
doc/assira.benchmark.ods