Skip to content

Commit

Permalink
Add dezalgo slide and add other metrics to performance test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Qard committed Nov 10, 2019
1 parent b38a6ba commit 060f38e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
6 changes: 6 additions & 0 deletions examples/dezalgo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
async function* dezalgo (iterator) {
for await (const item of iterator) {
await new Promise(setImmediate)
yield item
}
}
65 changes: 47 additions & 18 deletions performance/make-line.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
const { spawn } = require('child_process')

const MeasureStream = require('./measure-stream')

// const { join } = require('path')
// const { spawn } = require('child_process')
//
// module.exports = (test, mode) => {
// const gauge = new Measure(mode)
//
// const proc = spawn('node', ['container.js', test, mode], {
// cwd: __dirname
// })
// proc.stdout.pipe(gauge)
//
// return gauge.data
// }

module.exports = (test, mode) => {
const gauge = new MeasureStream(mode)
let lastCpu = process.cpuUsage()

const computers = {
bps (last, chunk) {
return last + chunk.length
},

rss (last, next) {
return process.memoryUsage().rss
},

heapTotal (last, next) {
return process.memoryUsage().heapTotal
},

user (last) {
if (last === 0) lastCpu = process.cpuUsage()
return process.cpuUsage(lastCpu).user
},

system (last) {
if (last === 0) lastCpu = process.cpuUsage()
return process.cpuUsage(lastCpu).system
}
}

module.exports = (test, metric, mode) => {
const computer = computers[metric]
if (!computer) throw new Error(`Unknown metric "${metric}"`)

const gauge = new MeasureStream(mode, computer)

requireAndRun(test, mode, gauge)
// spawnAndRun(test, mode, gauge)

return gauge.data
}

function requireAndRun (test, mode, gauge) {
const producer = require(`./${test}/producer`)
const run = require(`./${test}/${mode}`)
run(producer(), gauge)

return gauge.data
}

function spawnAndRun (test, mode, gauge) {
const proc = spawn('node', ['container.js', test, mode], {
cwd: __dirname
})
proc.stdout.pipe(gauge)
}
11 changes: 6 additions & 5 deletions performance/measure-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ const colors = ['red', 'yellow']
let n = 0

class MeasureStream extends Writable {
constructor(mode) {
constructor(mode, compute) {
super()
this.timer = microtime()
this.compute = compute

this.data = {
title: mode,
x: new CappedArray(50),
y: new CappedArray(50),
y: new CappedArray(60),
x: new CappedArray(60),
style: {
line: colors[n++]
}
Expand All @@ -28,9 +29,9 @@ class MeasureStream extends Writable {

if (!x.length || x[x.length - 1] !== bucket) {
x.push(bucket)
y.push(chunk.length)
y.push(this.compute(0, chunk, now))
} else {
y[y.length - 1] = chunk.length + y[y.length - 1]
y[y.length - 1] = this.compute(y[y.length - 1], chunk, now)
}

setImmediate(callback)
Expand Down
17 changes: 11 additions & 6 deletions performance/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const test = process.argv[2]
const modes = (process.argv[3] || 'async-iterator,stream').split(',')
let modes = process.argv[3]

if (!modes || modes === 'all') {
modes = 'async-iterator,stream'
}

modes = modes.split(',')
const metric = process.argv[4] || 'bps'

const blessed = require('blessed')
const blessedContrib = require('blessed-contrib')
Expand All @@ -8,9 +15,7 @@ const makeLine = require('./make-line')

const screen = blessed.screen()

screen.key(['escape', 'q', 'C-c'], function (ch, key) {
return process.exit(0)
})
screen.key(['escape', 'q', 'C-c'], () => process.exit(0))

const line = blessedContrib.line({
label: test,
Expand All @@ -24,7 +29,7 @@ function update () {
screen.render()
}

const data = modes.map(makeLine.bind(null, test))
const data = modes.map(makeLine.bind(null, test, metric))

update()
setInterval(update, 1000 / 60)
setInterval(update, 1000 / 1)
8 changes: 8 additions & 0 deletions slides.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ import { Appear } from 'mdx-deck'
]}
/>
---
<CodeSurfer
title="Remember dezalgo?"
code={require('!raw-loader!./examples/dezalgo.js').default}
steps={[
{ notes: "Ḥ̶̷͉̋̎ͩ̔̚ͅe̵̮ͫ̀͞ͅ ̂ͮ̌ͩ̉͝͏̙̱̥r̡̙͓̙̲̲̯̞̼̓͐̏̄͆̀ͮ͊̿ệ̸͖̭̤͕͚̬̚ͅt̶ͤ͋͛̑͜͏̠̪͔̲͚̙̦u̡̥̖̍ͮͧͪ̄͡r̵̤̰̥̭̦̲͐̓̐̆͑ň̝̜̭̔̉ͧ̎̎͋̒͘͟͠s̶̬̜̤̳̻͛̑̅ͨ̉ͦ͒ͨ̀̚ͅ,̰̠͇̩͙̋̅̋͊̈́̀ ̲̖̗̉ͯ̎̔̑̆͟w͐͌͆̿ͮͯ҉̢͔̲̻̱͕̻͞ͅi̸̞͖̙͔̲͉̼ͬṯ̼̌̐̀ͩ̈́̐́̓͞h̗̩̦͓̽̌̈̈̋͆̀͗ ̍̔̔̅̐ͥ̓̽̾͏҉̻̲̣̲̮̩ͅc͖̞̰̪͍̍̿͋̈̀ͣ͊ͬͭ͠h̷̜̗͍̺̥ͪ͊ͣͩ̚a̺ͩͤ͗ͨ̂̕͜o̟̖͚̟̗͉̠̗̿͂ͥ͌͑ͤ͂̒ͦ̀͢s̶̮̬̲̲̦̳ͣ̈́ͧ͒̕͞ͅ ̡̪̘̖͐͒ͤͨ̌̈́ͭͣ̐i͆́͆ͫ̄͑ͬ͆̉͏̝̤̹͍͖̩̩̺͜n͔̣̠̳̱͚̘͒̽͑̎ ͙̖̦̣̄̽́̽̎̍̊ͧ͜h͇̻̙̬ͯ̽ͣ̓ͣ́ͤͭ̈́i̞͖̰̿̔̎ͧ́s̨̯̫͍̝̰͉̬ͥ̍̊͐́̍̎̿ ̸̲̗͍̗͉̪͉̙͛̿̋͑̓͒ͥ̚̚͜w̭͖̻̺̬̳͕̗̄̽̃͌â̵̲͔ͭͪ͛͊̿͝ͅk̗̤̣͐̒̿̌͐̐̿̚͘e̸̳͎͇̺ͪ̅̓̿.͂̓̌̎̋͝҉̲͢" },
]}
/>
---
# What about performance?
## I've heard promises are slow
---
Expand Down

1 comment on commit 060f38e

@vercel
Copy link

@vercel vercel bot commented on 060f38e Nov 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.