Repository: groovy Updated Branches: refs/heads/master 010624d92 -> d20c047d3
migrate benchmarks to JMH (closes #583) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/d8705436 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d8705436 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d8705436 Branch: refs/heads/master Commit: d8705436ec0631a816ba0a4f88acd0b0797d6fe1 Parents: 010624d Author: John Wagenleitner <[email protected]> Authored: Sun Jul 23 12:54:48 2017 -0700 Committer: John Wagenleitner <[email protected]> Committed: Sat Aug 26 12:00:26 2017 -0700 ---------------------------------------------------------------------- .../org/apache/groovy/bench/AckermannBench.java | 63 ++++++++++++++++++ .../java/org/apache/groovy/bench/AryBench.java | 70 ++++++++++++++++++++ .../java/org/apache/groovy/bench/FiboBench.java | 62 +++++++++++++++++ .../groovy/bench/dispatch/CallsiteBench.java | 14 ++-- .../plugin/GroovyRunnerRegistryBench.java | 4 +- .../org/apache/groovy/bench/Ackermann.groovy | 29 ++++++++ .../groovy/org/apache/groovy/bench/Ary.groovy | 36 ++++++++++ .../groovy/org/apache/groovy/bench/Fibo.groovy | 28 ++++++++ 8 files changed, 298 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AckermannBench.java ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AckermannBench.java b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AckermannBench.java new file mode 100644 index 0000000..a49b69b --- /dev/null +++ b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AckermannBench.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.bench; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Fork(3) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +public class AckermannBench { + + @Param({"5", "6", "7", "8"}) + private int n; + + @Benchmark + public int java() { + return JavaAckermann.ack(3, n); + } + + @Benchmark + public int groovy() { + return Ackermann.ack(3, n); + } + + private static class JavaAckermann { + static int ack(int m, int n) { + if (m == 0) return n + 1; + if (n == 0) return ack(m - 1, 1); + return ack(m - 1, ack(m, n - 1)); + } + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AryBench.java ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AryBench.java b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AryBench.java new file mode 100644 index 0000000..73b088e --- /dev/null +++ b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/AryBench.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.bench; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Fork(3) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +public class AryBench { + + @Param({"10", "100", "1000", "1000000"}) + private int n; + + @Benchmark + public int java() { + return JavaAry.ary(n); + } + + @Benchmark + public int groovy() { + return Ary.ary(n); + } + + private static class JavaAry { + static int ary(int n) { + int[] x = new int[n]; + int[] y = new int[n]; + + for (int i = 0; i < n; i++) + x[i] = i + 1; + for (int k = 0; k < 1000; k++ ) + for (int j = n-1; j >= 0; j--) + y[j] += x[j]; + + return y[0] + y[n-1]; + } + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/jmh/java/org/apache/groovy/bench/FiboBench.java ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/jmh/java/org/apache/groovy/bench/FiboBench.java b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/FiboBench.java new file mode 100644 index 0000000..e834779 --- /dev/null +++ b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/FiboBench.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.bench; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) +@Fork(3) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +public class FiboBench { + + @Param({"30", "31", "32", "33", "34"}) + private int n; + + @Benchmark + public int java() { + return JavaFibo.fib(n); + } + + @Benchmark + public int groovy() { + return Fibo.fib(n); + } + + private static class JavaFibo { + static int fib(int n) { + if (n < 2) return 1; + return fib(n - 2) + fib(n - 1); + } + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/jmh/java/org/apache/groovy/bench/dispatch/CallsiteBench.java ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/jmh/java/org/apache/groovy/bench/dispatch/CallsiteBench.java b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/dispatch/CallsiteBench.java index c6b0b80..ae68c92 100644 --- a/subprojects/performance/src/jmh/java/org/apache/groovy/bench/dispatch/CallsiteBench.java +++ b/subprojects/performance/src/jmh/java/org/apache/groovy/bench/dispatch/CallsiteBench.java @@ -39,7 +39,7 @@ public class CallsiteBench { @Benchmark public void dispatch_1_monomorphic_java(MonomorphicState state, Blackhole bh) { - dispatch(state.receivers, bh); + JavaDispatch.dispatch(state.receivers, bh); } @Benchmark @@ -49,7 +49,7 @@ public class CallsiteBench { @Benchmark public void dispatch_3_polymorphic_java(PolymorphicState state, Blackhole bh) { - dispatch(state.receivers, bh); + JavaDispatch.dispatch(state.receivers, bh); } @Benchmark @@ -59,12 +59,14 @@ public class CallsiteBench { @Benchmark public void dispatch_8_megamorphic_java(MegamorphicState state, Blackhole bh) { - dispatch(state.receivers, bh); + JavaDispatch.dispatch(state.receivers, bh); } - private void dispatch(Object[] receivers, Blackhole bh) { - for (Object receiver : receivers) { - bh.consume(receiver.toString()); + private static class JavaDispatch { + static void dispatch(Object[] receivers, Blackhole bh) { + for (Object receiver : receivers) { + bh.consume(receiver.toString()); + } } } http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/jmh/java/org/apache/groovy/plugin/GroovyRunnerRegistryBench.java ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/jmh/java/org/apache/groovy/plugin/GroovyRunnerRegistryBench.java b/subprojects/performance/src/jmh/java/org/apache/groovy/plugin/GroovyRunnerRegistryBench.java index a419e54..606fa91 100644 --- a/subprojects/performance/src/jmh/java/org/apache/groovy/plugin/GroovyRunnerRegistryBench.java +++ b/subprojects/performance/src/jmh/java/org/apache/groovy/plugin/GroovyRunnerRegistryBench.java @@ -29,7 +29,7 @@ import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.Warmup; import org.openjdk.jmh.infra.Blackhole; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -41,7 +41,7 @@ import java.util.concurrent.TimeUnit; @State(Scope.Thread) public class GroovyRunnerRegistryBench { - static List<Object> control = new LinkedList<>(); + static List<Object> control = new ArrayList<>(); static GroovyRunnerRegistry registry = GroovyRunnerRegistry.getInstance(); static { control.add(new Object()); http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ackermann.groovy ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ackermann.groovy b/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ackermann.groovy new file mode 100644 index 0000000..61470ee --- /dev/null +++ b/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ackermann.groovy @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.bench + +class Ackermann { + + static int ack(int m, int n) { + if (m == 0) return n + 1 + if (n == 0) return ack(m - 1, 1) + return ack(m - 1, ack(m, n - 1)) + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ary.groovy ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ary.groovy b/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ary.groovy new file mode 100644 index 0000000..97c804f --- /dev/null +++ b/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Ary.groovy @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.bench + +class Ary { + + static int ary(int n) { + int[] x = new int[n] + int[] y = new int[n] + + for (int i = 0; i < n; i++) + x[i] = i + 1 + for (int k = 0; k < 1000; k++ ) + for (int j = n-1; j >= 0; j--) + y[j] += x[j] + + return y[0] + y[n-1] + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/d8705436/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Fibo.groovy ---------------------------------------------------------------------- diff --git a/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Fibo.groovy b/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Fibo.groovy new file mode 100644 index 0000000..4602599 --- /dev/null +++ b/subprojects/performance/src/test/groovy/org/apache/groovy/bench/Fibo.groovy @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.bench + +class Fibo { + + static int fib(int n) { + if (n < 2) return 1 + return fib(n - 2) + fib(n - 1) + } + +}
