-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathexample_usage.cc
195 lines (154 loc) · 7.2 KB
/
example_usage.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2021 Google LLC
#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;
}