在Apollo Server中,可以使用插件来测量执行时间。下面是一个例子:
const { ApolloServer } = require('apollo-server');
const { ApolloServerPluginInlineTrace } = require('apollo-server-core');
const typeDefs = ...
const resolvers = ...
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [ApolloServerPluginInlineTrace()],
});
server.listen().then(({ url }) => {
console.log(`� Server ready at ${url}`);
});
在上面的代码中,我们使用了ApolloServerPluginInlineTrace
插件来测量执行时间。这个插件会自动在HTTP响应头中添加Apollo-Trace-Parent-Span
和Apollo-Trace-Span
两个字段,用于追踪请求和响应之间的关系。
要查看执行时间,可以在请求头中添加x-apollo-tracing: 1
。这将使Apollo Server返回一个包含执行时间的tracing
字段的响应。例如:
{
"data": {
"hello": "world"
},
"extensions": {
"tracing": {
"version": 1,
"startTime": "2021-09-14T22:24:20.680Z",
"endTime": "2021-09-14T22:24:20.694Z",
"duration": 14079100,
"parsing": {
"startOffset": 5900,
"duration": 90300
},
"validation": {
"startOffset": 99700,
"duration": 164100
},
"execution": {
"resolvers": [
{
"path": [
"hello"
],
"parentType": "Query",
"fieldName": "hello",
"returnType": "String!",
"startOffset": 261500,
"duration": 1155700
}
],
"duration": 1155700
}
}
}
}
在上面的响应中,我们可以看到`tr