Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions performance/example_usage.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#include <iostream>

#include "app/src/include/firebase/app.h"
#include "performance/src/include/firebase/performance.h"
#include "performance/src/include/firebase/performance/http_metric.h"
#include "performance/src/include/firebase/performance/trace.h"

int main() {
// Initialize
firebase::InitResult fireperf_init_result =
firebase::performance::Initialize(*firebase::App::GetInstance());

if (fireperf_init_result ==
firebase::InitResult::kInitResultFailedMissingDependency) {
std::cout << "Failed to initialize firebase performance." << std::endl;
} else {
std::cout << "Successfully initialized firebase performance." << std::endl;
}

// Enable firebase performance monitoring.
firebase::performance::SetPerformanceCollectionEnabled(true);

// Disable firebase performance monitoring.
firebase::performance::SetPerformanceCollectionEnabled(false);

if (firebase::performance::GetPerformanceCollectionEnabled()) {
std::cout << "Firebase Performance monitoring is enabled." << std::endl;
} else {
std::cout << "Firebase Performance monitoring is disabled." << std::endl;
}

// Create and start a Trace on the heap, add custom attributes, metrics.
auto trace =
new firebase::performance::Trace("myMethod"); // Also starts the trace.
std::cout << "Trace started status: " << trace->is_started() << std::endl;
trace->IncrementMetric("cacheHit", 2);
trace->SetMetric("cacheSize", 50);
// Currently returns a stub value, but it should return 50 when a fake is
// implemented.
std::cout << "Value of the \"cacheSize\" metric: "
<< trace->GetLongMetric("cacheSize") << std::endl;

trace->SetAttribute("level", "4");
trace->GetAttribute("level"); // returns "4"
trace->SetAttribute("level", nullptr);

// Stop trace, and re-use the object for another trace.
trace->Start("myOtherMethod");

delete trace; // Logs myOtherMethod and deletes the object.

// Create a Trace on the heap, start it later and then stop it.
auto delayed_start_trace = new firebase::performance::Trace();
// Do some set up work that we don't want included in the trace duration.

// Once we're ready, start.
delayed_start_trace->Start("criticalSectionOfCode");

// Interesting code ends.
delete delayed_start_trace; // Stops and logs it to the backend.

// Trace using automatic storage (in this case on the stack).
{
firebase::performance::Trace trace_stack("myMethod");
trace_stack.IncrementMetric("cacheHit", 2);
trace_stack.SetMetric("cacheSize", 50);
// Currently returns a stub value, but it should return 50 when a fake is
// implemented.
std::cout << "Value of the \"cacheSize\" metric: "
<< trace_stack.GetLongMetric("cacheSize") << std::endl;

trace_stack.SetAttribute("level", "4");
// Currently returns a stub value, but should return 4 when a fake is
// implemented.
std::cout << "Value of \"level\" attribute on the \"myMethod\" trace: "
<< trace_stack.GetAttribute("level") << std::endl;
trace_stack.SetAttribute("level", nullptr);
std::cout << "Trace started status: " << trace_stack.is_started()
<< std::endl;
}
// Stop is called when it's destructed, and the trace is logged to the
// backend.

// Trace on the stack, and start it later.
{
firebase::performance::Trace trace_stack;

trace_stack.Start("someTrace");
trace_stack.IncrementMetric("cacheHit", 2);

trace_stack.Start(
"someOtherTrace"); // Logs someTrace, and starts "someOtherTrace"
trace_stack.Cancel(); // Cancel someOtherTrace.
std::cout << "Trace started status: " << trace_stack.is_started()
<< std::endl;
}

// Create an HttpMetric, custom attributes, counters and add details.
// Note: Only needed if developer is using non-standard networking library

// On the heap
auto http_metric = new firebase::performance::HttpMetric(
"https://google.com", firebase::performance::HttpMethod::kHttpMethodGet);

// Add more detail to http metric
http_metric->set_http_response_code(200);
http_metric->set_request_payload_size(25);
http_metric->set_response_content_type("application/json");
http_metric->set_response_payload_size(500);

std::cout << "HttpMetric started status: " << http_metric->is_started()
<< std::endl;

http_metric->SetAttribute("level", "4");
// Currently returns a stub value, but should return 4 when a fake is
// implemented.
std::cout
<< "Value of \"level\" attribute on the \"google.com\" http metric: "
<< http_metric->GetAttribute("level") << std::endl;

// Logs the google.com http metric and starts a new one for a different
// network request.
http_metric->Start("https://firebase.com",
firebase::performance::HttpMethod::kHttpMethodPost);
http_metric->set_response_payload_size(500);

delete http_metric; // Stops and logs it to the backend.

// create an http metric object on the heap, but start it later.
auto http_metric_delayed_start = new firebase::performance::HttpMetric();

// Do some setup.

// Start the trace.
http_metric_delayed_start->Start(
"https://firebase.com",
firebase::performance::HttpMethod::kHttpMethodGet);

// Stop it.
http_metric_delayed_start->Stop();

// HttpMetric using Automatic storage (in this case on the stack), restarted
// so that the first one is logged, and then the new one is cancelled which is
// not logged.
{
// This also starts the HttpMetric.
firebase::performance::HttpMetric http_metric_stack(
"https://google.com",
firebase::performance::HttpMethod::kHttpMethodGet);

// Add more detail to http metric
http_metric_stack.set_http_response_code(200);
http_metric_stack.set_request_payload_size(25);
http_metric_stack.set_response_content_type("application/json");
http_metric_stack.set_response_payload_size(500);

http_metric_stack.SetAttribute("level", "4");
// Currently returns a stub value, but should return 4 when a fake is
// implemented.
std::cout
<< "Value of \"level\" attribute on the \"google.com\" http metric: "
<< http_metric_stack.GetAttribute("level") << std::endl;

// Stops the google.com http metric and starts a new one that tracks the
// firebase.com network request.
http_metric_stack.Start("https://firebase.com",
firebase::performance::HttpMethod::kHttpMethodPost);

std::cout << "HttpMetric started status: " << http_metric_stack.is_started()
<< std::endl;

// Cancels the new firebase.com network trace, because it doesn't have any
// valid data.
http_metric_stack.Cancel();

std::cout << "HttpMetric started status: " << http_metric_stack.is_started()
<< std::endl;
}

// HttpMetric on stack is stopped and logged when it's destroyed.
{
firebase::performance::HttpMetric http_metric_stack;

http_metric_stack.Start("https://google.com",
firebase::performance::HttpMethod::kHttpMethodGet);

// Add more detail to http metric
http_metric_stack.set_http_response_code(200);
} // HttpMetric is stopped and logged to the backend as part of being
// destroyed.

return 0;
}
147 changes: 147 additions & 0 deletions performance/src/android/firebase_performance.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2016 Google Inc. All Rights Reserved.

#include <assert.h>
#include <jni.h>

#include <cstdint>
#include <cstring>

#include "app/src/assert.h"
#include "app/src/include/firebase/app.h"
#include "app/src/include/firebase/version.h"
#include "app/src/log.h"
#include "performance/src/android/performance_android_internal.h"
#include "performance/src/include/firebase/performance.h"
#include "performance/src/performance_common.h"

namespace firebase {
namespace performance {

// Global reference to the Android Performance class instance.
// This is initialized in performance::Initialize() and never released
// during the lifetime of the application.
static jobject g_performance_class_instance = nullptr;

// Used to retrieve the JNI environment in order to call methods on the
// Android Performance class.
static const ::firebase::App* g_app = nullptr;

// Initialize the Performance API.
InitResult Initialize(const ::firebase::App& app) {
if (g_app) {
LogWarning("%s API already initialized", internal::kPerformanceModuleName);
return kInitResultSuccess;
}
LogInfo("Firebase Performance API Initializing");
FIREBASE_ASSERT(!g_performance_class_instance);
JNIEnv* env = app.GetJNIEnv();

if (!util::Initialize(env, app.activity())) {
return kInitResultFailedMissingDependency;
}

// Cache method pointers on FirebasePerformance.
if (!internal::performance_jni::CacheMethodIds(env, app.activity())) {
util::Terminate(env);
return kInitResultFailedMissingDependency;
}

// Cache method pointers on HttpMetric.
if (!internal::http_metric_jni::CacheMethodIds(env, app.activity())) {
util::Terminate(env);
return kInitResultFailedMissingDependency;
}

// Cache method pointers on Trace.
if (!internal::trace_jni::CacheMethodIds(env, app.activity())) {
util::Terminate(env);
return kInitResultFailedMissingDependency;
}

g_app = &app;
// Get / create the Performance singleton.
jobject performance_class_instance_local =
env->CallStaticObjectMethod(internal::performance_jni::GetClass(),
internal::performance_jni::GetMethodId(
internal::performance_jni::kGetInstance));
util::CheckAndClearJniExceptions(env);
// Keep a reference to the Performance singleton.
g_performance_class_instance =
env->NewGlobalRef(performance_class_instance_local);
FIREBASE_ASSERT(g_performance_class_instance);

env->DeleteLocalRef(performance_class_instance_local);
internal::RegisterTerminateOnDefaultAppDestroy();
LogInfo("%s API Initialized", internal::kPerformanceModuleName);
return kInitResultSuccess;
}

namespace internal {

// Determine whether the performance module is initialized.
bool IsInitialized() { return g_app != nullptr; }

jobject GetFirebasePerformanceClassInstance() {
return g_performance_class_instance;
}

const ::firebase::App* GetFirebaseApp() { return g_app; }

DEFINE_FIREBASE_VERSION_STRING(FirebasePerformance);

METHOD_LOOKUP_DEFINITION(performance_jni,
PROGUARD_KEEP_CLASS
"com/google/firebase/perf/FirebasePerformance",
PERFORMANCE_METHODS)

METHOD_LOOKUP_DEFINITION(http_metric_jni,
"com/google/firebase/perf/metrics/HttpMetric",
HTTP_METRIC_METHODS)

METHOD_LOOKUP_DEFINITION(trace_jni, "com/google/firebase/perf/metrics/Trace",
TRACE_METHODS)

} // namespace internal

// Clean up the API.
void Terminate() {
if (!g_app) {
LogWarning("%s API already shut down", internal::kPerformanceModuleName);
return;
}
JNIEnv* env = g_app->GetJNIEnv();
util::CancelCallbacks(env, internal::kPerformanceModuleName);
internal::UnregisterTerminateOnDefaultAppDestroy();
g_app = nullptr;
env->DeleteGlobalRef(g_performance_class_instance);
g_performance_class_instance = nullptr;
internal::performance_jni::ReleaseClass(env);
util::Terminate(env);
}

// Determines if performance collection is enabled.
bool GetPerformanceCollectionEnabled() {
FIREBASE_ASSERT_RETURN(false, internal::IsInitialized());
JNIEnv* env = g_app->GetJNIEnv();
jboolean result =
env->CallBooleanMethod(g_performance_class_instance,
internal::performance_jni::GetMethodId(
internal::performance_jni::kGetEnabled));
util::CheckAndClearJniExceptions(env);
return static_cast<bool>(result);
}

// Sets performance collection enabled or disabled.
void SetPerformanceCollectionEnabled(bool enabled) {
FIREBASE_ASSERT_RETURN_VOID(internal::IsInitialized());
JNIEnv* env = g_app->GetJNIEnv();
jboolean collection_enabled = enabled;
env->CallVoidMethod(g_performance_class_instance,
internal::performance_jni::GetMethodId(
internal::performance_jni::kSetEnabled),
collection_enabled);
util::CheckAndClearJniExceptions(env);
}

} // namespace performance
} // namespace firebase
Loading