Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/benchmarks/CaliperObjectBenchmark.java

raw symbols SCIP
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 org.mozilla.javascript.Context;
8 import org.mozilla.javascript.Function;
9 import org.mozilla.javascript.Scriptable;
10 import org.mozilla.javascript.ScriptableObject;
11 import org.mozilla.javascript.tools.shell.Global;
12
13 import java.io.FileReader;
14 import java.io.IOException;
15 import java.util.Random;
16
17 @SuppressWarnings("unused")
18 public class CaliperObjectBenchmark
19 {
20 static final Random rand = new Random();
21
22 static void runCode(Context cx, Scriptable scope, String fileName)
23 throws IOException
24 {
25 FileReader rdr = new FileReader(fileName);
26 try {
27 cx.evaluateReader(scope, rdr, "test.js", 1, null);
28 } finally {
29 rdr.close();
30 }
31 }
32
33 @SuppressWarnings("unused")
34 public static class FieldAccess
35 {
36 @Param("10") int stringKeys;
37 @Param("10") int intKeys;
38 @Param("9") int optLevel;
39
40 private Context cx;
41 private Scriptable scope;
42
43 private Scriptable strings;
44 private Scriptable ints;
45
46 @BeforeExperiment
47 @SuppressWarnings("unused")
48 void create()
49 throws IOException
50 {
51 cx = Context.enter();
52 cx.setOptimizationLevel(optLevel);
53 cx.setLanguageVersion(Context.VERSION_ES6);
54
55 scope = new Global(cx);
56 runCode(cx, scope, "testsrc/benchmarks/caliper/fieldTests.js");
57
58 Object[] sarray = new Object[stringKeys];
59 for (int i = 0; i < stringKeys; i++) {
60 int len = rand.nextInt(49) + 1;
61 char[] c = new char[len];
62 for (int cc = 0; cc < len; cc++) {
63 c[cc] = (char) ('a' + rand.nextInt(25));
64 }
65 sarray[i] = new String(c);
66 }
67 strings = cx.newArray(scope, sarray);
68
69 Object[] iarray = new Object[intKeys];
70 for (int i = 0; i < intKeys; i++) {
71 iarray[i] = rand.nextInt(10000);
72 }
73 ints = cx.newArray(scope, iarray);
74 }
75
76 @AfterExperiment
77 @SuppressWarnings("unused")
78 void close()
79 {
80 Context.exit();
81 }
82
83 @Benchmark
84 @SuppressWarnings("unused")
85 void createFields(int count)
86 {
87 Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
88 create.call(cx, scope, null, new Object[]{count, strings, ints});
89 }
90
91 @Benchmark
92 @SuppressWarnings("unused")
93 void accessFields(int count)
94 {
95 Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
96 Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
97 Function access = (Function)ScriptableObject.getProperty(scope, "accessObject");
98 access.call(cx, scope, null, new Object[] {count, o, strings, ints});
99 }
100
101 @Benchmark
102 @SuppressWarnings("unused")
103 void iterateFields(long count)
104 {
105 Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
106 Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
107 Function iterate = (Function)ScriptableObject.getProperty(scope, "iterateObject");
108 iterate.call(cx, scope, null, new Object[] {count, o});
109 }
110
111 @Benchmark
112 @SuppressWarnings("unused")
113 void ownKeysFields(long count)
114 {
115 Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
116 Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
117 Function iterate = (Function)ScriptableObject.getProperty(scope, "iterateOwnKeysObject");
118 iterate.call(cx, scope, null, new Object[] {count, o});
119 }
120
121 @Benchmark
122 @SuppressWarnings("unused")
123 void deleteFields(int count)
124 {
125 Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
126 Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
127 Function delete = (Function)ScriptableObject.getProperty(scope, "deleteObject");
128 delete.call(cx, scope, null, new Object[] {count, o, strings, ints});
129 }
130 }
131 }