Skip to content

Commit ee55d23

Browse files
committed
feat: TEMP ADD PERF LOGS
1 parent 79aa221 commit ee55d23

10 files changed

Lines changed: 131 additions & 16 deletions

File tree

packages/cozy-client/src/CozyClient.js

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ class CozyClient {
474474
}
475475

476476
async _login(options) {
477+
console.log('🟥 login 1')
477478
this.emit('beforeLogin')
478479

479480
this.registerClientOnLinks()
@@ -487,18 +488,21 @@ class CozyClient {
487488
}
488489
}
489490

491+
console.log('🟥 login 2')
490492
for (const link of this.links) {
491493
if (link.onLogin) {
492494
await link.onLogin()
493495
}
494496
}
497+
console.log('🟥 login 3')
495498

496499
this.isLogged = true
497500
this.isRevoked = false
498501

499502
if (this.stackClient instanceof OAuthClient) {
500503
await this.loadInstanceOptionsFromStack()
501504
}
505+
console.log('🟥 login 4')
502506

503507
this.emit('login')
504508
}
@@ -926,6 +930,8 @@ client.query(Q('io.cozy.bills'))`)
926930
* @returns {Promise<import("./types").QueryResult>}
927931
*/
928932
async query(queryDefinition, { update, executeFromStore, ...options } = {}) {
933+
console.log('⏰query 1')
934+
const beginPrepare = performance.now()
929935
const markQuery = this.performanceApi.mark(
930936
`client.query(${queryDefinition.doctype})`
931937
)
@@ -976,21 +982,28 @@ client.query(Q('io.cozy.bills'))`)
976982
executeQueryFromState(this.store.getState(), queryDefinition)
977983
)
978984
: () => this.requestQuery(queryDefinition, options)
985+
const endPrepare = performance.now()
986+
console.log('🍺 CozyClient query prepare took', (endPrepare - beginPrepare), 'ms')
987+
const beginExec = performance.now()
979988
const response = await this._promiseCache.exec(requestFn, () =>
980989
stringify(queryDefinition)
981990
)
991+
const endExec = performance.now()
992+
console.log('🍺 CozyClient query exec took', (endExec - beginExec), 'ms')
982993

994+
const beginReceive = performance.now()
995+
const queryReceivedResult = receiveQueryResult(queryId, response, {
996+
update,
997+
backgroundFetching
998+
})
999+
const endReceive = performance.now()
1000+
console.log('🍺 CozyClient query receiveQueryResult took', (endReceive - beginReceive), 'ms')
1001+
const beginDispatch = performance.now()
9831002
this.dispatch(
984-
receiveQueryResult(queryId, response, {
985-
update,
986-
backgroundFetching
987-
})
1003+
queryReceivedResult
9881004
)
989-
this.performanceApi.measure({
990-
markName: markQuery,
991-
measureName: `${markQuery} success`,
992-
color: 'primary'
993-
})
1005+
const endDispatch = performance.now()
1006+
console.log('🍺 CozyClient query Dispatch took', (endDispatch - beginDispatch), 'ms')
9941007
return response
9951008
} catch (error) {
9961009
this.performanceApi.measure({
@@ -1025,10 +1038,14 @@ client.query(Q('io.cozy.bills'))`)
10251038
options.as || this.queryIdGenerator.generateId(queryDefinition)
10261039
const mergedOptions = { ...options, as: queryId }
10271040
try {
1041+
const begin = performance.now()
10281042
let resp = await this.query(queryDefinition, mergedOptions)
1043+
const end = performance.now()
1044+
console.log('🍺 first query took', (end - begin), 'ms')
10291045
const documents = resp.data
10301046

10311047
while (resp && resp.next) {
1048+
console.log('🍺 while')
10321049
if (resp.bookmark) {
10331050
resp = await this.query(
10341051
queryDefinition.offsetBookmark(resp.bookmark),
@@ -1046,6 +1063,7 @@ client.query(Q('io.cozy.bills'))`)
10461063
}
10471064
documents.push(...resp.data)
10481065
}
1066+
console.log('🍺 return docs')
10491067
return documents
10501068
} catch (e) {
10511069
logger.log(`queryAll error for ${e.toString()}`)
@@ -1111,17 +1129,27 @@ client.query(Q('io.cozy.bills'))`)
11111129
* @returns {Promise<import("./types").ClientResponse>}
11121130
*/
11131131
async requestQuery(definition, options) {
1132+
const begin = performance.now()
11141133
const mainResponse = await this.chain.request(definition, options)
1134+
const end = performance.now()
1135+
console.log('🛋️ CozyClient chain.request took', (end - begin), 'ms')
11151136

1137+
const begin2 = performance.now()
11161138
await this.persistVirtualDocuments(definition, mainResponse.data)
1139+
const end2 = performance.now()
1140+
console.log('🛋️ CozyClient persistVirtualDocuments took', (end2 - begin2), 'ms')
11171141

11181142
if (!definition.includes) {
1143+
console.log('🛋️ CozyClient return no-includes')
11191144
return mainResponse
11201145
}
1146+
const begin3 = performance.now()
11211147
const withIncluded = await this.fetchRelationships(
11221148
mainResponse,
11231149
this.getIncludesRelationships(definition)
11241150
)
1151+
const end3 = performance.now()
1152+
console.log('🛋️ CozyClient fetchRelationships took', (end3 - begin3), 'ms')
11251153
return withIncluded
11261154
}
11271155

@@ -1188,6 +1216,7 @@ client.query(Q('io.cozy.bills'))`)
11881216
}
11891217

11901218
if ((!document.meta?.rev && !document._rev) || enforce) {
1219+
console.log('🚨🚨🚨🚨 PERSIST')
11911220
await this.chain.persistCozyData(document)
11921221
}
11931222
}
@@ -1814,12 +1843,20 @@ instantiation of the client.`
18141843
* @returns {Promise<void>}
18151844
*/
18161845
async loadInstanceOptionsFromStack() {
1846+
console.log('🔰 loadInstanceOptionsFromStack 1')
18171847
const results = await Promise.all([
18181848
this.query(
18191849
Q('io.cozy.settings').getById('io.cozy.settings.capabilities')
1820-
),
1821-
this.query(Q('io.cozy.settings').getById('io.cozy.settings.instance'))
1850+
).catch(error => {
1851+
console.log(error)
1852+
throw error
1853+
}),
1854+
this.query(Q('io.cozy.settings').getById('io.cozy.settings.instance')).catch(error => {
1855+
console.log(error)
1856+
throw error
1857+
})
18221858
])
1859+
console.log('🔰 loadInstanceOptionsFromStack 2')
18231860

18241861
const { data: capabilitiesData } = results[0]
18251862
const { data: instanceData } = results[1]

packages/cozy-client/src/StackLink.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ export default class StackLink extends CozyLink {
9090
}
9191

9292
async request(operation, options, result, forward) {
93+
console.log('stack request')
9394
if (!options?.forceStack && this.isOnline && !(await this.isOnline())) {
95+
console.log('forward')
9496
return forward(operation, options)
9597
}
9698

@@ -108,6 +110,7 @@ export default class StackLink extends CozyLink {
108110
}
109111

110112
async persistCozyData(data, forward) {
113+
console.log('persistCozyData from StackLink')
111114
return forward(data)
112115
}
113116
/**

packages/cozy-client/src/WebFlagshipLink.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class WebFlagshipLink extends CozyLink {
2323
}
2424

2525
async persistCozyData(data, forward) {
26+
console.log('persistCozyData from WebFlagshipLink')
2627
// Persist data should do nothing here as data is already persisted on Flagship side
2728
return
2829
}

packages/cozy-client/src/hooks/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @returns
1111
*/
1212
export const equalityCheckForQuery = (queryResA, queryResB) => {
13+
// console.log('🌈 Equality check for query')
1314
//console.log('Call equality check : ', queryResA, queryResB)
1415
if (queryResA === queryResB) {
1516
// Referential equality

packages/cozy-client/src/store/documents.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { isReceivingMutationResult } from './mutations'
1414
import { properId } from './helpers'
1515

1616
const storeDocument = (state, document) => {
17+
console.log('🏪 storeDocument')
1718
const type = document._type
1819
if (!type) {
1920
if (process.env.NODE_ENV !== 'production') {
@@ -50,6 +51,7 @@ export const mergeDocumentsWithRelationships = (
5051
prevDocument = {},
5152
nextDocument = {}
5253
) => {
54+
console.log('🏪 mergeDocumentsWithRelationships')
5355
/**
5456
* @type {import("../types").CozyClientDocument}
5557
*/
@@ -69,6 +71,8 @@ export const mergeDocumentsWithRelationships = (
6971

7072
// reducer
7173
const documents = (state = {}, action) => {
74+
// console.log('🏪 documents reducer')
75+
// const begin = performance.now()
7276
if (!isReceivingData(action) && !isReceivingMutationResult(action)) {
7377
return state
7478
}
@@ -80,28 +84,40 @@ const documents = (state = {}, action) => {
8084
) {
8185
const docId = action.definition.document._id
8286
const _type = action.definition.document._type
87+
88+
// const end = performance.now()
89+
// console.log('🏪 documents reducer 1 took', (end - begin), 'ms')
8390
return {
8491
...state,
8592
[_type]: omit(state[_type], docId)
8693
}
8794
}
8895

8996
const { data, included } = action.response
90-
if (!data || (Array.isArray(data) && data.length === 0)) return state
97+
if (!data || (Array.isArray(data) && data.length === 0)) {
98+
// const end = performance.now()
99+
// console.log('🏪 documents reducer 2 took', (end - begin), 'ms')
100+
return state
101+
}
91102

92103
const updatedStateWithIncluded = included
93104
? included.reduce(storeDocument, state)
94105
: state
95106
if (!Array.isArray(data)) {
107+
// const end = performance.now()
108+
// console.log('🏪 documents reducer 3 took', (end - begin), 'ms')
96109
return storeDocument(updatedStateWithIncluded, data)
97110
}
111+
// const end = performance.now()
112+
// console.log('🏪 documents reducer 4 took', (end - begin), 'ms')
98113
return extractAndMergeDocument(data, updatedStateWithIncluded)
99114
}
100115

101116
export default documents
102117

103118
// selector
104119
export const getDocumentFromSlice = (state = {}, doctype, id) => {
120+
// console.log('🏪 getDocumentFromSlice')
105121
if (!doctype) {
106122
throw new Error(
107123
'getDocumentFromSlice: Cannot retrieve document with undefined doctype'
@@ -133,6 +149,7 @@ export const getDocumentFromSlice = (state = {}, doctype, id) => {
133149
}
134150

135151
export const getCollectionFromSlice = (state = {}, doctype) => {
152+
// console.log('🏪 getCollectionFromSlice')
136153
if (!doctype) {
137154
throw new Error(
138155
'getDocumentFromSlice: Cannot retrieve document with undefined doctype'

packages/cozy-client/src/store/queries.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const query = (
140140
action,
141141
documents
142142
) => {
143+
// console.log('🏪 query', action.type)
143144
switch (action.type) {
144145
case INIT_QUERY:
145146
if (
@@ -172,12 +173,15 @@ const query = (
172173
fetchStatus: 'loading'
173174
}
174175
case RECEIVE_QUERY_RESULT: {
176+
// const begin = performance.now()
175177
const markName = performanceApi.mark('RECEIVE_QUERY_RESULT')
176178
const response = action.response
177179
// Data can be null when we get a 404 not found
178180
// see Collection.get()
179181
// but we still need to update the fetchStatus.
180182
if (!response.data) {
183+
// const end = performance.now()
184+
// console.log('🏪 RECEIVE_QUERY_RESULT with no data took', (end - begin), 'ms')
181185
performanceApi.measure({
182186
markName: markName,
183187
measureName: `${markName} with no data`,
@@ -204,6 +208,8 @@ const query = (
204208
}
205209

206210
if (!Array.isArray(response.data)) {
211+
// const end = performance.now()
212+
// console.log('🏪 RECEIVE_QUERY_RESULT with object took', (end - begin), 'ms')
207213
performanceApi.measure({
208214
markName: markName,
209215
measureName: `${markName} with object`,
@@ -227,6 +233,8 @@ const query = (
227233
measureName: `${markName} with background fetching`,
228234
category: 'CozyClientStore'
229235
})
236+
// const end = performance.now()
237+
// console.log('🏪 RECEIVE_QUERY_RESULT with background fetching took', (end - begin), 'ms')
230238
return {
231239
...state,
232240
...common,
@@ -243,6 +251,8 @@ const query = (
243251
fetchedPagesCount
244252
})
245253

254+
// const end = performance.now()
255+
// console.log('🏪 RECEIVE_QUERY_RESULT default took', (end - begin), 'ms')
246256
performanceApi.measure({
247257
markName: markName,
248258
measureName: `${markName} default`,

0 commit comments

Comments
 (0)