1 // Copyright 2012 The Chromium Authors
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 "base/threading/thread.h"
10 #include "base/time/time.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::StartsWith;
21 std::atomic<jmethodID> g_atomic_id(nullptr);
22 int LazyMethodIDCall(JNIEnv* env, jclass clazz, int p) {
23 jmethodID id = base::android::MethodID::LazyGet<
24 base::android::MethodID::TYPE_STATIC>(
30 return env->CallStaticIntMethod(clazz, id, p);
33 int MethodIDCall(JNIEnv* env, jclass clazz, jmethodID id, int p) {
34 return env->CallStaticIntMethod(clazz, id, p);
39 TEST(JNIAndroidMicrobenchmark, MethodId) {
40 JNIEnv* env = AttachCurrentThread();
41 ScopedJavaLocalRef<jclass> clazz(GetClass(env, "java/lang/Math"));
42 base::Time start_lazy = base::Time::Now();
44 for (int i = 0; i < 1024; ++i)
45 o += LazyMethodIDCall(env, clazz.obj(), i);
46 base::Time end_lazy = base::Time::Now();
48 jmethodID id = g_atomic_id;
49 base::Time start = base::Time::Now();
50 for (int i = 0; i < 1024; ++i)
51 o += MethodIDCall(env, clazz.obj(), id, i);
52 base::Time end = base::Time::Now();
54 // On a Galaxy Nexus, results were in the range of:
55 // JNI LazyMethodIDCall (us) 1984
56 // JNI MethodIDCall (us) 1861
57 LOG(ERROR) << "JNI LazyMethodIDCall (us) " <<
58 base::TimeDelta(end_lazy - start_lazy).InMicroseconds();
59 LOG(ERROR) << "JNI MethodIDCall (us) " <<
60 base::TimeDelta(end - start).InMicroseconds();
61 LOG(ERROR) << "JNI " << o;
64 TEST(JNIAndroidTest, GetJavaStackTraceIfPresent) {
65 // The main thread should always have Java frames in it.
66 EXPECT_THAT(GetJavaStackTraceIfPresent(), StartsWith("\tat"));
68 class HelperThread : public Thread {
71 : Thread("TestThread"), java_stack_1_("X"), java_stack_2_("X") {}
73 void Init() override {
74 // Test without a JNIEnv.
75 java_stack_1_ = GetJavaStackTraceIfPresent();
77 // Test with a JNIEnv but no Java frames.
78 AttachCurrentThread();
79 java_stack_2_ = GetJavaStackTraceIfPresent();
82 std::string java_stack_1_;
83 std::string java_stack_2_;
87 t.StartAndWaitForTesting();
88 EXPECT_EQ(t.java_stack_1_, "");
89 EXPECT_EQ(t.java_stack_2_, "");
92 } // namespace android