function runBenchmarks()

in scripts/benchmarks.js [88:169]


function runBenchmarks() {
  const outputFile = process.argv[2]
  const lernaProcess = spawn('lerna', ['run', 'bench', '--stream'])
  lernaProcess.on('exit', code => {
    if (code !== 0) {
      process.exit(code)
    }
  })
  lernaProcess.on('close', async () => {
    try {
      const results = await getAllBenchmarkResults()
      if (results.length === 0) {
        console.warn('No benchmarks results found', 'Skipping this run')
        process.exit(1)
      }
      const gitlog = execSync('git', [
        'log',
        '-1',
        '--pretty=%h,%s',
        '--no-merges'
      ])
      const [commit, commitMessage] = gitlog.split(',')
      const branch = execSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'])

      const baseOutput = {
        process: {
          version: process.version,
          arch: process.arch,
          platform: process.platform
        },
        meta: {
          commit,
          commitMessage,
          branch,
          agentName: 'rum-js'
        }
      }

      let resultObj = {}
      for (let result of results) {
        const metricKey = result.name || result.scenario
        const browser = result.browser
        if (!resultObj[metricKey]) {
          resultObj[metricKey] = {}
        }
        resultObj[metricKey][browser] = result
      }

      const output = Object.assign({}, baseOutput, {
        metrics: resultObj,
        '@timestamp': Date.now()
      })

      /**
       * DEV - show the results in the terminal
       * CI - store the results in file and upload to ES
       */
      if (!outputFile) {
        console.log(
          'Benchmark Results:',
          '\n',
          JSON.stringify(output, undefined, 2)
        )
        return
      }

      /**
       * NDJSON format for uploading to ES
       */
      let ndJSONOutput = '{"index": { "_index": "benchmarks-rum-js" }}' + '\n'
      ndJSONOutput += JSON.stringify(output)
      ndJSONOutput += '\n'

      const outputPath = join(PKG_DIR, outputFile)
      await pWriteFile(outputPath, ndJSONOutput)
      console.log('Benchmark results is stored in', outputPath)
    } catch (err) {
      console.error('Error running benchmark script', err)
      process.exit(2)
    }
  })
}