Skip to content

Commit

Permalink
Hook de mesure des perfs avec une base Firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
pcanthelou-pass committed Nov 24, 2024
1 parent 1dc8f1a commit 020f1c2
Show file tree
Hide file tree
Showing 4 changed files with 605 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/shared/performance/types.tsx
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 }
}
58 changes: 58 additions & 0 deletions src/shared/performance/useFirebasePerformanceProfiler.tsx
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,
})
}
Loading

0 comments on commit 020f1c2

Please sign in to comment.