Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.interop.java.test/src/com/oracle/truffle/api/interop/java/test/JavaInteropSpeedTest.java @ 22135:e70b20f4bb00
Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Mon, 07 Sep 2015 17:07:20 +0200 |
parents | |
children | 291574f3e498 |
comparison
equal
deleted
inserted
replaced
22134:025869c88840 | 22135:e70b20f4bb00 |
---|---|
1 /* | |
2 * Copyright (c) 2015, 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. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.interop.java.test; | |
26 | |
27 import com.oracle.truffle.api.interop.TruffleObject; | |
28 import com.oracle.truffle.api.interop.java.JavaInterop; | |
29 import java.util.Random; | |
30 import org.junit.AfterClass; | |
31 import org.junit.BeforeClass; | |
32 import org.junit.Test; | |
33 import static org.junit.Assert.assertEquals; | |
34 import static org.junit.Assert.fail; | |
35 | |
36 public class JavaInteropSpeedTest { | |
37 private static final int REPEAT = 10000; | |
38 private static int[] arr; | |
39 private static long javaTime; | |
40 private static long interopTime; | |
41 | |
42 @BeforeClass | |
43 public static void beforeTesting() { | |
44 arr = initArray(REPEAT); | |
45 for (int i = 0; i < 1000; i++) { | |
46 JavaInteropSpeedTest t = new JavaInteropSpeedTest(); | |
47 t.doMinMaxInJava(); | |
48 t.doMinMaxWithInterOp(); | |
49 t.assertSame(); | |
50 } | |
51 } | |
52 | |
53 private int mmInOp; | |
54 private int mmInJava; | |
55 | |
56 @Test | |
57 public void doMinMaxInJava() { | |
58 int max = 0; | |
59 long now = System.currentTimeMillis(); | |
60 for (int i = 0; i < arr.length; i++) { | |
61 max = Math.max(arr[i], max); | |
62 } | |
63 javaTime = System.currentTimeMillis() - now; | |
64 mmInJava = max; | |
65 } | |
66 | |
67 private void assertSame() { | |
68 assertEquals(mmInJava, mmInOp); | |
69 } | |
70 | |
71 private static final TruffleObject TRUFFLE_MAX = JavaInterop.asTruffleFunction(IntBinaryOperation.class, new IntBinaryOperation() { | |
72 @Override | |
73 public int compute(int a, int b) { | |
74 return Math.max(a, b); | |
75 } | |
76 }); | |
77 private static final IntBinaryOperation MAX = JavaInterop.asJavaFunction(IntBinaryOperation.class, TRUFFLE_MAX); | |
78 | |
79 @Test | |
80 public void doMinMaxWithInterOp() { | |
81 int max = 0; | |
82 long now = System.currentTimeMillis(); | |
83 for (int i = 0; i < arr.length; i++) { | |
84 max = MAX.compute(arr[i], max); | |
85 } | |
86 interopTime = System.currentTimeMillis() - now; | |
87 mmInOp = max; | |
88 } | |
89 | |
90 @AfterClass | |
91 public static void nonSignificanDifference() { | |
92 if (javaTime < 1) { | |
93 javaTime = 1; | |
94 } | |
95 if (interopTime > 5 * javaTime) { | |
96 fail("Interop took too long: " + interopTime + " ms, while java only " + javaTime + " ms"); | |
97 } | |
98 } | |
99 | |
100 private static int[] initArray(int size) { | |
101 Random r = new Random(); | |
102 int[] tmp = new int[size]; | |
103 for (int i = 0; i < tmp.length; i++) { | |
104 tmp[i] = r.nextInt(100000); | |
105 } | |
106 return tmp; | |
107 } | |
108 } |