1 /* 2 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.bench.vm.lambda.chain; 24 import org.openjdk.jmh.annotations.Benchmark; 25 import org.openjdk.jmh.annotations.BenchmarkMode; 26 import org.openjdk.jmh.annotations.Mode; 27 import org.openjdk.jmh.annotations.OperationsPerInvocation; 28 import org.openjdk.jmh.annotations.OutputTimeUnit; 29 import org.openjdk.jmh.annotations.Scope; 30 import org.openjdk.jmh.annotations.Setup; 31 import org.openjdk.jmh.annotations.State; 32 import org.openjdk.jmh.infra.Blackhole; 33 34 import java.util.concurrent.TimeUnit; 35 36 /** 37 * Chain of (capture + invocation) microbenchmark. 38 */ 39 @State(Scope.Benchmark) 40 @BenchmarkMode(Mode.AverageTime) 41 @OutputTimeUnit(TimeUnit.NANOSECONDS) 42 public class ChainAnonymCap4 extends ChainBase { 43 44 public Level start1; 45 public Level start2; 46 public Level start4; 47 public Level start8; 48 49 @Setup 50 public void init() { 51 start1 = get1("capture me", "and me", "me too", "wait! what about me?"); 52 start2 = get2("capture me", "and me", "me too", "wait! what about me?"); 53 start4 = get4("capture me", "and me", "me too", "wait! what about me?"); 54 start8 = get8("capture me", "and me", "me too", "wait! what about me?"); 55 } 56 57 @Benchmark 58 @OperationsPerInvocation(1) 59 public void call1(Blackhole bh) { 60 process(bh, start1); 61 } 62 63 @Benchmark 64 @OperationsPerInvocation(2) 65 public void call2(Blackhole bh) { 66 process(bh, start2); 67 } 68 69 @Benchmark 70 @OperationsPerInvocation(4) 71 public void call4(Blackhole bh) { 72 process(bh, start4); 73 } 74 75 @Benchmark 76 @OperationsPerInvocation(8) 77 public void call8(Blackhole bh) { 78 process(bh, start8); 79 } 80 81 public static TopLevel get0(String v0, String v1, String v2, String v3) { 82 return new TopLevel() { 83 @Override 84 public String getImage() { 85 return "GOT: " + v0 + "," + v1 + "," + v2 + "," + v3; 86 } 87 }; 88 } 89 90 public static Level get1(String v0, String v1, String v2, String v3) { 91 return new Level() { 92 @Override 93 public Level up() { 94 return get0(v0, v1, v2, v3); 95 } 96 }; 97 } 98 99 public static Level get2(String v0, String v1, String v2, String v3) { 100 return new Level() { 101 @Override 102 public Level up() { 103 return get1(v0, v1, v2, v3); 104 } 105 }; 106 } 107 108 public static Level get3(String v0, String v1, String v2, String v3) { 109 return new Level() { 110 @Override 111 public Level up() { 112 return get2(v0, v1, v2, v3); 113 } 114 }; 115 } 116 117 public static Level get4(String v0, String v1, String v2, String v3) { 118 return new Level() { 119 @Override 120 public Level up() { 121 return get3(v0, v1, v2, v3); 122 } 123 }; 124 } 125 126 public static Level get5(String v0, String v1, String v2, String v3) { 127 return new Level() { 128 @Override 129 public Level up() { 130 return get4(v0, v1, v2, v3); 131 } 132 }; 133 } 134 135 public static Level get6(String v0, String v1, String v2, String v3) { 136 return new Level() { 137 @Override 138 public Level up() { 139 return get5(v0, v1, v2, v3); 140 } 141 }; 142 } 143 144 public static Level get7(String v0, String v1, String v2, String v3) { 145 return new Level() { 146 @Override 147 public Level up() { 148 return get6(v0, v1, v2, v3); 149 } 150 }; 151 } 152 153 public static Level get8(String v0, String v1, String v2, String v3) { 154 return new Level() { 155 @Override 156 public Level up() { 157 return get7(v0, v1, v2, v3); 158 } 159 }; 160 } 161 162 }