📖 Scenario: You are building a mobile app and want to track how fast your app loads and how long certain actions take. Firebase Performance Monitoring helps you see this data easily.
🎯 Goal: Set up Firebase Performance Monitoring in your app, create a custom trace to measure a specific action, and display the trace duration.
📋 What You'll Learn
Initialize Firebase Performance Monitoring in your app
Create a custom performance trace named app_startup
Start and stop the app_startup trace around a simulated startup task
Print the duration of the app_startup trace in milliseconds
💡 Why This Matters
🌍 Real World
Firebase Performance Monitoring helps developers see how fast their app runs and find slow parts to improve user experience.
💼 Career
Knowing how to set up and use performance monitoring is important for DevOps and mobile developers to maintain app quality and reliability.
Progress0 / 4 steps
1
Initialize Firebase Performance Monitoring
Import Firebase Performance Monitoring and initialize it by creating a variable called performance using getPerformance().
Firebase
Hint
Use import { getPerformance } from "firebase/performance"; and then const performance = getPerformance(); to initialize.
2
Create a custom performance trace
Create a custom trace named app_startup by calling performance.trace('app_startup') and store it in a variable called trace.
Firebase
Hint
Use const trace = performance.trace('app_startup'); to create the trace.
3
Start and stop the custom trace
Start the trace by calling trace.start(), simulate a startup task with a 100ms delay using setTimeout, then stop the trace by calling trace.stop() inside the timeout callback.
Firebase
Hint
Call trace.start() before the delay, then use setTimeout with 100ms, and call trace.stop() inside the callback.
4
Print the trace duration
After stopping the trace, print the duration of the app_startup trace in milliseconds using console.log and accessing trace.duration.
Firebase
Hint
Use console.log(`Trace duration: ${trace.duration} ms`); inside the timeout callback after stopping the trace.
Practice
(1/5)
1. What is the main purpose of Firebase Performance Monitoring?
easy
A. To send notifications to users
B. To track app speed and identify slow parts
C. To store user data securely
D. To manage user authentication
Solution
Step 1: Understand the role of performance monitoring
Performance monitoring is used to check how fast an app runs and find parts that slow it down.
Step 2: Compare with other Firebase features
Features like data storage, notifications, and authentication serve different purposes unrelated to speed tracking.
Final Answer:
To track app speed and identify slow parts -> Option B
Why does the trace not record the duration correctly?
medium
A. Because trace.start is missing parentheses and not called as a function
B. Because trace.stop() should be called before trace.start()
C. Because getPerformance(app) must be called after trace creation
D. Because the trace name 'loadTime' is invalid
Solution
Step 1: Check how trace.start is used
The code uses trace.start; without parentheses, so the function is not executed.
Step 2: Understand function call syntax
To start the trace, trace.start() must be called with parentheses to run the function.
Final Answer:
Because trace.start is missing parentheses and not called as a function -> Option A
Quick Check:
trace.start() needs parentheses to run [OK]
Hint: Always call start() with parentheses to begin trace [OK]
Common Mistakes:
Forgetting parentheses on start() call
Calling stop() before start()
Thinking trace name affects recording
Misplacing getPerformance call
5. You want to measure the time taken by two different functions in your app using Firebase Performance Monitoring. Which approach correctly creates and uses two custom traces?