Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(943)

Side by Side Diff: base/android/jni_android_unittest.cc

Issue 11038015: Android: lazy initialization for method id. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removes EXCEPTIONCHECK. Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/android/jni_android.h" 5 #include "base/android/jni_android.h"
6 6
7 #include "base/at_exit.h" 7 #include "base/at_exit.h"
8 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
10 namespace base { 11 namespace base {
11 namespace android { 12 namespace android {
12 13
13 namespace { 14 namespace {
14 15
15 const char kJavaLangObject[] = "java/lang/Object"; 16 const char kJavaLangObject[] = "java/lang/Object";
16 const char kGetClass[] = "getClass"; 17 const char kGetClass[] = "getClass";
17 const char kToString[] = "toString"; 18 const char kToString[] = "toString";
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 EXPECT_EQ(id1, id2); 84 EXPECT_EQ(id1, id2);
84 85
85 Reset(); 86 Reset();
86 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString, 87 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString,
87 kReturningJavaLangString); 88 kReturningJavaLangString);
88 EXPECT_STREQ(kToString, g_last_method); 89 EXPECT_STREQ(kToString, g_last_method);
89 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature); 90 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature);
90 EXPECT_EQ(g_last_method_id, id3); 91 EXPECT_EQ(g_last_method_id, id3);
91 } 92 }
92 93
94 namespace {
95
96 base::subtle::AtomicWord g_atomic_id = 0;
97 int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) {
98 jmethodID id = base::android::MethodID::LazyGet<
99 base::android::MethodID::METHODTYPE_STATIC>(
100 env, clazz,
101 "abs",
102 "(I)I",
103 &g_atomic_id);
104
105 return env->CallStaticIntMethod(clazz, id, p);
106 }
107
108 int MethodIDCall(JNIEnv* env, jclass clazz, jmethodID id, int p) {
109 return env->CallStaticIntMethod(clazz, id, p);
110 }
111
112 } // namespace
113
114 TEST(JNIAndroidMicrobenchmark, MethodId) {
115 JNIEnv* env = AttachCurrentThread();
116 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/Math"));
117 base::Time start_lazy = base::Time::Now();
118 int o = 0;
119 for (int i = 0; i < 1024; ++i)
120 o += LazyMethodIDCall(env, clazz.obj(), i);
121 base::Time end_lazy = base::Time::Now();
122
123 jmethodID id = reinterpret_cast<jmethodID>(g_atomic_id);
124 base::Time start = base::Time::Now();
125 for (int i = 0; i < 1024; ++i)
126 o += MethodIDCall(env, clazz.obj(), id, i);
127 base::Time end = base::Time::Now();
128
129 // On a Galaxy Nexus, results were in the range of:
130 // JNI LazyMethodIDCall (us) 1984
131 // JNI MethodIDCall (us) 1861
132 LOG(ERROR) << "JNI LazyMethodIDCall (us) " <<
133 base::TimeDelta(end_lazy - start_lazy).InMicroseconds();
134 LOG(ERROR) << "JNI MethodIDCall (us) " <<
135 base::TimeDelta(end - start).InMicroseconds();
136 LOG(ERROR) << "JNI " << o;
137 }
138
139
93 } // namespace android 140 } // namespace android
94 } // namespace base 141 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698