xref: /aosp_15_r20/external/libchrome/base/android/jni_android_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/android/jni_android.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/at_exit.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/logging.h"
9*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker namespace base {
12*635a8641SAndroid Build Coastguard Worker namespace android {
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker namespace {
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker base::subtle::AtomicWord g_atomic_id = 0;
LazyMethodIDCall(JNIEnv * env,jclass clazz,int p)17*635a8641SAndroid Build Coastguard Worker int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) {
18*635a8641SAndroid Build Coastguard Worker   jmethodID id = base::android::MethodID::LazyGet<
19*635a8641SAndroid Build Coastguard Worker       base::android::MethodID::TYPE_STATIC>(
20*635a8641SAndroid Build Coastguard Worker       env, clazz,
21*635a8641SAndroid Build Coastguard Worker       "abs",
22*635a8641SAndroid Build Coastguard Worker       "(I)I",
23*635a8641SAndroid Build Coastguard Worker       &g_atomic_id);
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker   return env->CallStaticIntMethod(clazz, id, p);
26*635a8641SAndroid Build Coastguard Worker }
27*635a8641SAndroid Build Coastguard Worker 
MethodIDCall(JNIEnv * env,jclass clazz,jmethodID id,int p)28*635a8641SAndroid Build Coastguard Worker int MethodIDCall(JNIEnv* env, jclass clazz, jmethodID id, int p) {
29*635a8641SAndroid Build Coastguard Worker   return env->CallStaticIntMethod(clazz, id, p);
30*635a8641SAndroid Build Coastguard Worker }
31*635a8641SAndroid Build Coastguard Worker 
32*635a8641SAndroid Build Coastguard Worker }  // namespace
33*635a8641SAndroid Build Coastguard Worker 
TEST(JNIAndroidMicrobenchmark,MethodId)34*635a8641SAndroid Build Coastguard Worker TEST(JNIAndroidMicrobenchmark, MethodId) {
35*635a8641SAndroid Build Coastguard Worker   JNIEnv* env = AttachCurrentThread();
36*635a8641SAndroid Build Coastguard Worker   ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/Math"));
37*635a8641SAndroid Build Coastguard Worker   base::Time start_lazy = base::Time::Now();
38*635a8641SAndroid Build Coastguard Worker   int o = 0;
39*635a8641SAndroid Build Coastguard Worker   for (int i = 0; i < 1024; ++i)
40*635a8641SAndroid Build Coastguard Worker     o += LazyMethodIDCall(env, clazz.obj(), i);
41*635a8641SAndroid Build Coastguard Worker   base::Time end_lazy = base::Time::Now();
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker   jmethodID id = reinterpret_cast<jmethodID>(g_atomic_id);
44*635a8641SAndroid Build Coastguard Worker   base::Time start = base::Time::Now();
45*635a8641SAndroid Build Coastguard Worker   for (int i = 0; i < 1024; ++i)
46*635a8641SAndroid Build Coastguard Worker     o += MethodIDCall(env, clazz.obj(), id, i);
47*635a8641SAndroid Build Coastguard Worker   base::Time end = base::Time::Now();
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker   // On a Galaxy Nexus, results were in the range of:
50*635a8641SAndroid Build Coastguard Worker   // JNI LazyMethodIDCall (us) 1984
51*635a8641SAndroid Build Coastguard Worker   // JNI MethodIDCall (us) 1861
52*635a8641SAndroid Build Coastguard Worker   LOG(ERROR) << "JNI LazyMethodIDCall (us) " <<
53*635a8641SAndroid Build Coastguard Worker       base::TimeDelta(end_lazy - start_lazy).InMicroseconds();
54*635a8641SAndroid Build Coastguard Worker   LOG(ERROR) << "JNI MethodIDCall (us) " <<
55*635a8641SAndroid Build Coastguard Worker       base::TimeDelta(end - start).InMicroseconds();
56*635a8641SAndroid Build Coastguard Worker   LOG(ERROR) << "JNI " << o;
57*635a8641SAndroid Build Coastguard Worker }
58*635a8641SAndroid Build Coastguard Worker 
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker }  // namespace android
61*635a8641SAndroid Build Coastguard Worker }  // namespace base
62