-
Notifications
You must be signed in to change notification settings - Fork 3
/
ReactMicroBenchmark.java
103 lines (87 loc) · 3.31 KB
/
ReactMicroBenchmark.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import jdk.nashorn.api.scripting.NashornScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ReactMicroBenchmark {
static final int DEFAULT_NUM_RUNS = 10000;
static final int DEFAULT_NUM_COMMENTS = 10;
public static void main(String[] args) {
int NUM_RUNES = numOfRunes(args);
int NUM_COMMENTS = numOfComments(args);
NashornScriptEngine nashornScriptEngine = setupNashornScriptEngine();
for (int i = 0; i < NUM_RUNES; ++i) {
List<Comment> comments = generateComments(NUM_COMMENTS);
long start = System.nanoTime();
try {
Object html = nashornScriptEngine.invokeFunction("renderServer", comments);
String.valueOf(html);
} catch (Exception e) {
throw new IllegalStateException("failed to render react component", e);
}
long stop = System.nanoTime();
System.out.println("Run #" + (i + 1) + ": " + ((stop - start) / 1e6) + " ms");
}
}
private static Reader read(String path) {
InputStream in = ReactMicroBenchmark.class.getClassLoader().getResourceAsStream(path);
return new InputStreamReader(in);
}
private static int numOfComments(String[] args) {
int NUM_COMMENTS = DEFAULT_NUM_COMMENTS;
if (args.length == 2 && args[1] != null) {
try {
NUM_COMMENTS = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
}
}
return NUM_COMMENTS;
}
private static int numOfRunes(String[] args) {
int NUM_RUNES = DEFAULT_NUM_RUNS;
if (args.length >= 1 && args[0] != null) {
try {
NUM_RUNES = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
}
}
return NUM_RUNES;
}
private static List<Comment> generateComments(int NUM_COMMENTS) {
Random random = new Random();
List<Comment> comments = new ArrayList<>();
for (int j = 0; j < NUM_COMMENTS; ++j) {
comments.add(new Comment("Name " + random.nextInt(), "This is comment #" + random.nextInt() + "."));
}
return comments;
}
private static NashornScriptEngine setupNashornScriptEngine() throws RuntimeException {
NashornScriptEngine nashornScriptEngine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
try {
nashornScriptEngine.eval(read("js/nashorn-polyfill.js"));
nashornScriptEngine.eval(read("js/react.js"));
nashornScriptEngine.eval(read("js/commentBox.js"));
} catch (ScriptException e) {
throw new RuntimeException(e);
}
return nashornScriptEngine;
}
public static class Comment {
private String author;
private String text;
public Comment(String author, String text) {
this.author = author;
this.text = text;
}
public String getAuthor() {
return author;
}
public String getText() {
return text;
}
}
}