File size: 2,824 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
105
import downsampler from 'downsample-lttb'
import asciichart from 'asciichart'
import terminalSize from 'term-size'

const CHART_WIDTH = terminalSize().columns - 15 // space for the labels

function getMetrics(data) {
  const sorted = [...data].sort((a, b) => a - b)
  const getPercentile = (percentile) => {
    const index = Math.floor((sorted.length - 1) * percentile)
    return sorted[index]
  }
  return {
    hits: sorted.length,
    confidenceInterval: round(getConfidenceInterval(sorted)),
    median: getPercentile(0.5),
    avg: sorted.reduce((a, b) => a + b, 0) / sorted.length,
    p75: getPercentile(0.75),
    p95: getPercentile(0.95),
    p99: getPercentile(0.99),
    p25: getPercentile(0.25),
    min: sorted[0],
    max: sorted[sorted.length - 1],
  }
}

function round(num) {
  return Math.round(num * 100) / 100
}

// thanks Copilot
function getConfidenceInterval(data) {
  const n = data.length
  const m = data.reduce((a, b) => a + b) / n
  const s = Math.sqrt(
    data.map((x) => Math.pow(x - m, 2)).reduce((a, b) => a + b) / n
  )
  const z = 1.96 // 95% confidence
  const e = z * (s / Math.sqrt(n))
  return e
}

export function downsample(data, maxPoints) {
  const sortedData = [...data].sort((a, b) => a - b)
  return downsampler
    .processData(
      // the downsampler expects a 2d array of [x, y] values, so we need to add an index
      sortedData.map((p, i) => [p, i]),
      maxPoints
    )
    .map((p) => p[0])
}

export function printBenchmarkResults({ origin, head }, metricSelector) {
  const [processedOriginData, processedHeadData] = [origin, head].map(
    (results) => results.map(metricSelector).filter(Boolean)
  )

  if (processedHeadData.length === 0 || processedOriginData.length === 0) {
    console.log('No data to compare, skipping')
    return
  }

  const [originMetrics, headMetrics] = [
    processedOriginData,
    processedHeadData,
  ].map(getMetrics)

  const deltaMetrics = {
    min: headMetrics.min - originMetrics.min,
    max: headMetrics.max - originMetrics.max,
    avg: headMetrics.avg - originMetrics.avg,
    median: headMetrics.median - originMetrics.median,
    p95: headMetrics.p95 - originMetrics.p95,
    p99: headMetrics.p99 - originMetrics.p99,
    p75: headMetrics.p75 - originMetrics.p75,
    p25: headMetrics.p25 - originMetrics.p25,
  }

  console.table({
    origin: originMetrics,
    head: headMetrics,
    delta: deltaMetrics,
  })

  const [originData, headData] = [processedOriginData, processedHeadData].map(
    (data) =>
      downsample(
        data,
        Math.min(
          CHART_WIDTH,
          processedOriginData.length,
          processedHeadData.length
        )
      )
  )

  console.log(
    asciichart.plot([originData, headData], {
      height: 15,
      colors: [asciichart.blue, asciichart.red],
    })
  )
}