Ranides Atterwim %!s(int64=10) %!d(string=hai) anos
pai
achega
4b03d9e92d

+ 49 - 0
assira.benchmark/src/main/java/net/ranides/assira/trace/EQException.java

@@ -0,0 +1,49 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira.drafts
+ */
+
+package net.ranides.assira.trace;
+
+import java.lang.management.ManagementFactory;
+import java.text.MessageFormat;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class EQException extends NSTException {
+    
+    private static final AtomicInteger COUNTER = new AtomicInteger(0);
+    
+    private static final class Li { // NOPMD - lazy init idiom
+        
+        public static final String VMNAME = ManagementFactory.getRuntimeMXBean().getName();
+        
+    }
+    
+    private final String uid;
+    private final String text;
+    
+    public EQException(String message) {
+        text = message;
+        uid = Li.VMNAME + "@" + COUNTER.incrementAndGet();
+    }
+
+    public EQException(String pattern, Object... params) {
+        this(MessageFormat.format(pattern, params));
+    }
+
+    @Override
+    public String toString() {
+        return "EMException{ " + uid + " : " + text + " }";
+    }
+
+    public String getUid() {
+        return uid;
+    }
+
+}

+ 154 - 129
assira.benchmark/src/main/java/net/ranides/assira/trace/NSTExceptionBenchmark.java

@@ -1,130 +1,155 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira.benchmark
- */
-package net.ranides.assira.trace;
-
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.util.Random;
-import net.ranides.benchmark.app.BenchmarkName;
-import net.ranides.benchmark.app.BenchmarkRunner;
-import org.openjdk.jmh.annotations.Benchmark;
-import org.openjdk.jmh.annotations.Level;
-import org.openjdk.jmh.annotations.Scope;
-import org.openjdk.jmh.annotations.Setup;
-import org.openjdk.jmh.annotations.State;
-import org.openjdk.jmh.annotations.Threads;
-import org.openjdk.jmh.runner.RunnerException;
-
-@Threads(1)
-@State(Scope.Thread)
-public class NSTExceptionBenchmark {
-    
-    private static final String SOMETHING_BAD_HAS_HAPPENED = "Something bad has happened";
-    
-    private UserException m_cached;
-    
-    private String m_str;
-
-    @Setup(Level.Iteration)
-    public void setup() {
-        m_cached = new UserException( SOMETHING_BAD_HAS_HAPPENED );
-        final byte[] bytes = new byte[ 100 ];
-        new Random().nextBytes( bytes );
-        m_str = new String( bytes, StandardCharsets.ISO_8859_1 );
-    }
-
-    @Benchmark
-    @BenchmarkName("Throw: cache")
-    public void throwCached() {
-        try {
-            throw m_cached;
-        }
-        catch ( UserException ignored ) {}
-    }
-
-    @Benchmark
-    @BenchmarkName("Throw: new")
-    public void throwNewException() {
-        try {
-            throw new UserException( SOMETHING_BAD_HAS_HAPPENED );
-        }
-        catch ( UserException ignored ) {}
-    }
-
-    @Benchmark
-    @BenchmarkName("Throw: new (no stacktrace)")
-    public void throwNewNST() {
-        try {
-            throw new NSTException0( SOMETHING_BAD_HAS_HAPPENED );
-        } 
-        catch ( NSTException0 ignored ) { }
-    }
-
-    @Benchmark
-    @BenchmarkName("Throw: new (classname)")
-    public void throwNewName() {
-        try {
-            throw new NSTException1( SOMETHING_BAD_HAS_HAPPENED );
-        } 
-        catch ( NSTException1 ignored ) { }
-    }
-
-    
-    @Benchmark
-    @BenchmarkName("Construct: new (no stacktrace)")
-    public NSTException0 createNST() {
-        return new NSTException0( SOMETHING_BAD_HAS_HAPPENED );
-    }
-    
-    @Benchmark
-    @BenchmarkName("Construct: new (classname)")
-    public NSTException1 createCN() {
-        return new NSTException1( SOMETHING_BAD_HAS_HAPPENED );
-    }
-
-    @Benchmark
-    @BenchmarkName("Construct: new")
-    public UserException createException() {
-        return new UserException( SOMETHING_BAD_HAS_HAPPENED );
-    }
-
-    @Benchmark
-    @BenchmarkName("Construct: object")
-    public UserObject createUserObject() {
-        return new UserObject( m_str );
-    }
-
-    private static class UserObject {
-        
-        private final String m_str;
-
-        private UserObject(String str) {
-            this.m_str = str;
-        }
-
-        public String getStr() {
-            return m_str;
-        }
-    }
-    
-    private static class UserAction {
-        
-        public void run() {
-            throw new NSTException1("Hello");
-        }
-        
-    }
-
-    public static void main(String[] args) throws RunnerException, IOException {
-        BenchmarkRunner.run(NSTExceptionBenchmark.class);
-    }
-    
-//    public static void main(String[] args) throws NSTException0, UserException {
-//        new UserAction().run();
-//    }
-    
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira.benchmark
+ */
+package net.ranides.assira.trace;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Random;
+import net.ranides.benchmark.app.BenchmarkName;
+import net.ranides.benchmark.app.BenchmarkRunner;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.runner.RunnerException;
+
+@Threads(1)
+@State(Scope.Thread)
+public class NSTExceptionBenchmark {
+    
+    private static final String SOMETHING_BAD_HAS_HAPPENED = "Something bad has happened";
+    
+    private UserException m_cached;
+    
+    private String m_str;
+
+    @Setup(Level.Iteration)
+    public void setup() {
+        m_cached = new UserException( SOMETHING_BAD_HAS_HAPPENED );
+        final byte[] bytes = new byte[ 100 ];
+        new Random().nextBytes( bytes );
+        m_str = new String( bytes, StandardCharsets.ISO_8859_1 );
+    }
+
+    @Benchmark
+    @BenchmarkName("Throw: cache")
+    public UserException throwCached() {
+        try {
+            throw m_cached;
+        }
+        catch ( UserException cause ) {
+            return cause;
+        }
+    }
+
+    @Benchmark
+    @BenchmarkName("Throw: new")
+    public UserException throwNewException() {
+        try {
+            throw new UserException( SOMETHING_BAD_HAS_HAPPENED );
+        }
+        catch ( UserException cause ) {
+            return cause;
+        }
+    }
+
+    @Benchmark
+    @BenchmarkName("Throw: new (no stacktrace)")
+    public NSTException0 throwNewNST() {
+        try {
+            throw new NSTException0( SOMETHING_BAD_HAS_HAPPENED );
+        } 
+        catch ( NSTException0 cause ) {
+            return cause;
+        }
+    }
+
+    @Benchmark
+    @BenchmarkName("Throw: new (classname)")
+    public NSTException1 throwNewName() {
+        try {
+            throw new NSTException1( SOMETHING_BAD_HAS_HAPPENED );
+        } 
+        catch ( NSTException1 cause ) { 
+            return cause;
+        }
+    }
+    
+    @Benchmark
+    @BenchmarkName("Throw: new (uid)")
+    public EQException throwUID() {
+        try {
+            throw new EQException( SOMETHING_BAD_HAS_HAPPENED );
+        } 
+        catch ( EQException cause ) { 
+            return cause;
+        }
+    }
+
+    
+    @Benchmark
+    @BenchmarkName("Construct: new (no stacktrace)")
+    public NSTException0 createNST() {
+        return new NSTException0( SOMETHING_BAD_HAS_HAPPENED );
+    }
+    
+    @Benchmark
+    @BenchmarkName("Construct: new (classname)")
+    public NSTException1 createCN() {
+        return new NSTException1( SOMETHING_BAD_HAS_HAPPENED );
+    }
+
+    @Benchmark
+    @BenchmarkName("Construct: new")
+    public UserException createException() {
+        return new UserException( SOMETHING_BAD_HAS_HAPPENED );
+    }
+    
+    @Benchmark
+    @BenchmarkName("Construct: new (uid)")
+    public EQException createUID() {
+        return new EQException( SOMETHING_BAD_HAS_HAPPENED );
+    }
+
+    @Benchmark
+    @BenchmarkName("Construct: object")
+    public UserObject createUserObject() {
+        return new UserObject( m_str );
+    }
+
+    private static class UserObject {
+        
+        private final String m_str;
+
+        private UserObject(String str) {
+            this.m_str = str;
+        }
+
+        public String getStr() {
+            return m_str;
+        }
+    }
+    
+    private static class UserAction {
+        
+        public void run() {
+            throw new NSTException1("Hello");
+        }
+        
+    }
+
+    public static void main(String[] args) throws RunnerException, IOException {
+        BenchmarkRunner.run(NSTExceptionBenchmark.class);
+    }
+    
+//    public static void main(String[] args) throws NSTException0, UserException {
+//        new UserAction().run();
+//    }
+    
 }

+ 71 - 67
assira.benchmark/src/main/java/net/ranides/benchmark/app/BenchmarkRunner.java

@@ -1,67 +1,71 @@
-/*
- * @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.File;
-import java.io.IOException;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
-import net.ranides.assira.reflection.IClass;
-import net.ranides.assira.text.Charsets;
-import net.ranides.assira.text.IOStrings;
-import org.openjdk.jmh.annotations.Mode;
-import org.openjdk.jmh.runner.Runner;
-import org.openjdk.jmh.runner.RunnerException;
-import org.openjdk.jmh.runner.options.Options;
-import org.openjdk.jmh.runner.options.OptionsBuilder;
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class BenchmarkRunner {
-    
-    public static void run(Class<?> benchmark) throws RunnerException, IOException {
-        Map<String, String> names = IClass.typeinfo(benchmark)
-            .methods()
-            .require(BenchmarkName.class)
-            .collect(Collectors.toMap(m -> m.name(), m -> m.annotation(BenchmarkName.class).value()));
-        
-        File file = new File("../doc/"+benchmark.getSimpleName()+".csv");
-        Iterator<String> re = new Runner(options(benchmark))
-            .run()
-            .stream()
-            .map(r -> r.getAggregatedResult().getPrimaryResult())
-            .map(r -> String.format("%s,%.0f,%.0f,%s", 
-                names.getOrDefault(r.getLabel(), r.getLabel()),
-                r.getScore(), 
-                r.getScoreError(), 
-                r.getScoreUnit()
-            ))
-            .iterator();
-        
-        IOStrings.writeLines(file, Charsets.UTF8, re);
-    }
-    
-    public static Options options(Class<?> benchmark) {
-        String name = benchmark.getSimpleName();
-        return new OptionsBuilder()
-            .include(".*" + name + ".*")
-            .timeUnit(TimeUnit.NANOSECONDS)
-            .mode(Mode.AverageTime)
-            .forks(3)
-            .measurementIterations(3)
-            .warmupIterations(3)
-            .build();
-    }
-    
-    public static void main(String[] args) {
-        
-    }
-    
-}
+/*
+ * @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.File;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import net.ranides.assira.reflection.IClass;
+import net.ranides.assira.text.Charsets;
+import net.ranides.assira.text.IOStrings;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class BenchmarkRunner {
+    
+    public static void run(Class<?> benchmark) throws RunnerException, IOException {
+        run(benchmark,3);
+    }
+    
+    public static void run(Class<?> benchmark, int iterations) throws RunnerException, IOException {
+        Map<String, String> names = IClass.typeinfo(benchmark)
+            .methods()
+            .require(BenchmarkName.class)
+            .collect(Collectors.toMap(m -> m.name(), m -> m.annotation(BenchmarkName.class).value()));
+        
+        File file = new File("../doc/"+benchmark.getSimpleName()+".csv");
+        Iterator<String> re = new Runner(options(benchmark,iterations))
+            .run()
+            .stream()
+            .map(r -> r.getAggregatedResult().getPrimaryResult())
+            .map(r -> String.format("%s,%.0f,%.0f,%s", 
+                names.getOrDefault(r.getLabel(), r.getLabel()),
+                r.getScore(), 
+                r.getScoreError(), 
+                r.getScoreUnit()
+            ))
+            .iterator();
+        
+        IOStrings.writeLines(file, Charsets.UTF8, re);
+    }
+    
+    public static Options options(Class<?> benchmark, int iterations) {
+        String name = benchmark.getSimpleName();
+        return new OptionsBuilder()
+            .include(".*" + name + ".*")
+            .timeUnit(TimeUnit.NANOSECONDS)
+            .mode(Mode.AverageTime)
+            .forks(3)
+            .measurementIterations(iterations)
+            .warmupIterations(3)
+            .build();
+    }
+    
+    public static void main(String[] args) {
+        
+    }
+    
+}

+ 97 - 0
assira.benchmark/src/main/java/net/ranides/contept/InvocationBenchmark.java

@@ -0,0 +1,97 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.contept;
+
+import net.ranides.assira.text.*;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Random;
+import java.util.regex.Pattern;
+import net.ranides.assira.collection.arrays.ArrayUtils;
+import net.ranides.assira.math.MathUtils;
+import net.ranides.benchmark.app.BenchmarkName;
+import net.ranides.benchmark.app.BenchmarkRunner;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.runner.RunnerException;
+
+/**
+ * 3 measurements:
+ *      interface   2'704   104.2 %
+ *      abstract    2'576    99.3 %
+ *      concrete    2'594   100.0 %
+ * 
+ * 10 measurements:
+ *      interface   2'582   104.9 %
+ *      abstract    2'459    99.9 %
+ *      concrete    2'461   100.0 %
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class InvocationBenchmark {
+    
+    private static ITestInterface mInterface = new CTestInterface();
+    
+    private static ITestAbstract mAbstract = new CTestAbstract();
+    
+    private static CTestConcrete mConcrete = new CTestConcrete();
+       
+    @Benchmark
+    @BenchmarkName("interface")
+    public int timeInterface() {
+        return mInterface.apply();
+    }
+    
+    @Benchmark
+    @BenchmarkName("abstract")
+    public int timeAbstract() {
+        return mAbstract.apply();
+    }
+    
+    @Benchmark
+    @BenchmarkName("concrete")
+    public int timeConcrete() {
+        return mConcrete.apply();
+    }
+    
+
+    public static void main(String[] args) throws IOException, RunnerException {
+        BenchmarkRunner.run(InvocationBenchmark.class,10);
+    }
+    
+    private static interface ITestInterface {
+        int apply();
+    }
+    
+    private static class CTestInterface implements ITestInterface {
+        
+        @Override
+        public int apply() {
+            return 777;
+        }
+    }
+    
+    private static abstract class ITestAbstract {
+        public abstract int apply();
+    }
+    
+    private static class CTestAbstract extends ITestAbstract {
+        
+        @Override
+        public int apply() {
+            return 777;
+        }
+    }
+    
+    private static class CTestConcrete {
+        
+        public int apply() {
+            return 777;
+        }
+    }
+}