我正在对 React Native 应用程序进行一些应用程序分析,我发现使用 apollo-link-logger
(以及我自己滚动的链接)和 Android 分析器的网络 channel 。对于探查器中约 600 毫秒的请求,我发现使用 apollo 链接系统的中间件需要 2 秒以上的时间。
我自己的链接(如下)中没有太多内容
const metricsLink = new ApolloLink((operation, forward) => {
const { operationName } = operation
const startTime = new Date().getTime()
const observable = forward(operation)
observable.subscribe({
complete: () => {
const elapsed = new Date().getTime() - startTime
console.warn(`[METRICS][${operationName}] (${elapsed}) complete`)
}
})
return observable
})
看起来它最终也会考虑到 apollo 管理这个请求链所需的时间。我通过直接使用 fetch 从其他端点获取并将这些时间与探查器时间(匹配)进行比较,确认这不是应用程序的普遍问题。
我应该期望这实际上反射(reflect)请求时间吗? native 网络请求和我在 apollo 客户端中看到的时间之间的其余时间可能会去哪里?
最佳答案
来自 Apollo Link docs ,有一个使用链接组合来测量时间的示例
注意:我已根据 this other SO answer 修改了示例以使用 performance.now()
而不是 new Date()
const timeStartLink = new ApolloLink((operation, forward) => {
operation.setContext({ start: performance.now() })
return forward(operation)
})
const logTimeLink = new ApolloLink((operation, forward) => {
return forward(operation).map(data => {
// data from a previous link
const time = performance.now() - operation.getContext().start
console.log(`operation ${operation.operationName} took ${time} to complete`)
return data
})
})
const link = timeStartLink.concat(logTimeLink)
关于graphql - 使用 apollo graphql 客户端记录请求时间的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56502738/