Skip to content

Commit

Permalink
changing unit tests to use performance perf_hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
crysmags committed Jun 7, 2024
1 parent 798670e commit a419471
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions packages/datadog-plugin-graphql/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ const getPort = require('get-port')
const dc = require('dc-polyfill')
const plugin = require('../src')

const { performance } = require('perf_hooks')

describe('Plugin', () => {
let tracer
let graphql
let schema
let sort
let clock

afterEach(() => {
if (clock) {
clock.restore()
clock = undefined
}
})
let markFast
let markSlow
let markSync

function buildSchema () {
const Human = new graphql.GraphQLObjectType({
Expand Down Expand Up @@ -93,22 +91,24 @@ describe('Plugin', () => {
type: graphql.GraphQLString,
resolve (obj, args) {
return new Promise((resolve) => {
setTimeout(() => resolve('fast field'), 100)
markFast = performance.now()
resolve('fast field')
})
}
},
slowAsyncField: {
type: graphql.GraphQLString,
resolve (obj, args) {
return new Promise((resolve) => {
setTimeout(() => resolve('slow field'), 1234)
markSlow = performance.now()
resolve('slow field')
})
}
},
syncField: {
type: graphql.GraphQLString,
resolve (obj, args) {
clock.tick(7)
markSync = performance.now()
return 'sync field'
}
}
Expand Down Expand Up @@ -412,7 +412,6 @@ describe('Plugin', () => {
})

it('should instrument each field resolver duration independently', done => {
clock = sinon.useFakeTimers()
const source = `
{
human {
Expand All @@ -427,22 +426,26 @@ describe('Plugin', () => {
let foundSlowFieldSpan = false
let foundSyncFieldSpan = false

let fastAsyncTime
let slowAsyncTime
let syncTime

const processTraces = (traces) => {
try {
for (const trace of traces) {
for (const span of trace) {
if (span.name !== 'graphql.resolve') {
continue
}
const spanDurationMs = Number(span.duration) / 1000000

if (span.resource === 'fastAsyncField:String') {
expect(spanDurationMs).to.equal(100)
expect(fastAsyncTime).to.be.below(slowAsyncTime)
foundFastFieldSpan = true
} else if (span.resource === 'slowAsyncField:String') {
expect(spanDurationMs).to.equal(1234)
expect(slowAsyncTime).to.be.below(syncTime)
foundSlowFieldSpan = true
} else if (span.resource === 'syncField:String') {
expect(spanDurationMs).to.equal(7)
expect(syncTime).to.be.above(slowAsyncTime)
foundSyncFieldSpan = true
}

Expand All @@ -460,13 +463,19 @@ describe('Plugin', () => {
}

agent.subscribe(processTraces)
graphql.graphql({ schema, source }).catch((e) => {
agent.unsubscribe(processTraces)
done(e)
})

// Asyncronously tick forward to release the pending graphql resolver timeouts
clock.tickAsync(2000)
const markStart = performance.now()

graphql.graphql({ schema, source })
.then((result) => {
fastAsyncTime = markFast - markStart
slowAsyncTime = markSlow - markStart
syncTime = markSync - markStart
})
.catch((e) => {
agent.unsubscribe(processTraces)
done(e)
})
})

it('should instrument nested field resolvers', done => {
Expand Down

0 comments on commit a419471

Please sign in to comment.