-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook de mesure des perfs avec une base Firebase
- Loading branch information
1 parent
1dc8f1a
commit 020f1c2
Showing
4 changed files
with
605 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export interface Metric { | ||
metricName: string | ||
value: number | ||
} | ||
|
||
export interface RouteParams { | ||
navigationStartTimeRef?: number | ||
} | ||
|
||
export interface Route { | ||
params?: RouteParams | ||
} | ||
|
||
export interface UsePerformanceProfilerOptions { | ||
route?: Route | ||
shouldProfile?: boolean | ||
} | ||
|
||
export interface PerformanceService { | ||
startTrace: (traceName: string) => Promise<Trace> | ||
sanitizeMetricName: (traceName: string) => string | ||
sanitizeTraceName: (traceName: string) => string | ||
} | ||
|
||
export interface Trace { | ||
putMetric: (metricName: string, value: number) => void | ||
stop: () => void | ||
} | ||
|
||
export interface InteractionService { | ||
runAfterInteractions: (callback: () => void) => { cancel: () => void } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import perf from '@react-native-firebase/perf' | ||
import { InteractionManager } from 'react-native' | ||
|
||
import { useAppStartTimeStore } from 'shared/performance/appStartTimeStore' | ||
import { usePerformanceProfiler } from 'shared/performance/usePerformanceProfiler' | ||
|
||
import { Route } from './types' | ||
|
||
function sanitizeName(name: string, length: number): string { | ||
// Supprimer les espaces en début et fin | ||
let sanitized = name.trim() | ||
// Supprimer les underscores au début et à la fin | ||
sanitized = sanitized.replace(/(^_+)|(_+$)/g, '') | ||
// Tronquer à 32 caractères maximum | ||
if (sanitized.length > length) { | ||
sanitized = sanitized.substring(0, length) | ||
} | ||
return sanitized | ||
} | ||
export function sanitizeMetricName(name: string): string { | ||
return sanitizeName(name, 32) | ||
} | ||
|
||
export function sanitizeTraceName(name: string): string { | ||
return sanitizeName(name, 100) | ||
} | ||
|
||
type useFirebasePerformanceProfilerProps = { | ||
route?: Route | ||
shouldProfile?: boolean | ||
} | ||
|
||
export const useFirebasePerformanceProfiler = ( | ||
traceName: string, | ||
props?: useFirebasePerformanceProfilerProps | ||
) => { | ||
const { route = undefined, shouldProfile = true } = props || {} | ||
const performanceService = { | ||
startTrace: async (identifier: string) => perf().startTrace(identifier), | ||
sanitizeMetricName: sanitizeMetricName, | ||
sanitizeTraceName: sanitizeTraceName, | ||
} | ||
|
||
const interactionService = { | ||
runAfterInteractions: InteractionManager.runAfterInteractions, | ||
} | ||
|
||
const { appStartTime } = useAppStartTimeStore() | ||
|
||
// Utilisation du hook de performance pour mesurer les délais | ||
return usePerformanceProfiler(traceName, { | ||
performanceService, | ||
interactionService, | ||
appStartTime, | ||
route, | ||
shouldProfile, | ||
}) | ||
} |
Oops, something went wrong.