OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 import dartino.*; | |
6 | |
7 import java.io.FileInputStream; | |
8 import java.io.FileNotFoundException; | |
9 import java.io.IOException; | |
10 | |
11 import java.util.List; | |
12 | |
13 class PerformanceTest { | |
14 static final int CALL_COUNT = 10000; | |
15 static final int TREE_DEPTH = 7; | |
16 | |
17 public static void main(String args[]) { | |
18 // Expecting a snapshot of the dart service code on the command line. | |
19 if (args.length != 1) { | |
20 System.out.println("Usage: java PerformanceTest <snapshot>"); | |
21 System.exit(1); | |
22 } | |
23 | |
24 // Load libdartino.so. | |
25 System.loadLibrary("dartino"); | |
26 | |
27 // Setup Dartino. | |
28 DartinoApi.Setup(); | |
29 DartinoServiceApi.Setup(); | |
30 DartinoApi.AddDefaultSharedLibrary("libdartino.so"); | |
31 | |
32 try { | |
33 // Load snapshot and start dart code on a separate thread. | |
34 FileInputStream snapshotStream = new FileInputStream(args[0]); | |
35 int available = snapshotStream.available(); | |
36 byte[] snapshot = new byte[available]; | |
37 snapshotStream.read(snapshot); | |
38 Thread dartThread = new Thread(new SnapshotRunner(snapshot)); | |
39 dartThread.start(); | |
40 } catch (FileNotFoundException e) { | |
41 System.err.println("Failed loading snapshot"); | |
42 System.exit(1); | |
43 } catch (IOException e) { | |
44 System.err.println("Failed loading snapshot"); | |
45 System.exit(1); | |
46 } | |
47 | |
48 // Run performance tests. | |
49 PerformanceService.Setup(); | |
50 try { | |
51 runEcho(); | |
52 runAsyncEcho(); | |
53 runTreeTests(); | |
54 } finally { | |
55 PerformanceService.TearDown(); | |
56 } | |
57 } | |
58 | |
59 private static void runEcho() { | |
60 long start = System.currentTimeMillis(); | |
61 for (int i = 0; i < CALL_COUNT; i++) { | |
62 int result = PerformanceService.echo(i); | |
63 if (i != result) throw new RuntimeException("Wrong result"); | |
64 } | |
65 long end = System.currentTimeMillis(); | |
66 double us = (end - start) * 1000.0 / CALL_COUNT; | |
67 System.out.println("Sync call took " + us + " us."); | |
68 } | |
69 | |
70 private static void runAsyncEcho() { | |
71 final Object monitor = new Object(); | |
72 final PerformanceService.EchoCallback[] callback = | |
73 new PerformanceService.EchoCallback[1]; | |
74 | |
75 final long start = System.currentTimeMillis(); | |
76 callback[0] = new PerformanceService.EchoCallback() { | |
77 public void handle(int i) { | |
78 if (i < CALL_COUNT) { | |
79 PerformanceService.echoAsync(i + 1, callback[0]); | |
80 } else { | |
81 synchronized (monitor) { | |
82 monitor.notify(); | |
83 } | |
84 } | |
85 } | |
86 }; | |
87 | |
88 synchronized (monitor) { | |
89 boolean done = false; | |
90 PerformanceService.echoAsync(0, callback[0]); | |
91 while (!done) { | |
92 try { | |
93 monitor.wait(); | |
94 done = true; | |
95 } catch (InterruptedException e) { | |
96 // Ignored. | |
97 } | |
98 } | |
99 } | |
100 | |
101 long end = System.currentTimeMillis(); | |
102 double us = (end - start) * 1000.0 / CALL_COUNT; | |
103 System.out.println("Async call took " + us + " us."); | |
104 } | |
105 | |
106 private static int countTreeNodes(TreeNode node) { | |
107 int sum = 1; | |
108 TreeNodeList children = node.getChildren(); | |
109 for (int i = 0; i < children.size(); i++) { | |
110 sum += countTreeNodes(children.get(i)); | |
111 } | |
112 return sum; | |
113 } | |
114 | |
115 private static void buildTree(int n, TreeNodeBuilder node) { | |
116 if (n > 1) { | |
117 TreeNodeListBuilder children = node.initChildren(2); | |
118 buildTree(n - 1, children.get(0)); | |
119 buildTree(n - 1, children.get(1)); | |
120 } | |
121 } | |
122 | |
123 private static void runTreeTests() { | |
124 | |
125 long start = System.currentTimeMillis(); | |
126 TreeNodeBuilder built = null; | |
127 for (int i = 0; i < CALL_COUNT; i++) { | |
128 MessageBuilder builder = new MessageBuilder(8192); | |
129 built = new TreeNodeBuilder(); | |
130 builder.initRoot(built, TreeNodeBuilder.kSize); | |
131 buildTree(TREE_DEPTH, built); | |
132 } | |
133 long end = System.currentTimeMillis(); | |
134 double us = (end - start) * 1000.0 / CALL_COUNT; | |
135 System.out.println("Building (Java) took " + us + " us."); | |
136 | |
137 start = System.currentTimeMillis(); | |
138 for (int i = 0; i < CALL_COUNT; i++) { | |
139 PerformanceService.countTreeNodes(built); | |
140 } | |
141 end = System.currentTimeMillis(); | |
142 us = (end - start) * 1000.0 / CALL_COUNT; | |
143 System.out.println("Counting (Dart) took " + us + " us."); | |
144 | |
145 start = System.currentTimeMillis(); | |
146 for (int i = 0; i < CALL_COUNT; i++) { | |
147 TreeNode generated = PerformanceService.buildTree(TREE_DEPTH); | |
148 } | |
149 end = System.currentTimeMillis(); | |
150 us = (end - start) * 1000.0 / CALL_COUNT; | |
151 System.out.println("Building (Dart) took " + us + " us."); | |
152 | |
153 TreeNode generated = PerformanceService.buildTree(TREE_DEPTH); | |
154 | |
155 start = System.currentTimeMillis(); | |
156 for (int i = 0; i < CALL_COUNT; i++) { | |
157 countTreeNodes(generated); | |
158 } | |
159 end = System.currentTimeMillis(); | |
160 us = (end - start) * 1000.0 / CALL_COUNT; | |
161 System.out.println("Counting (Java) took " + us + " us."); | |
162 } | |
163 } | |
OLD | NEW |