Skip to content

Commit

Permalink
feat: Add PerformanceApi to SearchEngine
Browse files Browse the repository at this point in the history
We want to add some metrics to understand where time is consumed

All measurement are made through the PerformanceApi helper that can be
injected from consuming app

Related PR: cozy/cozy-client#1574
  • Loading branch information
Ldoppea committed Feb 6, 2025
1 parent d1753d7 commit 2e87021
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions packages/cozy-dataproxy-lib/src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import FlexSearch from 'flexsearch'

import CozyClient from 'cozy-client'
import CozyClient, { defaultPerformanceApi } from 'cozy-client'
import type { PerformanceAPI } from 'cozy-client/types/performances/types'
import Minilog from 'cozy-minilog'
import { RealtimePlugin } from 'cozy-realtime'

Expand Down Expand Up @@ -55,11 +56,17 @@ export class SearchEngine {
debouncedReplication: () => void
isLocalSearch: boolean
storage: StorageInterface
performanceApi: PerformanceAPI

constructor(client: CozyClient, storage: StorageInterface) {
constructor(
client: CozyClient,
storage: StorageInterface,
performanceApi?: PerformanceAPI
) {
this.client = client
this.searchIndexes = {} as SearchIndexes
this.storage = storage
this.performanceApi = performanceApi ?? defaultPerformanceApi

this.isLocalSearch = !!getPouchLink(this.client)
log.info('Use local data on trusted device: ', this.isLocalSearch)
Expand All @@ -85,6 +92,8 @@ export class SearchEngine {
return
}

const markName = this.performanceApi.mark('indexDocuments')

const lastExportDate = await getExportDate(this.storage)
if (!lastExportDate || !this.isLocalSearch) {
// No persisted index found: let's create them
Expand Down Expand Up @@ -113,6 +122,11 @@ export class SearchEngine {
// Use replication events to have up-to-date search indexes, based on local data
this.indexOnReplicationEvents()
}

this.performanceApi.measure({
markName: markName,
category: 'Search'
})
}

indexOnReplicationEvents(): void {
Expand Down Expand Up @@ -286,6 +300,9 @@ export class SearchEngine {
async indexDocsForSearch(
doctype: keyof typeof SEARCH_SCHEMA
): Promise<SearchIndex | null> {
const markeNameIndex = this.performanceApi.mark(
`indexDocsForSearch ${doctype}`
)
const searchIndex = this.searchIndexes[doctype]
const startIndexing = performance.now()

Expand All @@ -294,6 +311,11 @@ export class SearchEngine {
// First creation of search index
index = await this.initialIndexation(doctype)
if (!index) {
this.performanceApi.measure({
markName: markeNameIndex,
measureName: `${markeNameIndex} initial indexation`,
category: 'Search'
})
return null
}
} else {
Expand All @@ -307,6 +329,13 @@ export class SearchEngine {
log.debug(
`Indexing ${doctype} took ${(endIndexing - startIndexing).toFixed(2)} ms`
)

this.performanceApi.measure({
markName: markeNameIndex,
measureName: `${markeNameIndex} incremental indexation`,
category: 'Search'
})

return index
}

Expand All @@ -320,6 +349,8 @@ export class SearchEngine {
return null
}

const markeNameIndex = this.performanceApi.mark('search')

const allResults = this.searchOnIndexes(query, options?.doctypes)
const dedupResults = this.deduplicateAndFlatten(allResults)
const sortedResults = this.sortSearchResults(
Expand All @@ -333,7 +364,14 @@ export class SearchEngine {
const normalizedRes = normalizeSearchResult(this.client, res, query)
normResults.push(normalizedRes)
}
return normResults.filter(res => res.title)
const output = normResults.filter(res => res.title)

this.performanceApi.measure({
markName: markeNameIndex,
category: 'Search'
})

return output
}

searchOnIndexes(
Expand Down

0 comments on commit 2e87021

Please sign in to comment.