groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cchamp...@apache.org
Subject [1/2] incubator-groovy git commit: Added a compiler performance test module
Date Mon, 21 Sep 2015 07:32:11 GMT
Repository: incubator-groovy
Updated Branches:
  refs/heads/master 8e9716256 -> 6dbfae85e


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6dbfae85/benchmark/bench/moments.java
----------------------------------------------------------------------
diff --git a/benchmark/bench/moments.java b/benchmark/bench/moments.java
index 56ce716..7f08dd1 100644
--- a/benchmark/bench/moments.java
+++ b/benchmark/bench/moments.java
@@ -1,77 +1,77 @@
-// $Id: moments.java,v 1.1 2004-11-23 08:08:44 bfulgham Exp $
-// http://www.bagley.org/~doug/shootout/
-
-import java.io.*;
-import java.util.*;
-import java.text.*;
-import java.lang.Math;
-
-public class moments {
-    public static void main(String[] args) {
-    String line;
-    Vector nums = new Vector();
-    double num, sum = 0.0;
-    double mean = 0.0;
-    double average_deviation = 0.0;
-    double standard_deviation = 0.0;
-    double variance = 0.0;
-    double skew = 0.0;
-    double kurtosis = 0.0;
-    double median = 0.0;
-    double deviation = 0.0;
-    int i, n, mid = 0;
-
-        try {
-            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
-            while ((line = in.readLine()) != null) {
-        num = Double.parseDouble(line);
-        sum += num;
-        nums.add(new Double(num));
-            }
-        } catch (IOException e) {
-            System.err.println(e);
-            return;
-        }
-
-    n = nums.size();
-    mean = sum/n;
-    for (i=0; i<n; i++) {
-        deviation = ((Double)nums.get(i)).doubleValue() - mean;
-        average_deviation += Math.abs(deviation);
-        variance += Math.pow(deviation,2);
-        skew += Math.pow(deviation,3);
-        kurtosis += Math.pow(deviation,4);
-    }
-    average_deviation /= n;
-    variance /= (n - 1);
-    standard_deviation = Math.sqrt(variance);
-    if (variance != 0.0) {
-        skew /= (n * variance * standard_deviation);
-        kurtosis = kurtosis/(n * variance * variance) - 3.0;
-    }
-    
-    Collections.sort(nums);
-
-    mid = (n/2);
-    median = (n % 2 != 0) ?
-        ((Double)nums.get(mid)).doubleValue() :
-        (((Double)nums.get(mid)).doubleValue() +
-         ((Double)nums.get(mid-1)).doubleValue())/2;
-    
-    NumberFormat nf = NumberFormat.getInstance();
-    nf.setMaximumFractionDigits(13);
-    nf.setGroupingUsed(false);
-    nf.setMaximumFractionDigits(6);
-    nf.setMinimumFractionDigits(6);
-
-    System.out.println("n:                  " + n);
-    System.out.println("median:             " + nf.format(median));
-    System.out.println("mean:               " + nf.format(mean));
-    System.out.println("average_deviation:  " + nf.format(average_deviation));
-    System.out.println("standard_deviation: " + nf.format(standard_deviation));
-    System.out.println("variance:           " + nf.format(variance));
-    System.out.println("skew:               " + nf.format(skew));
-    System.out.println("kurtosis:           " + nf.format(kurtosis));
-    }
-}
-
+// $Id: moments.java,v 1.1 2004-11-23 08:08:44 bfulgham Exp $
+// http://www.bagley.org/~doug/shootout/
+
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.lang.Math;
+
+public class moments {
+    public static void main(String[] args) {
+    String line;
+    Vector nums = new Vector();
+    double num, sum = 0.0;
+    double mean = 0.0;
+    double average_deviation = 0.0;
+    double standard_deviation = 0.0;
+    double variance = 0.0;
+    double skew = 0.0;
+    double kurtosis = 0.0;
+    double median = 0.0;
+    double deviation = 0.0;
+    int i, n, mid = 0;
+
+        try {
+            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+            while ((line = in.readLine()) != null) {
+        num = Double.parseDouble(line);
+        sum += num;
+        nums.add(new Double(num));
+            }
+        } catch (IOException e) {
+            System.err.println(e);
+            return;
+        }
+
+    n = nums.size();
+    mean = sum/n;
+    for (i=0; i<n; i++) {
+        deviation = ((Double)nums.get(i)).doubleValue() - mean;
+        average_deviation += Math.abs(deviation);
+        variance += Math.pow(deviation,2);
+        skew += Math.pow(deviation,3);
+        kurtosis += Math.pow(deviation,4);
+    }
+    average_deviation /= n;
+    variance /= (n - 1);
+    standard_deviation = Math.sqrt(variance);
+    if (variance != 0.0) {
+        skew /= (n * variance * standard_deviation);
+        kurtosis = kurtosis/(n * variance * variance) - 3.0;
+    }
+    
+    Collections.sort(nums);
+
+    mid = (n/2);
+    median = (n % 2 != 0) ?
+        ((Double)nums.get(mid)).doubleValue() :
+        (((Double)nums.get(mid)).doubleValue() +
+         ((Double)nums.get(mid-1)).doubleValue())/2;
+    
+    NumberFormat nf = NumberFormat.getInstance();
+    nf.setMaximumFractionDigits(13);
+    nf.setGroupingUsed(false);
+    nf.setMaximumFractionDigits(6);
+    nf.setMinimumFractionDigits(6);
+
+    System.out.println("n:                  " + n);
+    System.out.println("median:             " + nf.format(median));
+    System.out.println("mean:               " + nf.format(mean));
+    System.out.println("average_deviation:  " + nf.format(average_deviation));
+    System.out.println("standard_deviation: " + nf.format(standard_deviation));
+    System.out.println("variance:           " + nf.format(variance));
+    System.out.println("skew:               " + nf.format(skew));
+    System.out.println("kurtosis:           " + nf.format(kurtosis));
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6dbfae85/benchmark/bench/wordfreq.groovy
----------------------------------------------------------------------
diff --git a/benchmark/bench/wordfreq.groovy b/benchmark/bench/wordfreq.groovy
index d4c7855..4bab668 100644
--- a/benchmark/bench/wordfreq.groovy
+++ b/benchmark/bench/wordfreq.groovy
@@ -1,5 +1,5 @@
-/*
- * The Great Computer Language Shootout
+/**
+ * The Computer Language Shootout
  * http://shootout.alioth.debian.org/
  *
  * contributed by Jochen Hinrichsen
@@ -13,7 +13,7 @@ System.in.eachLine() { line ->
     line.split("\\W").each() { word ->
         def s = word.toLowerCase()
         def entry = dict[s]
-        dict[s] = (entry == null) ? 1 : entry+1
+        dict[s] = (entry == null) ? 1 : entry + 1
     }
 }
 
@@ -21,7 +21,7 @@ System.in.eachLine() { line ->
 // sort for multiple properties: [ it.value, it.key ]
 assert dict != null
 assert dict.values() != null
-assert (dict.values().sort({ l, r -> r <=> l})) != null
-dict.values().sort({ l, r -> r <=> l}).each() { value ->
+assert (dict.values().sort({ l, r -> r <=> l })) != null
+dict.values().sort({ l, r -> r <=> l }).each() { value ->
     println "${value.toString().padLeft(8)}"
 }

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6dbfae85/settings.gradle
----------------------------------------------------------------------
diff --git a/settings.gradle b/settings.gradle
index e09d8a1..eccf7e8 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -31,12 +31,17 @@ def subprojects = ['groovy-ant',
         'groovy-templates',
         'groovy-test',
         'groovy-testng',
-        'groovy-xml']
+        'groovy-xml'
+]
 
 if(JavaVersion.current().isJava7Compatible()) {
     subprojects << 'groovy-nio'
 }
- 
+
+if (JavaVersion.current().isJava8Compatible()) {
+    subprojects << 'performance'
+}
+
 include(subprojects as String[])
         
 rootProject.children.each { prj ->

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6dbfae85/subprojects/performance/build.gradle
----------------------------------------------------------------------
diff --git a/subprojects/performance/build.gradle b/subprojects/performance/build.gradle
new file mode 100644
index 0000000..e7136a0
--- /dev/null
+++ b/subprojects/performance/build.gradle
@@ -0,0 +1,90 @@
+import java.text.DecimalFormat
+
+configurations {
+    stats
+    testCompile.extendsFrom(stats)
+}
+
+dependencies {
+    testCompile 'org.codehaus.groovy:groovy:2.4.4'
+    stats 'org.apache.commons:commons-math3:3.5'
+}
+
+sourceCompatibility = 1.8
+targetCompatibility = 1.8
+
+task performanceTests {
+    ext.outputDir = file("$buildDir/compilation")
+    ext.dataFile = file("$buildDir/compilation-stats.csv")
+
+    dependsOn rootProject.jarAll
+    doLast {
+        ext.outputDir.deleteDir()
+        def versions = []
+        dataFile.eachLine {
+            def (version, mean, stdDev) = it.split(';')
+            mean = Double.valueOf(mean)
+            stdDev = Double.valueOf(stdDev)
+            versions << [version == project.version ? 'current' : version, mean, stdDev]
+        }
+        versions = versions.sort { it[1] }
+        def fastest = versions[0][1]
+        versions.each { version, mean, stdDev ->
+            print "Groovy $version Average ${mean}ms ± ${new DecimalFormat("#.##").format(stdDev)}ms
"
+            if (mean > fastest) {
+                def diff = 100 * (mean - fastest) / fastest
+                print "(${new DecimalFormat("#.##").format(diff)}% slower)"
+            }
+            println()
+        }
+        dataFile.delete()
+    }
+}
+
+['1.8.9', '2.0.8', '2.1.9', '2.2.2', '2.3.10', '2.3.11', '2.4.4', 'current'].each { version
->
+    def t = task "performanceTestGroovy${version.replace('.', '_')}"(type: JavaExec) {
+        dependsOn compileTestJava
+        def groovyConf = configurations.detachedConfiguration(
+                dependencies.create(
+                        'current' == version ? files(rootProject.jarAll.archivePath) : "org.codehaus.groovy:groovy-all:$version")
+        )
+        groovyConf.transitive = false
+        main = 'org.apache.groovy.perf.CompilerPerformanceTest'
+        classpath = groovyConf + sourceSets.test.output + configurations.stats
+        jvmArgs = ['-Xms512m', '-Xmx512m', '-XX:MaxPermSize=512m']
+        def compileClassPath = [
+                '-cp',
+                groovyConf.files[0]
+        ]
+        rootProject.sourceSets.test.compileClasspath.files
+                .findAll { it.name.endsWith('jar') && !it.name.contains('groovy')
}
+                .collect(compileClassPath) { it.absolutePath }
+
+        // configure some files to compile. This is an arbitrary set of files which can be
compiled
+        // independently of the version of Groovy being tested
+        def testFiles = ['gls/CompilableTestSupport.groovy',
+                         'groovy/beans',
+                         'groovy/benchmarks',
+                         'groovy/execute',
+                         'groovy/gpath',
+                         'groovy/io',
+                         'groovy/script',
+                         'groovy/time',
+                         'groovy/tree',
+                         'gls/syntax'].collect { "../../src/test/$it" }
+        ['groovy-ant', 'groovy-test', 'groovy-jsr223'].collect(testFiles) { "../../subprojects/$it/src/test/groovy"
}
+
+        ['ackermann', 'fibo', 'random', 'spectralnorm', 'ary', 'hello', 'recursive', 'threadring',
+         'binarytrees', 'mandelbrot', 'regexdna', 'wordfreq',
+         'fannkuch', 'nsieve', 'revcomp'].collect(testFiles) {
+            "../../benchmark/bench/${it}.groovy"
+        }
+
+        args = [
+                *testFiles,
+                *compileClassPath
+        ]
+
+    }
+    performanceTests.dependsOn(t)
+}

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6dbfae85/subprojects/performance/src/test/java/org/apache/groovy/perf/CompilerPerformanceTest.java
----------------------------------------------------------------------
diff --git a/subprojects/performance/src/test/java/org/apache/groovy/perf/CompilerPerformanceTest.java
b/subprojects/performance/src/test/java/org/apache/groovy/perf/CompilerPerformanceTest.java
new file mode 100644
index 0000000..a31fa55
--- /dev/null
+++ b/subprojects/performance/src/test/java/org/apache/groovy/perf/CompilerPerformanceTest.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2003-2015 the original author or authors.
+ *
+ * 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 org.apache.groovy.perf;
+
+import groovy.lang.GroovySystem;
+import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CompilerPerformanceTest {
+    private final static String GROOVY_VERSION = GroovySystem.getVersion();
+    private final static int WARMUP = 200;
+    private final static int REPEAT = 1000;
+
+    public static void main(String[] args) throws Exception {
+        List<File> sources = new ArrayList<>();
+        List<URL> classpath = new ArrayList<>();
+        boolean isCp = false;
+        for (String arg : args) {
+            if ("-cp".equals(arg)) {
+                isCp = true;
+            } else if (isCp) {
+                classpath.add(new File(arg).toURI().toURL());
+            } else {
+                sources.add(new File(arg));
+            }
+        }
+        ScriptCompilationExecuter executer = new ScriptCompilationExecuter(
+                sources.toArray(new File[sources.size()]),
+                classpath
+        );
+        System.out.println("Using Groovy " + GROOVY_VERSION);
+
+        DescriptiveStatistics stats = new DescriptiveStatistics();
+
+        for (int i=0;i<WARMUP+REPEAT;i++) {
+            if (i<WARMUP) {
+                System.out.println("Warmup #" + (i+1));
+            } else {
+                System.out.println("Round #" + (i-WARMUP));
+            }
+            long dur = executer.execute();
+            System.gc();
+            System.out.printf("Compile time = %dms%n", dur);
+            if (i>=WARMUP) {
+                stats.addValue((double) dur);
+            }
+        }
+
+
+        System.out.println("Compilation took " + stats.getMean() + "ms ± " + stats.getStandardDeviation()
+ "ms");
+        FileWriter wrt = new FileWriter(new File("target/compilation-stats.csv"), true);
+        wrt.append(String.format("%s;%s;%s\n", GROOVY_VERSION, stats.getMean(), stats.getStandardDeviation()));
+        wrt.close();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/6dbfae85/subprojects/performance/src/test/java/org/apache/groovy/perf/ScriptCompilationExecuter.java
----------------------------------------------------------------------
diff --git a/subprojects/performance/src/test/java/org/apache/groovy/perf/ScriptCompilationExecuter.java
b/subprojects/performance/src/test/java/org/apache/groovy/perf/ScriptCompilationExecuter.java
new file mode 100644
index 0000000..7c97580
--- /dev/null
+++ b/subprojects/performance/src/test/java/org/apache/groovy/perf/ScriptCompilationExecuter.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2003-2015 the original author or authors.
+ *
+ * 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 org.apache.groovy.perf;
+
+import groovy.lang.GroovyClassLoader;
+import org.codehaus.groovy.control.CompilationUnit;
+import org.codehaus.groovy.control.CompilePhase;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+public class ScriptCompilationExecuter {
+    private final File[] sources;
+    private final URL[] classpath;
+
+    public ScriptCompilationExecuter(File[] sourceDirectories, final List<URL> classpath)
throws IOException {
+        this.classpath = classpath.toArray(new URL[classpath.size()]);
+        Set<File> sources = new HashSet<>();
+        for (File sourceDirectory : sourceDirectories) {
+            Files.walk(sourceDirectory.toPath())
+                    .filter(path -> {
+                        File file = path.toFile();
+                        return file.isFile()
+                                && file.getName().endsWith(".groovy");
+                    })
+                    .forEach(path -> sources.add(path.toFile()));
+        }
+        this.sources = sources.toArray(new File[sources.size()]);
+        System.out.println("sources = " + sources.size());
+    }
+
+    public long execute() throws Exception {
+        ClassLoader cl = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader().getParent());
+        GroovyClassLoader gcl = new GroovyClassLoader(cl);
+        CompilationUnit cu = new CompilationUnit(new CompilerConfiguration(), null, gcl,
new GroovyClassLoader(this.getClass().getClassLoader()));
+        for (File source : sources) {
+            cu.addSource(source);
+        }
+        long sd = System.nanoTime();
+        cu.compile(CompilePhase.CLASS_GENERATION.getPhaseNumber());
+        long dur = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sd);
+        return dur;
+    }
+}


Mime
View raw message