-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathindex.js
111 lines (84 loc) · 2.83 KB
/
index.js
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
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
const perf = getInstance();
function intialize() {
// [START perf_import_app]
const { initializeApp } = require("firebase/app");
// [END perf_import_app]
// [START perf_import]
const { getPerformance } = require("firebase/performance");
// [END perf_import]
// [START perf_initialize_app]
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// [END perf_initialize_app]
// [START perf_singleton]
// Initialize Performance Monitoring and get a reference to the service
const perf = getPerformance(app);
// [END perf_singleton]
}
export function getInstance() {
// [START perf_get_instance]
const { getPerformance } = require("firebase/performance");
const perf = getPerformance();
// [END perf_get_instance]
return perf;
}
export function addCustomTrace() {
// [START perf_add_custom_trace]
const { trace } = require("firebase/performance");
const t = trace(perf, "CUSTOM_TRACE_NAME");
t.start();
// Code that you want to trace
// ...
t.stop();
// [END perf_add_custom_trace]
}
export function userTimingMarks() {
// [START perf_user_timing_marks]
const performance = window.performance;
performance.mark("measurementStart");
// Code that you want to trace
// ...
performance.mark("measurementStop");
performance.measure("customTraceName", "measurementStart", "measurementStop");
// [END perf_user_timing_marks]
}
export function addCustomAttributes() {
// [START perf_add_custom_attributes]
const { trace } = require("firebase/performance");
const t = trace(perf, "test_trace");
t.putAttribute("experiment", "A");
// Update scenario
t.putAttribute("experiment", "B");
// Reading scenario
const experimentValue = t.getAttribute("experiment");
// Delete scenario
t.removeAttribute("experiment");
// Read attributes
const traceAttributes = t.getAttributes();
// [END perf_add_custom_attributes]
}
export function addCustomMetrics() {
async function retrieveInventory(inventoryIds) {
return {};
}
// [START perf_add_custom_metrics]
const { trace } = require("firebase/performance");
async function getInventory(inventoryIds) {
const t = trace(perf, "inventoryRetrieval");
// Tracks the number of IDs fetched (the metric could help you to optimize in the future)
t.incrementMetric("numberOfIds", inventoryIds.length);
// Measures the time it takes to request inventory based on the amount of inventory
t.start();
const inventoryData = await retrieveInventory(inventoryIds);
t.stop();
return inventoryData;
}
// [END perf_add_custom_metrics]
}