Skip to content

Commit a966ef8

Browse files
jonsimantovAlex Ames
authored andcommitted
Added experimental Firebase Performance library code.
PiperOrigin-RevId: 356347936
1 parent c5cf0fe commit a966ef8

30 files changed

+4370
-0
lines changed

performance/example_usage.cc

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#include <iostream>
2+
3+
#include "app/src/include/firebase/app.h"
4+
#include "performance/src/include/firebase/performance.h"
5+
#include "performance/src/include/firebase/performance/http_metric.h"
6+
#include "performance/src/include/firebase/performance/trace.h"
7+
8+
int main() {
9+
// Initialize
10+
firebase::InitResult fireperf_init_result =
11+
firebase::performance::Initialize(*firebase::App::GetInstance());
12+
13+
if (fireperf_init_result ==
14+
firebase::InitResult::kInitResultFailedMissingDependency) {
15+
std::cout << "Failed to initialize firebase performance." << std::endl;
16+
} else {
17+
std::cout << "Successfully initialized firebase performance." << std::endl;
18+
}
19+
20+
// Enable firebase performance monitoring.
21+
firebase::performance::SetPerformanceCollectionEnabled(true);
22+
23+
// Disable firebase performance monitoring.
24+
firebase::performance::SetPerformanceCollectionEnabled(false);
25+
26+
if (firebase::performance::GetPerformanceCollectionEnabled()) {
27+
std::cout << "Firebase Performance monitoring is enabled." << std::endl;
28+
} else {
29+
std::cout << "Firebase Performance monitoring is disabled." << std::endl;
30+
}
31+
32+
// Create and start a Trace on the heap, add custom attributes, metrics.
33+
auto trace =
34+
new firebase::performance::Trace("myMethod"); // Also starts the trace.
35+
std::cout << "Trace started status: " << trace->is_started() << std::endl;
36+
trace->IncrementMetric("cacheHit", 2);
37+
trace->SetMetric("cacheSize", 50);
38+
// Currently returns a stub value, but it should return 50 when a fake is
39+
// implemented.
40+
std::cout << "Value of the \"cacheSize\" metric: "
41+
<< trace->GetLongMetric("cacheSize") << std::endl;
42+
43+
trace->SetAttribute("level", "4");
44+
trace->GetAttribute("level"); // returns "4"
45+
trace->SetAttribute("level", nullptr);
46+
47+
// Stop trace, and re-use the object for another trace.
48+
trace->Start("myOtherMethod");
49+
50+
delete trace; // Logs myOtherMethod and deletes the object.
51+
52+
// Create a Trace on the heap, start it later and then stop it.
53+
auto delayed_start_trace = new firebase::performance::Trace();
54+
// Do some set up work that we don't want included in the trace duration.
55+
56+
// Once we're ready, start.
57+
delayed_start_trace->Start("criticalSectionOfCode");
58+
59+
// Interesting code ends.
60+
delete delayed_start_trace; // Stops and logs it to the backend.
61+
62+
// Trace using automatic storage (in this case on the stack).
63+
{
64+
firebase::performance::Trace trace_stack("myMethod");
65+
trace_stack.IncrementMetric("cacheHit", 2);
66+
trace_stack.SetMetric("cacheSize", 50);
67+
// Currently returns a stub value, but it should return 50 when a fake is
68+
// implemented.
69+
std::cout << "Value of the \"cacheSize\" metric: "
70+
<< trace_stack.GetLongMetric("cacheSize") << std::endl;
71+
72+
trace_stack.SetAttribute("level", "4");
73+
// Currently returns a stub value, but should return 4 when a fake is
74+
// implemented.
75+
std::cout << "Value of \"level\" attribute on the \"myMethod\" trace: "
76+
<< trace_stack.GetAttribute("level") << std::endl;
77+
trace_stack.SetAttribute("level", nullptr);
78+
std::cout << "Trace started status: " << trace_stack.is_started()
79+
<< std::endl;
80+
}
81+
// Stop is called when it's destructed, and the trace is logged to the
82+
// backend.
83+
84+
// Trace on the stack, and start it later.
85+
{
86+
firebase::performance::Trace trace_stack;
87+
88+
trace_stack.Start("someTrace");
89+
trace_stack.IncrementMetric("cacheHit", 2);
90+
91+
trace_stack.Start(
92+
"someOtherTrace"); // Logs someTrace, and starts "someOtherTrace"
93+
trace_stack.Cancel(); // Cancel someOtherTrace.
94+
std::cout << "Trace started status: " << trace_stack.is_started()
95+
<< std::endl;
96+
}
97+
98+
// Create an HttpMetric, custom attributes, counters and add details.
99+
// Note: Only needed if developer is using non-standard networking library
100+
101+
// On the heap
102+
auto http_metric = new firebase::performance::HttpMetric(
103+
"https://google.com", firebase::performance::HttpMethod::kHttpMethodGet);
104+
105+
// Add more detail to http metric
106+
http_metric->set_http_response_code(200);
107+
http_metric->set_request_payload_size(25);
108+
http_metric->set_response_content_type("application/json");
109+
http_metric->set_response_payload_size(500);
110+
111+
std::cout << "HttpMetric started status: " << http_metric->is_started()
112+
<< std::endl;
113+
114+
http_metric->SetAttribute("level", "4");
115+
// Currently returns a stub value, but should return 4 when a fake is
116+
// implemented.
117+
std::cout
118+
<< "Value of \"level\" attribute on the \"google.com\" http metric: "
119+
<< http_metric->GetAttribute("level") << std::endl;
120+
121+
// Logs the google.com http metric and starts a new one for a different
122+
// network request.
123+
http_metric->Start("https://firebase.com",
124+
firebase::performance::HttpMethod::kHttpMethodPost);
125+
http_metric->set_response_payload_size(500);
126+
127+
delete http_metric; // Stops and logs it to the backend.
128+
129+
// create an http metric object on the heap, but start it later.
130+
auto http_metric_delayed_start = new firebase::performance::HttpMetric();
131+
132+
// Do some setup.
133+
134+
// Start the trace.
135+
http_metric_delayed_start->Start(
136+
"https://firebase.com",
137+
firebase::performance::HttpMethod::kHttpMethodGet);
138+
139+
// Stop it.
140+
http_metric_delayed_start->Stop();
141+
142+
// HttpMetric using Automatic storage (in this case on the stack), restarted
143+
// so that the first one is logged, and then the new one is cancelled which is
144+
// not logged.
145+
{
146+
// This also starts the HttpMetric.
147+
firebase::performance::HttpMetric http_metric_stack(
148+
"https://google.com",
149+
firebase::performance::HttpMethod::kHttpMethodGet);
150+
151+
// Add more detail to http metric
152+
http_metric_stack.set_http_response_code(200);
153+
http_metric_stack.set_request_payload_size(25);
154+
http_metric_stack.set_response_content_type("application/json");
155+
http_metric_stack.set_response_payload_size(500);
156+
157+
http_metric_stack.SetAttribute("level", "4");
158+
// Currently returns a stub value, but should return 4 when a fake is
159+
// implemented.
160+
std::cout
161+
<< "Value of \"level\" attribute on the \"google.com\" http metric: "
162+
<< http_metric_stack.GetAttribute("level") << std::endl;
163+
164+
// Stops the google.com http metric and starts a new one that tracks the
165+
// firebase.com network request.
166+
http_metric_stack.Start("https://firebase.com",
167+
firebase::performance::HttpMethod::kHttpMethodPost);
168+
169+
std::cout << "HttpMetric started status: " << http_metric_stack.is_started()
170+
<< std::endl;
171+
172+
// Cancels the new firebase.com network trace, because it doesn't have any
173+
// valid data.
174+
http_metric_stack.Cancel();
175+
176+
std::cout << "HttpMetric started status: " << http_metric_stack.is_started()
177+
<< std::endl;
178+
}
179+
180+
// HttpMetric on stack is stopped and logged when it's destroyed.
181+
{
182+
firebase::performance::HttpMetric http_metric_stack;
183+
184+
http_metric_stack.Start("https://google.com",
185+
firebase::performance::HttpMethod::kHttpMethodGet);
186+
187+
// Add more detail to http metric
188+
http_metric_stack.set_http_response_code(200);
189+
} // HttpMetric is stopped and logged to the backend as part of being
190+
// destroyed.
191+
192+
return 0;
193+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Copyright 2016 Google Inc. All Rights Reserved.
2+
3+
#include <assert.h>
4+
#include <jni.h>
5+
6+
#include <cstdint>
7+
#include <cstring>
8+
9+
#include "app/src/assert.h"
10+
#include "app/src/include/firebase/app.h"
11+
#include "app/src/include/firebase/version.h"
12+
#include "app/src/log.h"
13+
#include "performance/src/android/performance_android_internal.h"
14+
#include "performance/src/include/firebase/performance.h"
15+
#include "performance/src/performance_common.h"
16+
17+
namespace firebase {
18+
namespace performance {
19+
20+
// Global reference to the Android Performance class instance.
21+
// This is initialized in performance::Initialize() and never released
22+
// during the lifetime of the application.
23+
static jobject g_performance_class_instance = nullptr;
24+
25+
// Used to retrieve the JNI environment in order to call methods on the
26+
// Android Performance class.
27+
static const ::firebase::App* g_app = nullptr;
28+
29+
// Initialize the Performance API.
30+
InitResult Initialize(const ::firebase::App& app) {
31+
if (g_app) {
32+
LogWarning("%s API already initialized", internal::kPerformanceModuleName);
33+
return kInitResultSuccess;
34+
}
35+
LogInfo("Firebase Performance API Initializing");
36+
FIREBASE_ASSERT(!g_performance_class_instance);
37+
JNIEnv* env = app.GetJNIEnv();
38+
39+
if (!util::Initialize(env, app.activity())) {
40+
return kInitResultFailedMissingDependency;
41+
}
42+
43+
// Cache method pointers on FirebasePerformance.
44+
if (!internal::performance_jni::CacheMethodIds(env, app.activity())) {
45+
util::Terminate(env);
46+
return kInitResultFailedMissingDependency;
47+
}
48+
49+
// Cache method pointers on HttpMetric.
50+
if (!internal::http_metric_jni::CacheMethodIds(env, app.activity())) {
51+
util::Terminate(env);
52+
return kInitResultFailedMissingDependency;
53+
}
54+
55+
// Cache method pointers on Trace.
56+
if (!internal::trace_jni::CacheMethodIds(env, app.activity())) {
57+
util::Terminate(env);
58+
return kInitResultFailedMissingDependency;
59+
}
60+
61+
g_app = &app;
62+
// Get / create the Performance singleton.
63+
jobject performance_class_instance_local =
64+
env->CallStaticObjectMethod(internal::performance_jni::GetClass(),
65+
internal::performance_jni::GetMethodId(
66+
internal::performance_jni::kGetInstance));
67+
util::CheckAndClearJniExceptions(env);
68+
// Keep a reference to the Performance singleton.
69+
g_performance_class_instance =
70+
env->NewGlobalRef(performance_class_instance_local);
71+
FIREBASE_ASSERT(g_performance_class_instance);
72+
73+
env->DeleteLocalRef(performance_class_instance_local);
74+
internal::RegisterTerminateOnDefaultAppDestroy();
75+
LogInfo("%s API Initialized", internal::kPerformanceModuleName);
76+
return kInitResultSuccess;
77+
}
78+
79+
namespace internal {
80+
81+
// Determine whether the performance module is initialized.
82+
bool IsInitialized() { return g_app != nullptr; }
83+
84+
jobject GetFirebasePerformanceClassInstance() {
85+
return g_performance_class_instance;
86+
}
87+
88+
const ::firebase::App* GetFirebaseApp() { return g_app; }
89+
90+
DEFINE_FIREBASE_VERSION_STRING(FirebasePerformance);
91+
92+
METHOD_LOOKUP_DEFINITION(performance_jni,
93+
PROGUARD_KEEP_CLASS
94+
"com/google/firebase/perf/FirebasePerformance",
95+
PERFORMANCE_METHODS)
96+
97+
METHOD_LOOKUP_DEFINITION(http_metric_jni,
98+
"com/google/firebase/perf/metrics/HttpMetric",
99+
HTTP_METRIC_METHODS)
100+
101+
METHOD_LOOKUP_DEFINITION(trace_jni, "com/google/firebase/perf/metrics/Trace",
102+
TRACE_METHODS)
103+
104+
} // namespace internal
105+
106+
// Clean up the API.
107+
void Terminate() {
108+
if (!g_app) {
109+
LogWarning("%s API already shut down", internal::kPerformanceModuleName);
110+
return;
111+
}
112+
JNIEnv* env = g_app->GetJNIEnv();
113+
util::CancelCallbacks(env, internal::kPerformanceModuleName);
114+
internal::UnregisterTerminateOnDefaultAppDestroy();
115+
g_app = nullptr;
116+
env->DeleteGlobalRef(g_performance_class_instance);
117+
g_performance_class_instance = nullptr;
118+
internal::performance_jni::ReleaseClass(env);
119+
util::Terminate(env);
120+
}
121+
122+
// Determines if performance collection is enabled.
123+
bool GetPerformanceCollectionEnabled() {
124+
FIREBASE_ASSERT_RETURN(false, internal::IsInitialized());
125+
JNIEnv* env = g_app->GetJNIEnv();
126+
jboolean result =
127+
env->CallBooleanMethod(g_performance_class_instance,
128+
internal::performance_jni::GetMethodId(
129+
internal::performance_jni::kGetEnabled));
130+
util::CheckAndClearJniExceptions(env);
131+
return static_cast<bool>(result);
132+
}
133+
134+
// Sets performance collection enabled or disabled.
135+
void SetPerformanceCollectionEnabled(bool enabled) {
136+
FIREBASE_ASSERT_RETURN_VOID(internal::IsInitialized());
137+
JNIEnv* env = g_app->GetJNIEnv();
138+
jboolean collection_enabled = enabled;
139+
env->CallVoidMethod(g_performance_class_instance,
140+
internal::performance_jni::GetMethodId(
141+
internal::performance_jni::kSetEnabled),
142+
collection_enabled);
143+
util::CheckAndClearJniExceptions(env);
144+
}
145+
146+
} // namespace performance
147+
} // namespace firebase

0 commit comments

Comments
 (0)