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.
5 #include "base/android/jni_android.h"
7 #include "base/at_exit.h"
8 #include "base/logging.h"
9 #include "testing/gtest/include/gtest/gtest.h"
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
>(
25 return env
->CallStaticIntMethod(clazz
, id
, p
);
28 int MethodIDCall(JNIEnv
* env
, jclass clazz
, jmethodID id
, int p
) {
29 return env
->CallStaticIntMethod(clazz
, id
, p
);
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();
39 for (int i
= 0; i
< 1024; ++i
)
40 o
+= LazyMethodIDCall(env
, clazz
.obj(), i
);
41 base::Time end_lazy
= base::Time::Now();
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();
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
;
60 } // namespace android