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 3 // found in the LICENSE file. 4 5 #include "base/android/jni_android.h" 6 7 #include "base/at_exit.h" 8 #include "base/logging.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace base { 12 namespace android { 13 14 namespace { 15 16 base::subtle::AtomicWord g_atomic_id = 0; 17 int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) { 18 jmethodID id = base::android::MethodID::LazyGet< 19 base::android::MethodID::TYPE_STATIC>( 20 env, clazz, 21 "abs", 22 "(I)I", 23 &g_atomic_id); 24 25 return env->CallStaticIntMethod(clazz, id, p); 26 } 27 28 int MethodIDCall(JNIEnv* env, jclass clazz, jmethodID id, int p) { 29 return env->CallStaticIntMethod(clazz, id, p); 30 } 31 32 } // namespace 33 34 TEST(JNIAndroidMicrobenchmark, MethodId) { 35 JNIEnv* env = AttachCurrentThread(); 36 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/Math")); 37 base::Time start_lazy = base::Time::Now(); 38 int o = 0; 39 for (int i = 0; i < 1024; ++i) 40 o += LazyMethodIDCall(env, clazz.obj(), i); 41 base::Time end_lazy = base::Time::Now(); 42 43 jmethodID id = reinterpret_cast<jmethodID>(g_atomic_id); 44 base::Time start = base::Time::Now(); 45 for (int i = 0; i < 1024; ++i) 46 o += MethodIDCall(env, clazz.obj(), id, i); 47 base::Time end = base::Time::Now(); 48 49 // On a Galaxy Nexus, results were in the range of: 50 // JNI LazyMethodIDCall (us) 1984 51 // JNI MethodIDCall (us) 1861 52 LOG(ERROR) << "JNI LazyMethodIDCall (us) " << 53 base::TimeDelta(end_lazy - start_lazy).InMicroseconds(); 54 LOG(ERROR) << "JNI MethodIDCall (us) " << 55 base::TimeDelta(end - start).InMicroseconds(); 56 LOG(ERROR) << "JNI " << o; 57 } 58 59 60 } // namespace android 61 } // namespace base 62