|
| 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 | +} |
0 commit comments