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 Jan 29, 2025
1 parent 06d1e40 commit 1e13480
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'cozy-client'
import { CozyLink } from 'cozy-client'
import { CozyLink, defaultPerformanceApi } from 'cozy-client'

Check failure on line 2 in packages/cozy-dataproxy-lib/src/search/@types/cozy-client.d.ts

View workflow job for this annotation

GitHub Actions / Build and publish

'defaultPerformanceApi' is defined but never used
import {
CozyClientDocument,
QueryOptions,
Expand Down
41 changes: 38 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 @@ -48,10 +49,12 @@ export class SearchEngine {
searchIndexes: SearchIndexes
debouncedReplication: () => void
isLocalSearch: boolean
performanceApi: PerformanceAPI

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

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

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

// Create search indexes by querying docs, either locally or remotely
for (const doctype of SEARCHABLE_DOCTYPES) {
const searchIndex = await this.indexDocsForSearch(
Expand All @@ -90,6 +96,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 @@ -337,6 +348,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 @@ -345,6 +359,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 @@ -358,6 +377,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 @@ -368,6 +394,8 @@ export class SearchEngine {
return []
}

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

const allResults = this.searchOnIndexes(query, options?.doctypes)
const dedupResults = this.deduplicateAndFlatten(allResults)
const sortedResults = this.sortSearchResults(
Expand All @@ -381,7 +409,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 1e13480

Please sign in to comment.