OLD | NEW |
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 Loading... |
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 |
| 37 static jmethodID g_MTCall = 0; |
| 38 jobject MTCall(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 "valueOf", |
| 44 "(I)Ljava/lang/String;", |
| 45 &g_MTCall); |
| 46 |
| 47 return env->CallStaticObjectMethod(clazz, g_MTCall, p); |
| 48 } |
| 49 |
| 50 jobject NonMTCall(JNIEnv* env, jclass clazz, int p) { |
| 51 return env->CallStaticObjectMethod(clazz, g_MTCall, p); |
| 52 } |
| 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 Loading... |
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, JNIWithLock) { |
| 112 JNIEnv* env = AttachCurrentThread(); |
| 113 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/String")); |
| 114 base::Time start_mt = base::Time::Now(); |
| 115 for (int i = 0; i < 1024; ++i) |
| 116 ScopedJavaLocalRef<jobject> o(env, MTCall(env, clazz.obj(), i)); |
| 117 base::Time end_mt = base::Time::Now(); |
| 118 |
| 119 base::Time start_non_mt = base::Time::Now(); |
| 120 for (int i = 0; i < 1024; ++i) |
| 121 ScopedJavaLocalRef<jobject> o(env, NonMTCall(env, clazz.obj(), i)); |
| 122 base::Time end_non_mt = base::Time::Now(); |
| 123 |
| 124 LOG(ERROR) << "JNI MT" << |
| 125 base::TimeDelta(start_mt - end_mt).InMicroseconds(); |
| 126 LOG(ERROR) << "JNI Non-MT" << |
| 127 base::TimeDelta(start_non_mt - end_non_mt).InMicroseconds(); |
| 128 } |
| 129 |
93 } // namespace android | 130 } // namespace android |
94 } // namespace base | 131 } // namespace base |
OLD | NEW |