|
| 1 | +'use strict' |
| 2 | + |
| 3 | +require('dd-trace/init') |
| 4 | + |
| 5 | +const Fastify = require('fastify') |
| 6 | + |
| 7 | +const fastify = Fastify({ logger: { level: 'error' } }) |
| 8 | + |
| 9 | +fastify.get('/deeply-nested-large-object', function handler () { |
| 10 | + // The size of `obj` generated is carefully tuned to never result in a snapshot larger than the 1MB size limit, while |
| 11 | + // still being large enough to trigger the time budget limit. However, the size of `fastify` and `request` is not |
| 12 | + // stable across Node.js and Fastify versions, so the generated object might need to be adjusted. |
| 13 | + const obj = generateObject(5, 12) // eslint-disable-line no-unused-vars |
| 14 | + const start = process.hrtime.bigint() |
| 15 | + const diff = process.hrtime.bigint() - start // BREAKPOINT: /deeply-nested-large-object |
| 16 | + return { paused: Number(diff) / 1_000_000 } |
| 17 | +}) |
| 18 | + |
| 19 | +fastify.get('/large-object-of-primitives', function handler () { |
| 20 | + const obj = generateObject(0, 1_000_000) // eslint-disable-line no-unused-vars |
| 21 | + const start = process.hrtime.bigint() |
| 22 | + const diff = process.hrtime.bigint() - start // BREAKPOINT: /large-object-of-primitives |
| 23 | + return { paused: Number(diff) / 1_000_000 } |
| 24 | +}) |
| 25 | + |
| 26 | +fastify.get('/large-array-of-primitives', function handler () { |
| 27 | + const arr = Array.from({ length: 1_000_000 }, (_, i) => i) // eslint-disable-line no-unused-vars |
| 28 | + const start = process.hrtime.bigint() |
| 29 | + const diff = process.hrtime.bigint() - start // BREAKPOINT: /large-array-of-primitives |
| 30 | + return { paused: Number(diff) / 1_000_000 } |
| 31 | +}) |
| 32 | + |
| 33 | +fastify.get('/large-array-of-objects', function handler () { |
| 34 | + const arr = Array.from({ length: 1_000_000 }, (_, i) => ({ i })) // eslint-disable-line no-unused-vars |
| 35 | + const start = process.hrtime.bigint() |
| 36 | + const diff = process.hrtime.bigint() - start // BREAKPOINT: /large-array-of-objects |
| 37 | + return { paused: Number(diff) / 1_000_000 } |
| 38 | +}) |
| 39 | + |
| 40 | +fastify.listen({ port: process.env.APP_PORT || 0 }, (err) => { |
| 41 | + if (err) { |
| 42 | + fastify.log.error(err) |
| 43 | + process.exit(1) |
| 44 | + } |
| 45 | + process.send?.({ port: fastify.server.address().port }) |
| 46 | +}) |
| 47 | + |
| 48 | +const leafValues = [null, undefined, true, 1, ''] |
| 49 | +const complexTypes = ['object', 'array', 'map', 'set'] |
| 50 | + |
| 51 | +/** |
| 52 | + * Generate a complex nested object that requires a lot of async CDP calls to traverse |
| 53 | + */ |
| 54 | +function generateObject (depth, breath) { |
| 55 | + const obj = {} |
| 56 | + for (let i = 0; i < breath; i++) { |
| 57 | + const key = `p${i}` |
| 58 | + if (depth === 0) { |
| 59 | + obj[key] = leafValues[i % leafValues.length] |
| 60 | + } else { |
| 61 | + const type = complexTypes[i % complexTypes.length] |
| 62 | + obj[key] = generateType(type, depth - 1, breath) |
| 63 | + } |
| 64 | + } |
| 65 | + return obj |
| 66 | +} |
| 67 | + |
| 68 | +function generateArray (depth, breath) { |
| 69 | + const arr = [] |
| 70 | + for (let i = 0; i < breath; i++) { |
| 71 | + if (depth === 0) { |
| 72 | + arr.push(leafValues[i % leafValues.length]) |
| 73 | + } else { |
| 74 | + const type = complexTypes[i % complexTypes.length] |
| 75 | + arr.push(generateType(type, depth - 1, breath)) |
| 76 | + } |
| 77 | + } |
| 78 | + return arr |
| 79 | +} |
| 80 | + |
| 81 | +function generateMap (depth, breath) { |
| 82 | + const map = new Map() |
| 83 | + for (let i = 0; i < breath; i++) { |
| 84 | + if (depth === 0) { |
| 85 | + map.set(i, leafValues[i % leafValues.length]) |
| 86 | + } else { |
| 87 | + const type = complexTypes[i % complexTypes.length] |
| 88 | + map.set(i, generateType(type, depth - 1, breath)) |
| 89 | + } |
| 90 | + } |
| 91 | + return map |
| 92 | +} |
| 93 | + |
| 94 | +function generateSet (depth, breath) { |
| 95 | + const set = new Set() |
| 96 | + for (let i = 0; i < breath; i++) { |
| 97 | + if (depth === 0) { |
| 98 | + set.add(leafValues[i % leafValues.length]) |
| 99 | + } else { |
| 100 | + const type = complexTypes[i % complexTypes.length] |
| 101 | + set.add(generateType(type, depth - 1, breath)) |
| 102 | + } |
| 103 | + } |
| 104 | + return set |
| 105 | +} |
| 106 | + |
| 107 | +function generateType (type, depth, breath) { |
| 108 | + switch (type) { |
| 109 | + case 'object': |
| 110 | + return generateObject(depth, breath) |
| 111 | + case 'array': |
| 112 | + return generateArray(depth, breath) |
| 113 | + case 'map': |
| 114 | + return generateMap(depth, breath) |
| 115 | + case 'set': |
| 116 | + return generateSet(depth, breath) |
| 117 | + } |
| 118 | +} |
0 commit comments