|
| 1 | +'use strict' |
| 2 | + |
| 3 | +process.on('SIGINT', end) |
| 4 | + |
| 5 | +let agent |
| 6 | +if (process.env.AGENT) { |
| 7 | + agent = require('../../').start({ |
| 8 | + serviceName: '002-custom-transactions-normal-sized', |
| 9 | + captureExceptions: false |
| 10 | + }) |
| 11 | +} |
| 12 | + |
| 13 | +const callstack = require('./utils/callstack') |
| 14 | + |
| 15 | +let start |
| 16 | +const pid = process.argv[2] |
| 17 | +const warmup = 1e4 |
| 18 | +const runtime = 20 |
| 19 | +const metrics = { |
| 20 | + transactions: 0 |
| 21 | +} |
| 22 | + |
| 23 | +// To avoid randomness, but still generate what appears to be natural random |
| 24 | +// call stacks, number of spans etc, use a pre-defined set of numbers |
| 25 | +const numbers = [2, 5, 10, 0, 2, 21, 2, 5, 6, 9, 1, 11, 9, 8, 12] |
| 26 | +let numbersSpanIndex = 5 |
| 27 | +let numbersStackLevelIndex = 0 |
| 28 | + |
| 29 | +// warm up |
| 30 | +console.error('Warming up for %d transactions...', warmup) |
| 31 | +addTransaction(function runAgain () { |
| 32 | + if (metrics.transactions < warmup) return addTransaction(runAgain) |
| 33 | + |
| 34 | + console.error('Running benchmark for %d seconds...', runtime) |
| 35 | + |
| 36 | + setTimeout(end, runtime * 1000) |
| 37 | + |
| 38 | + process.kill(pid, 'SIGINT') |
| 39 | + metrics.transactions = 0 |
| 40 | + start = process.hrtime() |
| 41 | + |
| 42 | + // actual benchmark |
| 43 | + addTransaction(function runAgain () { |
| 44 | + addTransaction(runAgain) |
| 45 | + }) |
| 46 | +}) |
| 47 | + |
| 48 | +function addTransaction (cb) { |
| 49 | + if (agent) agent.startTransaction('my-transaction') |
| 50 | + const amount = numbers[numbersStackLevelIndex % numbers.length] |
| 51 | + callstack(amount, () => { |
| 52 | + const amount = numbers[numbersSpanIndex % numbers.length] |
| 53 | + addSpan(amount, () => { |
| 54 | + if (agent) agent.endTransaction() |
| 55 | + metrics.transactions++ |
| 56 | + setImmediate(cb) |
| 57 | + }) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +function addSpan (amount, cb) { |
| 62 | + if (typeof amount === 'function') return addSpan(1, amount) |
| 63 | + // setImmediate(() => { |
| 64 | + const span = agent && agent.startSpan('my-span', 'my-span-type') |
| 65 | + // setImmediate(() => { |
| 66 | + if (agent) span.end() |
| 67 | + if (--amount === 0) cb() |
| 68 | + else addSpan(amount, cb) |
| 69 | + // }) |
| 70 | + // }) |
| 71 | +} |
| 72 | + |
| 73 | +function end () { |
| 74 | + const hrtime = process.hrtime(start) |
| 75 | + process.kill(pid, 'SIGINT') |
| 76 | + process.stdout.write(JSON.stringify({hrtime, metrics})) |
| 77 | + process.exit() |
| 78 | +} |
0 commit comments