| 1 | package org.mozilla.javascript.benchmarks; |
| 2 | |
| 3 | import com.google.caliper.AfterExperiment; |
| 4 | import com.google.caliper.BeforeExperiment; |
| 5 | import com.google.caliper.Benchmark; |
| 6 | import com.google.caliper.Param; |
| 7 | import com.google.caliper.api.Macrobenchmark; |
| 8 | import org.mozilla.javascript.Context; |
| 9 | import org.mozilla.javascript.Script; |
| 10 | import org.mozilla.javascript.Scriptable; |
| 11 | import org.mozilla.javascript.tools.shell.Global; |
| 12 | |
| 13 | import java.io.File; |
| 14 | import java.io.FileReader; |
| 15 | import java.io.IOException; |
| 16 | import java.util.HashMap; |
| 17 | |
| 18 | @SuppressWarnings("unused") |
| 19 | public class CaliperSpiderBenchmark |
| 20 | { |
| 21 | public static final File FILE_BASE = new File("testsrc/benchmarks/sunspider-0.9.1"); |
| 22 | |
| 23 | static Script compileBenchmark(Context cx, String fileName) |
| 24 | throws IOException |
| 25 | { |
| 26 | File f = new File(FILE_BASE, fileName); |
| 27 | FileReader rdr = new FileReader(f); |
| 28 | try { |
| 29 | return cx.compileReader(rdr, fileName, 1, null); |
| 30 | } finally { |
| 31 | rdr.close(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | @SuppressWarnings("unused") |
| 36 | public static class Spider |
| 37 | { |
| 38 | @Param("9") int optLevel; |
| 39 | |
| 40 | private Context cx; |
| 41 | private Scriptable scope; |
| 42 | |
| 43 | private final HashMap<String, Script> scripts = new HashMap<String, Script>(); |
| 44 | |
| 45 | private static final String[] BENCHMARKS = { |
| 46 | "3d-cube.js", "3d-morph.js", "3d-raytrace.js", |
| 47 | "access-binary-trees.js", "access-fannkuch.js", "access-nbody.js", "access-nsieve.js", |
| 48 | "bitops-3bit-bits-in-byte.js", "bitops-bits-in-byte.js", "bitops-bitwise-and.js", "bitops-nsieve-bits.js", |
| 49 | "controlflow-recursive.js", "crypto-aes.js", "crypto-md5.js", "crypto-sha1.js", |
| 50 | "date-format-tofte.js", "date-format-xparb.js", |
| 51 | "math-cordic.js", "math-partial-sums.js", "math-spectral-norm.js", |
| 52 | "regexp-dna.js", |
| 53 | "string-base64.js", "string-fasta.js", "string-tagcloud.js", |
| 54 | "string-unpack-code.js", "string-validate-input.js" |
| 55 | }; |
| 56 | |
| 57 | @BeforeExperiment |
| 58 | @SuppressWarnings("unused") |
| 59 | void create() |
| 60 | throws IOException |
| 61 | { |
| 62 | cx = Context.enter(); |
| 63 | cx.setOptimizationLevel(optLevel); |
| 64 | cx.setLanguageVersion(Context.VERSION_ES6); |
| 65 | scope = new Global(cx); |
| 66 | |
| 67 | for (String bn : BENCHMARKS) { |
| 68 | compileScript(cx, bn); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private void compileScript(Context cx, String fileName) |
| 73 | throws IOException |
| 74 | { |
| 75 | Script s = compileBenchmark(cx, fileName); |
| 76 | scripts.put(fileName, s); |
| 77 | } |
| 78 | |
| 79 | @AfterExperiment |
| 80 | @SuppressWarnings("unused") |
| 81 | void close() |
| 82 | { |
| 83 | Context.exit(); |
| 84 | } |
| 85 | |
| 86 | @Macrobenchmark |
| 87 | @SuppressWarnings("unused") |
| 88 | public void threeDCube(int iterations) |
| 89 | { |
| 90 | runBenchmark("3d-cube.js", iterations); |
| 91 | } |
| 92 | |
| 93 | @Macrobenchmark |
| 94 | @SuppressWarnings("unused") |
| 95 | public void threeDMorph(int iterations) |
| 96 | { |
| 97 | runBenchmark("3d-morph.js", iterations); |
| 98 | } |
| 99 | |
| 100 | @Macrobenchmark |
| 101 | @SuppressWarnings("unused") |
| 102 | public void accessBinaryTrees(int iterations) |
| 103 | { |
| 104 | runBenchmark("access-binary-trees.js", iterations); |
| 105 | } |
| 106 | |
| 107 | @Macrobenchmark |
| 108 | @SuppressWarnings("unused") |
| 109 | public void accessFannkuch(int iterations) |
| 110 | { |
| 111 | runBenchmark("access-fannkuch.js", iterations); |
| 112 | } |
| 113 | |
| 114 | @Macrobenchmark |
| 115 | @SuppressWarnings("unused") |
| 116 | public void accessNBody(int iterations) |
| 117 | { |
| 118 | runBenchmark("access-nbody.js", iterations); |
| 119 | } |
| 120 | |
| 121 | @Macrobenchmark |
| 122 | @SuppressWarnings("unused") |
| 123 | public void accessNSieve(int iterations) |
| 124 | { |
| 125 | runBenchmark("access-nsieve.js", iterations); |
| 126 | } |
| 127 | |
| 128 | @Macrobenchmark |
| 129 | @SuppressWarnings("unused") |
| 130 | public void bitops3BitBitsInByte(int iterations) |
| 131 | { |
| 132 | runBenchmark("bitops-3bit-bits-in-byte.js", iterations); |
| 133 | } |
| 134 | |
| 135 | @Macrobenchmark |
| 136 | @SuppressWarnings("unused") |
| 137 | public void bitopsBitsInByte(int iterations) |
| 138 | { |
| 139 | runBenchmark("bitops-bits-in-byte.js", iterations); |
| 140 | } |
| 141 | |
| 142 | @Macrobenchmark |
| 143 | @SuppressWarnings("unused") |
| 144 | public void BitopsBitwiseAnd(int iterations) |
| 145 | { |
| 146 | runBenchmark("bitops-bitwise-and.js", iterations); |
| 147 | } |
| 148 | |
| 149 | @Macrobenchmark |
| 150 | @SuppressWarnings("unused") |
| 151 | public void bisopsNsieveBits(int iterations) |
| 152 | { |
| 153 | runBenchmark("bitops-nsieve-bits.js", iterations); |
| 154 | } |
| 155 | |
| 156 | @Macrobenchmark |
| 157 | @SuppressWarnings("unused") |
| 158 | public void controlFlowRecursive(int iterations) |
| 159 | { |
| 160 | runBenchmark("controlflow-recursive.js", iterations); |
| 161 | } |
| 162 | |
| 163 | @Macrobenchmark |
| 164 | @SuppressWarnings("unused") |
| 165 | public void cryptoAES(int iterations) |
| 166 | { |
| 167 | runBenchmark("crypto-aes.js", iterations); |
| 168 | } |
| 169 | |
| 170 | @Macrobenchmark |
| 171 | @SuppressWarnings("unused") |
| 172 | public void cryptoMD5(int iterations) |
| 173 | { |
| 174 | runBenchmark("crypto-md5.js", iterations); |
| 175 | } |
| 176 | |
| 177 | @Macrobenchmark |
| 178 | @SuppressWarnings("unused") |
| 179 | public void cryptoSHA1(int iterations) |
| 180 | { |
| 181 | runBenchmark("crypto-sha1.js", iterations); |
| 182 | } |
| 183 | |
| 184 | @Macrobenchmark |
| 185 | @SuppressWarnings("unused") |
| 186 | public void dateFormatToFTE(int iterations) |
| 187 | { |
| 188 | runBenchmark("date-format-tofte.js", iterations); |
| 189 | } |
| 190 | |
| 191 | @Macrobenchmark |
| 192 | @SuppressWarnings("unused") |
| 193 | public void dateFormatXparb(int iterations) |
| 194 | { |
| 195 | runBenchmark("date-format-xparb.js", iterations); |
| 196 | } |
| 197 | |
| 198 | @Macrobenchmark |
| 199 | @SuppressWarnings("unused") |
| 200 | public void mathCordic(int iterations) |
| 201 | { |
| 202 | runBenchmark("math-cordic.js", iterations); |
| 203 | } |
| 204 | |
| 205 | @Macrobenchmark |
| 206 | @SuppressWarnings("unused") |
| 207 | public void mathPartialSums(int iterations) |
| 208 | { |
| 209 | runBenchmark("math-partial-sums.js", iterations); |
| 210 | } |
| 211 | |
| 212 | @Macrobenchmark |
| 213 | @SuppressWarnings("unused") |
| 214 | public void mathSpectralNorm(int iterations) |
| 215 | { |
| 216 | runBenchmark("math-spectral-norm.js", iterations); |
| 217 | } |
| 218 | |
| 219 | @Macrobenchmark |
| 220 | @SuppressWarnings("unused") |
| 221 | public void regexpDNA(int iterations) |
| 222 | { |
| 223 | runBenchmark("regexp-dna.js", iterations); |
| 224 | } |
| 225 | |
| 226 | @Macrobenchmark |
| 227 | @SuppressWarnings("unused") |
| 228 | public void stringBase64(int iterations) |
| 229 | { |
| 230 | runBenchmark("string-base64.js", iterations); |
| 231 | } |
| 232 | |
| 233 | @Macrobenchmark |
| 234 | @SuppressWarnings("unused") |
| 235 | public void stringFasta(int iterations) |
| 236 | { |
| 237 | runBenchmark("string-fasta.js", iterations); |
| 238 | } |
| 239 | |
| 240 | @Macrobenchmark |
| 241 | @SuppressWarnings("unused") |
| 242 | public void stringTagCloud(int iterations) |
| 243 | { |
| 244 | runBenchmark("string-tagcloud.js", iterations); |
| 245 | } |
| 246 | |
| 247 | @Macrobenchmark |
| 248 | @SuppressWarnings("unused") |
| 249 | public void stringUnpackCode(int iterations) |
| 250 | { |
| 251 | runBenchmark("string-unpack-code.js", iterations); |
| 252 | } |
| 253 | |
| 254 | @Macrobenchmark |
| 255 | @SuppressWarnings("unused") |
| 256 | public void stringValidateInput(int iterations) |
| 257 | { |
| 258 | runBenchmark("string-validate-input.js", iterations); |
| 259 | } |
| 260 | |
| 261 | private void runBenchmark(String name, int iterations) |
| 262 | { |
| 263 | Script s = scripts.get(name); |
| 264 | for (int i = 0; i < iterations; i++) { |
| 265 | s.exec(cx, scope); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |