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

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: Tidy up + tests 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 "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace base { 10 namespace base {
(...skipping 15 matching lines...) Expand all
26 26
27 jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method, 27 jmethodID GetMethodIDWrapper(JNIEnv* env, jclass clazz, const char* method,
28 const char* jni_signature) { 28 const char* jni_signature) {
29 g_last_method = method; 29 g_last_method = method;
30 g_last_jni_signature = jni_signature; 30 g_last_jni_signature = jni_signature;
31 g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method, 31 g_last_method_id = g_previous_functions->GetMethodID(env, clazz, method,
32 jni_signature); 32 jni_signature);
33 return g_last_method_id; 33 return g_last_method_id;
34 } 34 }
35 35
36
joth 2012/10/02 17:47:43 \n nit
bulach 2012/10/03 13:09:00 Done.
37 static jmethodID g_method_id = 0;
joth 2012/10/02 17:47:43 this should be declared as an AtomicWord nit: sta
bulach 2012/10/03 13:09:00 Done.
38 int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) {
39 base::android::LazyMethodID::Get<
40 base::android::LazyMethodID::METHODTYPE_STATIC,
41 base::android::LazyMethodID::EXCEPTIONCHECK_NO>(
42 env, clazz,
43 "abs",
44 "(I)I",
45 &g_method_id);
joth 2012/10/02 17:47:43 are you specifically using a global method id rath
bulach 2012/10/03 13:09:00 yes. I'm trying to reproduce as much as possible t
46
47 return env->CallStaticIntMethod(clazz, g_method_id, p);
48 }
49
50 int MethodIDCall(JNIEnv* env, jclass clazz, int p) {
51 return env->CallStaticIntMethod(clazz, g_method_id, p);
52 }
joth 2012/10/02 17:47:43 AIUI it's preferred to put single-use anon methods
bulach 2012/10/03 13:09:00 moved to its own file..
53
36 } // namespace 54 } // namespace
37 55
38 class JNIAndroidTest : public testing::Test { 56 class JNIAndroidTest : public testing::Test {
39 protected: 57 protected:
40 virtual void SetUp() { 58 virtual void SetUp() {
41 JNIEnv* env = AttachCurrentThread(); 59 JNIEnv* env = AttachCurrentThread();
42 g_previous_functions = env->functions; 60 g_previous_functions = env->functions;
43 hooked_functions = *g_previous_functions; 61 hooked_functions = *g_previous_functions;
44 env->functions = &hooked_functions; 62 env->functions = &hooked_functions;
45 hooked_functions.GetMethodID = &GetMethodIDWrapper; 63 hooked_functions.GetMethodID = &GetMethodIDWrapper;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 EXPECT_EQ(id1, id2); 101 EXPECT_EQ(id1, id2);
84 102
85 Reset(); 103 Reset();
86 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString, 104 jmethodID id3 = GetMethodIDFromClassName(env, kJavaLangObject, kToString,
87 kReturningJavaLangString); 105 kReturningJavaLangString);
88 EXPECT_STREQ(kToString, g_last_method); 106 EXPECT_STREQ(kToString, g_last_method);
89 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature); 107 EXPECT_STREQ(kReturningJavaLangString, g_last_jni_signature);
90 EXPECT_EQ(g_last_method_id, id3); 108 EXPECT_EQ(g_last_method_id, id3);
91 } 109 }
92 110
111 TEST(JNIAndroidMicrobenchmark, MethodId) {
joth 2012/10/02 17:47:43 in net/ they put micro benchmarks into a different
bulach 2012/10/03 13:09:00 yeah, the extra target doesn't seem to be worth th
112 JNIEnv* env = AttachCurrentThread();
113 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/Math"));
114 base::Time start_lazy = base::Time::Now();
115 int o = 0;
116 for (int i = 0; i < 1024; ++i)
117 o += LazyMethodIDCall(env, clazz.obj(), i);
118 base::Time end_lazy = base::Time::Now();
119
120 base::Time start = base::Time::Now();
121 for (int i = 0; i < 1024; ++i)
122 o += MethodIDCall(env, clazz.obj(), i);
123 base::Time end = base::Time::Now();
124
125 LOG(ERROR) << "JNI LazyMethodIDCall (us) " <<
126 base::TimeDelta(end_lazy - start_lazy).InMicroseconds();
127 LOG(ERROR) << "JNI MethodIDCall (us) " <<
128 base::TimeDelta(end - start).InMicroseconds();
129 LOG(ERROR) << "JNI " << o;
joth 2012/10/02 17:47:43 can you include in comment here as to typical resu
bulach 2012/10/03 13:09:00 good point! done
130 }
131
93 } // namespace android 132 } // namespace android
94 } // namespace base 133 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698