OSDN Git Service

Add benchmark for jobject functions
authorMathieu Chartier <mathieuc@google.com>
Wed, 23 Sep 2015 01:14:29 +0000 (18:14 -0700)
committerMathieu Chartier <mathieuc@google.com>
Wed, 23 Sep 2015 01:20:10 +0000 (18:20 -0700)
Measures performance of:
Add/RemoveLocalRef
Add/RemoveGlobalRef
Add/RemoveWeakGlobalRef
Decoding local, weak, global, handle scope jobjects.

N5 results:
benchmark    ns linear runtime
AddRemoveGlobal 476.7 ===========================
AddRemoveLocal  42.0 ==
AddRemoveWeakGlobal 519.7 ==============================
DecodeGlobal  69.9 ====
DecodeHandleScopeRef  38.9 ==
DecodeLocal  70.7 ====
DecodeWeakGlobal 119.7 ======

Change-Id: Iba014a9993909ee45fd52ae48d6dea8428a86bf2

benchmark/Android.mk
benchmark/jobject-benchmark/info.txt [new file with mode: 0644]
benchmark/jobject-benchmark/jobject_benchmark.cc [new file with mode: 0644]
benchmark/jobject-benchmark/src/JObjectBenchmark.java [new file with mode: 0644]

index 09aca98..a4a603a 100644 (file)
@@ -19,6 +19,7 @@ LOCAL_PATH := $(call my-dir)
 include art/build/Android.common_build.mk
 
 LIBARTBENCHMARK_COMMON_SRC_FILES := \
+  jobject-benchmark/jobject_benchmark.cc \
   jni-perf/perf_jni.cc \
   scoped-primitive-array/scoped_primitive_array.cc
 
diff --git a/benchmark/jobject-benchmark/info.txt b/benchmark/jobject-benchmark/info.txt
new file mode 100644 (file)
index 0000000..f2a256a
--- /dev/null
@@ -0,0 +1,7 @@
+Benchmark for jobject functions
+
+Measures performance of:
+Add/RemoveLocalRef
+Add/RemoveGlobalRef
+Add/RemoveWeakGlobalRef
+Decoding local, weak, global, handle scope jobjects.
diff --git a/benchmark/jobject-benchmark/jobject_benchmark.cc b/benchmark/jobject-benchmark/jobject_benchmark.cc
new file mode 100644 (file)
index 0000000..e7ca9eb
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "jni.h"
+
+#include "mirror/class-inl.h"
+#include "scoped_thread_state_change.h"
+
+namespace art {
+namespace {
+
+extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeAddRemoveLocal(
+    JNIEnv* env, jobject jobj, jint reps) {
+  ScopedObjectAccess soa(env);
+  mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
+  CHECK(obj != nullptr);
+  for (jint i = 0; i < reps; ++i) {
+    jobject ref = soa.Env()->AddLocalReference<jobject>(obj);
+    soa.Env()->DeleteLocalRef(ref);
+  }
+}
+
+extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeLocal(
+    JNIEnv* env, jobject jobj, jint reps) {
+  ScopedObjectAccess soa(env);
+  mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
+  CHECK(obj != nullptr);
+  jobject ref = soa.Env()->AddLocalReference<jobject>(obj);
+  for (jint i = 0; i < reps; ++i) {
+    CHECK_EQ(soa.Decode<mirror::Object*>(ref), obj);
+  }
+  soa.Env()->DeleteLocalRef(ref);
+}
+
+extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeAddRemoveGlobal(
+    JNIEnv* env, jobject jobj, jint reps) {
+  ScopedObjectAccess soa(env);
+  mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
+  CHECK(obj != nullptr);
+  for (jint i = 0; i < reps; ++i) {
+    jobject ref = soa.Vm()->AddGlobalRef(soa.Self(), obj);
+    soa.Vm()->DeleteGlobalRef(soa.Self(), ref);
+  }
+}
+
+extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeGlobal(
+    JNIEnv* env, jobject jobj, jint reps) {
+  ScopedObjectAccess soa(env);
+  mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
+  CHECK(obj != nullptr);
+  jobject ref = soa.Vm()->AddGlobalRef(soa.Self(), obj);
+  for (jint i = 0; i < reps; ++i) {
+    CHECK_EQ(soa.Decode<mirror::Object*>(ref), obj);
+  }
+  soa.Vm()->DeleteGlobalRef(soa.Self(), ref);
+}
+
+extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeAddRemoveWeakGlobal(
+    JNIEnv* env, jobject jobj, jint reps) {
+  ScopedObjectAccess soa(env);
+  mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
+  CHECK(obj != nullptr);
+  for (jint i = 0; i < reps; ++i) {
+    jobject ref = soa.Vm()->AddWeakGlobalRef(soa.Self(), obj);
+    soa.Vm()->DeleteWeakGlobalRef(soa.Self(), ref);
+  }
+}
+
+extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeWeakGlobal(
+    JNIEnv* env, jobject jobj, jint reps) {
+  ScopedObjectAccess soa(env);
+  mirror::Object* obj = soa.Decode<mirror::Object*>(jobj);
+  CHECK(obj != nullptr);
+  jobject ref = soa.Vm()->AddWeakGlobalRef(soa.Self(), obj);
+  for (jint i = 0; i < reps; ++i) {
+    CHECK_EQ(soa.Decode<mirror::Object*>(ref), obj);
+  }
+  soa.Vm()->DeleteWeakGlobalRef(soa.Self(), ref);
+}
+
+extern "C" JNIEXPORT void JNICALL Java_JObjectBenchmark_timeDecodeHandleScopeRef(
+    JNIEnv* env, jobject jobj, jint reps) {
+  ScopedObjectAccess soa(env);
+  for (jint i = 0; i < reps; ++i) {
+    soa.Decode<mirror::Object*>(jobj);
+  }
+}
+
+}  // namespace
+}  // namespace art
diff --git a/benchmark/jobject-benchmark/src/JObjectBenchmark.java b/benchmark/jobject-benchmark/src/JObjectBenchmark.java
new file mode 100644 (file)
index 0000000..f4c059c
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import com.google.caliper.SimpleBenchmark;
+
+public class JObjectBenchmark extends SimpleBenchmark {
+  public JObjectBenchmark() {
+    // Make sure to link methods before benchmark starts.
+    System.loadLibrary("artbenchmark");
+    timeAddRemoveLocal(1);
+    timeDecodeLocal(1);
+    timeAddRemoveGlobal(1);
+    timeDecodeGlobal(1);
+    timeAddRemoveWeakGlobal(1);
+    timeDecodeWeakGlobal(1);
+    timeDecodeHandleScopeRef(1);
+  }
+
+  public native void timeAddRemoveLocal(int reps);
+  public native void timeDecodeLocal(int reps);
+  public native void timeAddRemoveGlobal(int reps);
+  public native void timeDecodeGlobal(int reps);
+  public native void timeAddRemoveWeakGlobal(int reps);
+  public native void timeDecodeWeakGlobal(int reps);
+  public native void timeDecodeHandleScopeRef(int reps);
+}