File size: 2,761 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Command } from 'commander'
import console from 'console'

import PQueue from 'p-queue'
import {
  generateProjects,
  cleanupProjectFolders,
  TEST_PROJECT_NAME,
} from './project-utils.js'
import { printBenchmarkResults } from './chart.js'
import { genRetryableRequest } from './gen-request.js'
import { bold, red } from '../../packages/next/dist/lib/picocolors.js'

const program = new Command()

const queue = new PQueue({ concurrency: 50 })
const TTFB_OUTLIERS_THRESHOLD = 1500

let progress = 0

program.option('-p, --path <path>')
program.option('-s, --skip-build', 'Skip build step')
program.option('-f, --force-crash', 'Force function crash')

program.parse(process.argv)

const options = program.opts()

if (options.path) {
  console.log('Running benchmark for path: ', options.path)
}

if (options.skipBuild) {
  console.log('Skipping build step')
}

if (options.forceCrash) {
  console.log('Forcing function crash')
}

export const forceCrash = options.forceCrash

try {
  const [originDeploymentURL, headDeploymentURL] = options.skipBuild
    ? [
        `https://${TEST_PROJECT_NAME}-origin.vercel.app`,
        `https://${TEST_PROJECT_NAME}-head.vercel.app`,
      ]
    : await generateProjects()

  const originBenchmarkURL = `${originDeploymentURL}${options.path || ''}`
  const headBenchmarkURL = `${headDeploymentURL}${options.path || ''}`

  console.log(`Origin deployment URL: ${originBenchmarkURL}`)
  console.log(`Head deployment URL: ${headBenchmarkURL}`)
  console.log(`Running benchmark...`)

  const benchResults = await runBenchmark(originBenchmarkURL)

  const headBenchResults = await runBenchmark(headBenchmarkURL)

  console.log(bold('Benchmark results for cold:'))
  printBenchmarkResults(
    {
      origin: benchResults,
      head: headBenchResults,
    },
    (r) => r.cold && r.firstByte <= TTFB_OUTLIERS_THRESHOLD && r.firstByte
  )
  console.log(bold('Benchmark results for hot:'))
  printBenchmarkResults(
    {
      origin: benchResults,
      head: headBenchResults,
    },
    (r) => !r.cold && r.firstByte <= TTFB_OUTLIERS_THRESHOLD && r.firstByte
  )
} catch (err) {
  console.log(red('Benchmark failed: ', err))
} finally {
  await cleanupProjectFolders()
}

async function runBenchmark(url) {
  progress = 0
  process.stdout.write(`Sending requests to ${url} ...\n`)
  process.stdout.write(`Progress: ${++progress}/500`)
  return (
    await Promise.all(
      Array.from({ length: 500 }).map(async () => {
        const p = await queue.add(() => genRetryableRequest(url))
        refreshProgress()
        return p
      })
    )
  ).filter(Boolean)
}

function refreshProgress() {
  process.stdout.clearLine()
  process.stdout.cursorTo(0)
  process.stdout.write(`Requests sent: ${++progress}/500`)
}